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

name: {
        type: DataTypes.TEXT,
      
      

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"error",


"warning"

        ],

      },

rule_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"required",


"format",


"range",


"enum_in",


"unique",


"duplicate_in_file",


"foreign_key",


"custom_expression"

        ],

      },

target_field: {
        type: DataTypes.TEXT,
      
      

      },

condition_expression: {
        type: DataTypes.TEXT,
      
      

      },

parameters: {
        type: DataTypes.TEXT,
      
      

      },

message_template: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

order_index: {
        type: DataTypes.INTEGER,
      
      

      },

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

  validation_rules.associate = (db) => {


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
















    db.validation_rules.hasMany(db.import_issues, {
      as: 'import_issues_rule',
      foreignKey: {
          name: 'ruleId',
      },
      constraints: false,
    });









//end loop



    db.validation_rules.belongsTo(db.validation_rule_sets, {
      as: 'rule_set',
      foreignKey: {
        name: 'rule_setId',
      },
      constraints: false,
    });

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




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

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



  return validation_rules;
};


