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,
      
      

      },

job_title: {
        type: DataTypes.ENUM,
      
      

        values: [

"cashier",


"sales_attendant",


"store_manager",


"procurement",


"finance",


"hr",


"security",


"cleaner",


"other"

        ],

      },

hire_date: {
        type: DataTypes.DATE,
      
      

      },

employment_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"on_leave",


"terminated"

        ],

      },

base_salary: {
        type: DataTypes.DECIMAL,
      
      

      },

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


    db.employees.hasMany(db.attendance_records, {
      as: 'attendance_records_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.performance_reviews, {
      as: 'performance_reviews_employee',
      foreignKey: {
          name: 'employeeId',
      },
      constraints: false,
    });











//end loop



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

    db.employees.belongsTo(db.branches, {
      as: 'branch',
      foreignKey: {
        name: 'branchId',
      },
      constraints: false,
    });

    db.employees.belongsTo(db.departments, {
      as: 'department',
      foreignKey: {
        name: 'departmentId',
      },
      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;
};


