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

framework_name: {
        type: DataTypes.TEXT,
      
      

      },

version: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        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_frameworks.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_frameworks.hasMany(db.control_requirements, {
      as: 'control_requirements_framework',
      foreignKey: {
          name: 'frameworkId',
      },
      constraints: false,
    });






    db.control_frameworks.hasMany(db.proof_packets, {
      as: 'proof_packets_framework',
      foreignKey: {
          name: 'frameworkId',
      },
      constraints: false,
    });












//end loop



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




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

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



  return control_frameworks;
};


