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

      country: {
        type: DataTypes.TEXT,
      },

      pay_period_start: {
        type: DataTypes.DATE,
      },

      pay_period_end: {
        type: DataTypes.DATE,
      },

      pay_date: {
        type: DataTypes.DATE,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['Open', 'Processed', 'Amended'],
      },

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

  payroll_runs.associate = (db) => {
    /// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity

    db.payroll_runs.hasMany(db.deductions, {
      as: 'deductions_payroll_run',
      foreignKey: {
        name: 'payroll_runId',
      },
      constraints: false,
    });

    db.payroll_runs.hasMany(db.earnings, {
      as: 'earnings_payroll_run',
      foreignKey: {
        name: 'payroll_runId',
      },
      constraints: false,
    });

    db.payroll_runs.hasMany(db.taxes, {
      as: 'taxes_payroll_run',
      foreignKey: {
        name: 'payroll_runId',
      },
      constraints: false,
    });

    //end loop

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

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

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

  return payroll_runs;
};
