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

version_label: {
        type: DataTypes.TEXT,
      
      

      },

version_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"archived"

        ],

      },

effective_from: {
        type: DataTypes.DATE,
      
      

      },

effective_to: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  payer_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.payer_rules.hasMany(db.rule_requirements, {
      as: 'rule_requirements_payer_rule',
      foreignKey: {
          name: 'payer_ruleId',
      },
      constraints: false,
    });









//end loop



    db.payer_rules.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

    db.payer_rules.belongsTo(db.payers, {
      as: 'payer',
      foreignKey: {
        name: 'payerId',
      },
      constraints: false,
    });

    db.payer_rules.belongsTo(db.payer_plans, {
      as: 'payer_plan',
      foreignKey: {
        name: 'payer_planId',
      },
      constraints: false,
    });

    db.payer_rules.belongsTo(db.procedure_families, {
      as: 'procedure_family',
      foreignKey: {
        name: 'procedure_familyId',
      },
      constraints: false,
    });




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

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



  return payer_rules;
};


