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_number: {
        type: DataTypes.TEXT,
      
      

      },

first_name: {
        type: DataTypes.TEXT,
      
      

      },

last_name: {
        type: DataTypes.TEXT,
      
      

      },

preferred_name: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

national_id: {
        type: DataTypes.TEXT,
      
      

      },

date_of_birth: {
        type: DataTypes.DATE,
      
      

      },

gender: {
        type: DataTypes.ENUM,
      
      

        values: [

"male",


"female",


"non_binary",


"prefer_not_to_say"

        ],

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

hire_date: {
        type: DataTypes.DATE,
      
      

      },

termination_date: {
        type: DataTypes.DATE,
      
      

      },

employment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"probation",


"on_leave",


"terminated"

        ],

      },

employment_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"full_time",


"part_time",


"contractor",


"intern"

        ],

      },

      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.employment_contracts, {
      as: 'employment_contracts_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });



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

    db.employees.hasMany(db.leave_requests, {
      as: 'leave_requests_approver',
      foreignKey: {
          name: 'approverId',
      },
      constraints: false,
    });




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


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

    db.employees.hasMany(db.time_entries, {
      as: 'time_entries_approved_by',
      foreignKey: {
          name: 'approved_byId',
      },
      constraints: false,
    });




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

    db.employees.hasMany(db.expense_claims, {
      as: 'expense_claims_approver',
      foreignKey: {
          name: 'approverId',
      },
      constraints: false,
    });


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






//end loop



    db.employees.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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

    db.employees.belongsTo(db.job_titles, {
      as: 'job_title',
      foreignKey: {
        name: 'job_titleId',
      },
      constraints: false,
    });

    db.employees.belongsTo(db.work_locations, {
      as: 'work_location',
      foreignKey: {
        name: 'work_locationId',
      },
      constraints: false,
    });

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

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

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



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


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

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



  return employees;
};


