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

first_name: {
        type: DataTypes.TEXT,
      
      

      },

middle_name: {
        type: DataTypes.TEXT,
      
      

      },

last_name: {
        type: DataTypes.TEXT,
      
      

      },

national_id: {
        type: DataTypes.TEXT,
      
      

      },

birth_date: {
        type: DataTypes.DATE,
      
      

      },

nationality: {
        type: DataTypes.TEXT,
      
      

      },

email: {
        type: DataTypes.TEXT,
      
      

      },

phone: {
        type: DataTypes.TEXT,
      
      

      },

phone2: {
        type: DataTypes.TEXT,
      
      

      },

permanent_address: {
        type: DataTypes.TEXT,
      
      

      },

current_address: {
        type: DataTypes.TEXT,
      
      

      },

emergency_contact: {
        type: DataTypes.TEXT,
      
      

      },

emergency_phone: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"ACTIVE",


"INACTIVE",


"SUSPENDED",


"TERMINATED"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

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

      },

deleted_at: {
        type: DataTypes.DATE,
      
      

      },

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


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


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



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


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














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





    db.employees.hasMany(db.trips, {
      as: 'trips_driver',
      foreignKey: {
          name: 'driverId',
      },
      constraints: false,
    });







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






//end loop



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




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

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



  return employees;
};


