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,
      },

periodFrom: {
        type: DataTypes.DATE,
      
      

      },

periodTo: {
        type: DataTypes.DATE,
      
      

      },

payrollType: {
        type: DataTypes.ENUM,
      
      

        values: [

"SEMI_MONTHLY_1",


"SEMI_MONTHLY_2",


"THIRTEENTH_MONTH",


"FINAL_PAY",


"ADJUSTMENT"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"DRAFT",


"COMPUTED",


"APPROVED",


"POSTED",


"VOIDED"

        ],

      },

lockedAt: {
        type: DataTypes.DATE,
      
      

      },

computedAt: {
        type: DataTypes.DATE,
      
      

      },

approvedAt: {
        type: DataTypes.DATE,
      
      

      },

postedAt: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

      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.time_records, {
      as: 'time_records_payrollRun',
      foreignKey: {
          name: 'payrollRunId',
      },
      constraints: false,
    });









    db.payroll_runs.hasMany(db.payroll_items, {
      as: 'payroll_items_payrollRun',
      foreignKey: {
          name: 'payrollRunId',
      },
      constraints: false,
    });


    db.payroll_runs.hasMany(db.bank_files, {
      as: 'bank_files_payrollRun',
      foreignKey: {
          name: 'payrollRunId',
      },
      constraints: false,
    });



    db.payroll_runs.hasMany(db.journal_entries, {
      as: 'journal_entries_payrollRun',
      foreignKey: {
          name: 'payrollRunId',
      },
      constraints: false,
    });



















//end loop



    db.payroll_runs.belongsTo(db.users, {
      as: 'approvedByUser',
      foreignKey: {
        name: 'approvedByUserId',
      },
      constraints: false,
    });

    db.payroll_runs.belongsTo(db.users, {
      as: 'postedByUser',
      foreignKey: {
        name: 'postedByUserId',
      },
      constraints: false,
    });

    db.payroll_runs.belongsTo(db.journal_entries, {
      as: 'journalEntry',
      foreignKey: {
        name: 'journalEntryId',
      },
      constraints: false,
    });




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

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



  return payroll_runs;
};


