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

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

policy_scope: {
        type: DataTypes.ENUM,
      
      

        values: [

"tenant",


"organization",


"team",


"conference"

        ],

      },

rego_module: {
        type: DataTypes.TEXT,
      
      

      },

policy_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"disabled"

        ],

      },

effective_from: {
        type: DataTypes.DATE,
      
      

      },

effective_until: {
        type: DataTypes.DATE,
      
      

      },

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

  policy_rules.associate = (db) => {


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























































//end loop



    db.policy_rules.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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




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

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



  return policy_rules;
};


