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,
      },

name: {
        type: DataTypes.TEXT,
      
      

      },

inspection_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"incoming",


"in_process",


"final"

        ],

      },

sampling_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"100_percent",


"aql",


"fixed_n",


"c_equals_0"

        ],

      },

sample_size: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"inactive"

        ],

      },

notes: {
        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_characteristics, {
      as: 'qa_characteristics_inspection_plan',
      foreignKey: {
          name: 'inspection_planId',
      },
      constraints: false,
    });


    db.qa_inspection_plans.hasMany(db.qa_inspections, {
      as: 'qa_inspections_inspection_plan',
      foreignKey: {
          name: 'inspection_planId',
      },
      constraints: false,
    });








//end loop



    db.qa_inspection_plans.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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

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




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

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



  return qa_inspection_plans;
};


