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

name: {
        type: DataTypes.TEXT,
      
      

      },

period_start: {
        type: DataTypes.DATE,
      
      

      },

period_end: {
        type: DataTypes.DATE,
      
      

      },

standard_workdays: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"generated",


"hr_approved",


"finance_approved",


"paid",


"archived"

        ],

      },

generated_at: {
        type: DataTypes.DATE,
      
      

      },

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

  payroll_periods.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_periods.hasMany(db.payroll_items, {
      as: 'payroll_items_payroll_period',
      foreignKey: {
          name: 'payroll_periodId',
      },
      constraints: false,
    });







//end loop



    db.payroll_periods.belongsTo(db.outlets, {
      as: 'outlet',
      foreignKey: {
        name: 'outletId',
      },
      constraints: false,
    });

    db.payroll_periods.belongsTo(db.users, {
      as: 'generated_by_user',
      foreignKey: {
        name: 'generated_by_userId',
      },
      constraints: false,
    });

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




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

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



  return payroll_periods;
};


