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

name: {
        type: DataTypes.TEXT,
      
      

      },

inspection_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"incoming",


"in_process",


"final",


"audit"

        ],

      },

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

  qa_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_plans.hasMany(db.qa_characteristics, {
      as: 'qa_characteristics_qa_plan',
      foreignKey: {
          name: 'qa_planId',
      },
      constraints: false,
    });


    db.qa_plans.hasMany(db.qa_inspections, {
      as: 'qa_inspections_qa_plan',
      foreignKey: {
          name: 'qa_planId',
      },
      constraints: false,
    });












//end loop



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



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


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

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



  return qa_plans;
};


