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

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

  employees.associate = (db) => {
    db.employees.belongsToMany(db.employments, {
      as: 'employments',
      foreignKey: {
        name: 'employees_employmentsId',
      },
      constraints: false,
      through: 'employeesEmploymentsEmployments',
    });

    db.employees.belongsToMany(db.employments, {
      as: 'employments_filter',
      foreignKey: {
        name: 'employees_employmentsId',
      },
      constraints: false,
      through: 'employeesEmploymentsEmployments',
    });

    db.employees.belongsToMany(db.positions, {
      as: 'positions',
      foreignKey: {
        name: 'employees_positionsId',
      },
      constraints: false,
      through: 'employeesPositionsPositions',
    });

    db.employees.belongsToMany(db.positions, {
      as: 'positions_filter',
      foreignKey: {
        name: 'employees_positionsId',
      },
      constraints: false,
      through: 'employeesPositionsPositions',
    });

    db.employees.belongsToMany(db.compensations, {
      as: 'compensations',
      foreignKey: {
        name: 'employees_compensationsId',
      },
      constraints: false,
      through: 'employeesCompensationsCompensations',
    });

    db.employees.belongsToMany(db.compensations, {
      as: 'compensations_filter',
      foreignKey: {
        name: 'employees_compensationsId',
      },
      constraints: false,
      through: 'employeesCompensationsCompensations',
    });

    db.employees.belongsToMany(db.documents, {
      as: 'documents',
      foreignKey: {
        name: 'employees_documentsId',
      },
      constraints: false,
      through: 'employeesDocumentsDocuments',
    });

    db.employees.belongsToMany(db.documents, {
      as: 'documents_filter',
      foreignKey: {
        name: 'employees_documentsId',
      },
      constraints: false,
      through: 'employeesDocumentsDocuments',
    });

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

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

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

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

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

    db.employees.hasMany(db.devices, {
      as: 'devices_owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

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

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

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

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

    db.employees.hasMany(db.enrollment_learnings, {
      as: 'enrollment_learnings_assigned_by',
      foreignKey: {
        name: 'assigned_byId',
      },
      constraints: false,
    });

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

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

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

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

    db.employees.hasMany(db.interviews, {
      as: 'interviews_interviewer',
      foreignKey: {
        name: 'interviewerId',
      },
      constraints: false,
    });

    db.employees.hasMany(db.job_requisitions, {
      as: 'job_requisitions_recruiter',
      foreignKey: {
        name: 'recruiterId',
      },
      constraints: false,
    });

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

    db.employees.hasMany(db.positions, {
      as: 'positions_manager',
      foreignKey: {
        name: 'managerId',
      },
      constraints: false,
    });

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

    db.employees.hasMany(db.reviews, {
      as: 'reviews_reviewer',
      foreignKey: {
        name: 'reviewerId',
      },
      constraints: false,
    });

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

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

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

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

    //end loop

    db.employees.belongsTo(db.users, {
      as: 'user',
      foreignKey: {
        name: 'userId',
      },
      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;
};
