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

employeeCode: {
        type: DataTypes.TEXT,
      
      

      },

firstName: {
        type: DataTypes.TEXT,
      
      

      },

middleName: {
        type: DataTypes.TEXT,
      
      

      },

lastName: {
        type: DataTypes.TEXT,
      
      

      },

nameExtension: {
        type: DataTypes.TEXT,
      
      

      },

dateOfBirth: {
        type: DataTypes.DATE,
      
      

      },

placeOfBirth: {
        type: DataTypes.TEXT,
      
      

      },

sex: {
        type: DataTypes.ENUM,
      
      

        values: [

"MALE",


"FEMALE"

        ],

      },

civilStatus: {
        type: DataTypes.ENUM,
      
      

        values: [

"SINGLE",


"MARRIED",


"WIDOWED",


"SEPARATED",


"LEGALLY_SEPARATED"

        ],

      },

nationality: {
        type: DataTypes.TEXT,
      
      

      },

religion: {
        type: DataTypes.TEXT,
      
      

      },

bloodType: {
        type: DataTypes.TEXT,
      
      

      },

homeStreet: {
        type: DataTypes.TEXT,
      
      

      },

homeBarangay: {
        type: DataTypes.TEXT,
      
      

      },

homeCity: {
        type: DataTypes.TEXT,
      
      

      },

homeProvince: {
        type: DataTypes.TEXT,
      
      

      },

homeZipCode: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

permanentStreet: {
        type: DataTypes.TEXT,
      
      

      },

permanentBarangay: {
        type: DataTypes.TEXT,
      
      

      },

permanentCity: {
        type: DataTypes.TEXT,
      
      

      },

permanentProvince: {
        type: DataTypes.TEXT,
      
      

      },

permanentZipCode: {
        type: DataTypes.TEXT,
      
      

      },

personalEmail: {
        type: DataTypes.TEXT,
      
      

      },

personalMobile: {
        type: DataTypes.TEXT,
      
      

      },

emergencyContactName: {
        type: DataTypes.TEXT,
      
      

      },

emergencyContactRel: {
        type: DataTypes.TEXT,
      
      

      },

emergencyContactMobile: {
        type: DataTypes.TEXT,
      
      

      },

sssNumber: {
        type: DataTypes.TEXT,
      
      

      },

philhealthNumber: {
        type: DataTypes.TEXT,
      
      

      },

pagibigNumber: {
        type: DataTypes.TEXT,
      
      

      },

tinNumber: {
        type: DataTypes.TEXT,
      
      

      },

bankName: {
        type: DataTypes.TEXT,
      
      

      },

bankAccountNumber: {
        type: DataTypes.TEXT,
      
      

      },

bankAccountName: {
        type: DataTypes.TEXT,
      
      

      },

employmentDate: {
        type: DataTypes.DATE,
      
      

      },

regularizationDate: {
        type: DataTypes.DATE,
      
      

      },

employmentStatus: {
        type: DataTypes.ENUM,
      
      

        values: [

"PROBATIONARY",


"REGULAR",


"CONTRACTUAL",


"PROJECT_BASED"

        ],

      },

employmentType: {
        type: DataTypes.ENUM,
      
      

        values: [

"FULL_TIME",


"PART_TIME"

        ],

      },

jobLevel: {
        type: DataTypes.ENUM,
      
      

        values: [

"RANK_AND_FILE",


"SUPERVISOR",


"MANAGER",


"OFFICER"

        ],

      },

salaryType: {
        type: DataTypes.ENUM,
      
      

        values: [

"MONTHLY",


"DAILY",


"HOURLY"

        ],

      },

basicSalary: {
        type: DataTypes.DECIMAL,
      
      

      },

taxStatus: {
        type: DataTypes.ENUM,
      
      

        values: [

"S",


"ME",


"S1",


"S2",


"S3",


"S4",


"ME1",


"ME2",


"ME3",


"ME4"

        ],

      },

workLocation: {
        type: DataTypes.TEXT,
      
      

      },

separationDate: {
        type: DataTypes.DATE,
      
      

      },

separationType: {
        type: DataTypes.ENUM,
      
      

        values: [

"RESIGNED",


"TERMINATED",


"RETIRED",


"REDUNDANCY",


"AWOL",


"DEATH"

        ],

      },

separationReason: {
        type: DataTypes.TEXT,
      
      

      },

clearanceStatus: {
        type: DataTypes.TEXT,
      
      

      },

finalPayAmount: {
        type: DataTypes.DECIMAL,
      
      

      },

finalPayReleasedDate: {
        type: DataTypes.DATE,
      
      

      },

isActive: {
        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_documents, {
      as: 'employee_documents_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });


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




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


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


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




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














    db.employees.hasMany(db.applicants, {
      as: 'applicants_convertedEmployee',
      foreignKey: {
          name: 'convertedEmployeeId',
      },
      constraints: false,
    });









//end loop



    db.employees.belongsTo(db.departments, {
      as: 'department',
      foreignKey: {
        name: 'departmentId',
      },
      constraints: false,
    });

    db.employees.belongsTo(db.positions, {
      as: 'position',
      foreignKey: {
        name: 'positionId',
      },
      constraints: false,
    });

    db.employees.belongsTo(db.shifts, {
      as: 'shift',
      foreignKey: {
        name: 'shiftId',
      },
      constraints: false,
    });

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



    db.employees.hasMany(db.file, {
      as: 'profilePhoto',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.employees.getTableName(),
        belongsToColumn: 'profilePhoto',
      },
    });


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

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



  return employees;
};


