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

      base_pay: {
        type: DataTypes.DECIMAL,
      },

      pay_type: {
        type: DataTypes.ENUM,

        values: ['Hourly', 'Salary'],
      },

      pay_frequency: {
        type: DataTypes.ENUM,

        values: ['Weekly', 'Bi-weekly', 'Monthly'],
      },

      currency: {
        type: DataTypes.TEXT,
      },

      effective_date: {
        type: DataTypes.DATE,
      },

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

  compensations.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.compensations.belongsTo(db.employees, {
      as: 'employee',
      foreignKey: {
        name: 'employeeId',
      },
      constraints: false,
    });

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

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

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

  return compensations;
};
