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

standard_name: {
        type: DataTypes.TEXT,
      
      

      },

standard_code: {
        type: DataTypes.TEXT,
      
      

      },

inspection_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"incoming",


"in_process",


"final",


"audit"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"inactive"

        ],

      },

requirements: {
        type: DataTypes.TEXT,
      
      

      },

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

  quality_standards.associate = (db) => {


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





























    db.quality_standards.hasMany(db.inspection_plans, {
      as: 'inspection_plans_quality_standard',
      foreignKey: {
          name: 'quality_standardId',
      },
      constraints: false,
    });








//end loop



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

    db.quality_standards.belongsTo(db.material_revisions, {
      as: 'revision',
      foreignKey: {
        name: 'revisionId',
      },
      constraints: false,
    });



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


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

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



  return quality_standards;
};


