const config = require('../../config');
const providers = config.providers;
const crypto = require('crypto');
const bcrypt = require('bcrypt');
const moment = require('moment');

module.exports = function(sequelize, DataTypes) {
  const inspection_plans = sequelize.define(
    'inspection_plans',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

plan_number: {
        type: DataTypes.TEXT,
      
      

      },

inspection_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"incoming",


"in_process",


"final",


"audit"

        ],

      },

revision: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"inactive"

        ],

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

      importHash: {
        type: DataTypes.STRING(255),
        allowNull: true,
        unique: true,
      },
    },
    {
      timestamps: true,
      paranoid: true,
      freezeTableName: true,
    },
  );

  inspection_plans.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity





























    db.inspection_plans.hasMany(db.inspection_characteristics, {
      as: 'inspection_characteristics_inspection_plan',
      foreignKey: {
          name: 'inspection_planId',
      },
      constraints: false,
    });


    db.inspection_plans.hasMany(db.inspections, {
      as: 'inspections_inspection_plan',
      foreignKey: {
          name: 'inspection_planId',
      },
      constraints: false,
    });













//end loop



    db.inspection_plans.belongsTo(db.materials, {
      as: 'material',
      foreignKey: {
        name: 'materialId',
      },
      constraints: false,
    });

    db.inspection_plans.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



    db.inspection_plans.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.inspection_plans.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


    db.inspection_plans.belongsTo(db.users, {
      as: 'createdBy',
    });

    db.inspection_plans.belongsTo(db.users, {
      as: 'updatedBy',
    });
  };



  return inspection_plans;
};


