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

payroll_name: {
        type: DataTypes.TEXT,
      
      

      },

period_start: {
        type: DataTypes.DATE,
      
      

      },

period_end: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"approved",


"paid",


"cancelled"

        ],

      },

paid_at: {
        type: DataTypes.DATE,
      
      

      },

total_gross: {
        type: DataTypes.DECIMAL,
      
      

      },

total_deductions: {
        type: DataTypes.DECIMAL,
      
      

      },

total_net: {
        type: DataTypes.DECIMAL,
      
      

      },

      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.payroll_items, {
      as: 'payroll_items_payroll_run',
      foreignKey: {
          name: 'payroll_runId',
      },
      constraints: false,
    });












//end loop



    db.payroll_runs.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

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


