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

employee_code: {
        type: DataTypes.TEXT,
      
      

      },

full_name: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

nik: {
        type: DataTypes.TEXT,
      
      

      },

npwp: {
        type: DataTypes.TEXT,
      
      

      },

bank_name: {
        type: DataTypes.TEXT,
      
      

      },

bank_account_number: {
        type: DataTypes.TEXT,
      
      

      },

bank_account_name: {
        type: DataTypes.TEXT,
      
      

      },

employment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"probation",


"contract",


"permanent",


"resigned"

        ],

      },

join_date: {
        type: DataTypes.DATE,
      
      

      },

resign_date: {
        type: DataTypes.DATE,
      
      

      },

base_salary_monthly: {
        type: DataTypes.DECIMAL,
      
      

      },

position_allowance_fixed: {
        type: DataTypes.DECIMAL,
      
      

      },

meal_allowance_daily: {
        type: DataTypes.DECIMAL,
      
      

      },

transport_allowance_daily: {
        type: DataTypes.DECIMAL,
      
      

      },

prorate_position_allowance_under_20_days: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  employees.associate = (db) => {


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









    db.employees.hasMany(db.employee_contracts, {
      as: 'employee_contracts_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });


    db.employees.hasMany(db.attendance_logs, {
      as: 'attendance_logs_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });


    db.employees.hasMany(db.attendance_requests, {
      as: 'attendance_requests_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });


    db.employees.hasMany(db.overtime_logs, {
      as: 'overtime_logs_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });


    db.employees.hasMany(db.disciplinary_actions, {
      as: 'disciplinary_actions_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });



    db.employees.hasMany(db.kpi_scores, {
      as: 'kpi_scores_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });




    db.employees.hasMany(db.loan_accounts, {
      as: 'loan_accounts_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });



    db.employees.hasMany(db.payroll_items, {
      as: 'payroll_items_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });







//end loop



    db.employees.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      constraints: false,
    });

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

    db.employees.belongsTo(db.job_positions, {
      as: 'job_position',
      foreignKey: {
        name: 'job_positionId',
      },
      constraints: false,
    });

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




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

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



  return employees;
};


