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 qa_inspection_plans = sequelize.define(
    'qa_inspection_plans',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

plan_code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

inspection_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"receiving",


"in_process",


"final",


"audit"

        ],

      },

sampling_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"100_percent",


"aql",


"custom"

        ],

      },

aql_level: {
        type: DataTypes.DECIMAL,
      
      

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

instructions: {
        type: DataTypes.TEXT,
      
      

      },

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

  qa_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.qa_inspection_plans.hasMany(db.qa_inspections, {
      as: 'qa_inspections_plan',
      foreignKey: {
          name: 'planId',
      },
      constraints: false,
    });



















//end loop



    db.qa_inspection_plans.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });



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


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

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



  return qa_inspection_plans;
};


