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

loanType: {
        type: DataTypes.ENUM,
      
      

        values: [

"SSS",


"PAG_IBIG",


"COMPANY",


"SALARY"

        ],

      },

loanSource: {
        type: DataTypes.TEXT,
      
      

      },

loanAmount: {
        type: DataTypes.DECIMAL,
      
      

      },

approvalDate: {
        type: DataTypes.DATE,
      
      

      },

firstDeductionDate: {
        type: DataTypes.DATE,
      
      

      },

monthlyAmortization: {
        type: DataTypes.DECIMAL,
      
      

      },

remainingBalance: {
        type: DataTypes.DECIMAL,
      
      

      },

termsMonths: {
        type: DataTypes.INTEGER,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"ACTIVE",


"FULLY_PAID",


"CANCELLED",


"DEFERRED"

        ],

      },

loanAccountNumber: {
        type: DataTypes.TEXT,
      
      

      },

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

  employee_loans.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity






















    db.employee_loans.hasMany(db.loan_deductions, {
      as: 'loan_deductions_loan',
      foreignKey: {
          name: 'loanId',
      },
      constraints: false,
    });
























//end loop



    db.employee_loans.belongsTo(db.employees, {
      as: 'employee',
      foreignKey: {
        name: 'employeeId',
      },
      constraints: false,
    });




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

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



  return employee_loans;
};


