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

name: {
        type: DataTypes.TEXT,
      
      

      },

payer_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"commercial",


"medicare",


"medicaid",


"workers_comp",


"other"

        ],

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

website_url: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  payers.associate = (db) => {


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









    db.payers.hasMany(db.payer_plans, {
      as: 'payer_plans_payer',
      foreignKey: {
          name: 'payerId',
      },
      constraints: false,
    });













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








    db.payers.hasMany(db.contacts, {
      as: 'contacts_payer',
      foreignKey: {
          name: 'payerId',
      },
      constraints: false,
    });



//end loop



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




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

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



  return payers;
};


