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

control_code: {
        type: DataTypes.TEXT,
      
      

      },

control_family: {
        type: DataTypes.TEXT,
      
      

      },

control_title: {
        type: DataTypes.TEXT,
      
      

      },

control_description: {
        type: DataTypes.TEXT,
      
      

      },

evidence_expectation_summary: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  control_requirements.associate = (db) => {


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











    db.control_requirements.hasMany(db.evidence_models, {
      as: 'evidence_models_control_requirement',
      foreignKey: {
          name: 'control_requirementId',
      },
      constraints: false,
    });





    db.control_requirements.hasMany(db.remediation_items, {
      as: 'remediation_items_linked_control_requirement',
      foreignKey: {
          name: 'linked_control_requirementId',
      },
      constraints: false,
    });











//end loop



    db.control_requirements.belongsTo(db.control_frameworks, {
      as: 'framework',
      foreignKey: {
        name: 'frameworkId',
      },
      constraints: false,
    });

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




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

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



  return control_requirements;
};


