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 job_requisitions = sequelize.define(
    'job_requisitions',
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },

requestingDept: {
        type: DataTypes.TEXT,
      
      

      },

positionTitle: {
        type: DataTypes.TEXT,
      
      

      },

numberOfVacancies: {
        type: DataTypes.INTEGER,
      
      

      },

justification: {
        type: DataTypes.TEXT,
      
      

      },

requiredSkills: {
        type: DataTypes.TEXT,
      
      

      },

salaryRangeFrom: {
        type: DataTypes.DECIMAL,
      
      

      },

salaryRangeTo: {
        type: DataTypes.DECIMAL,
      
      

      },

targetStartDate: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"JOB_REQUEST",


"APPROVAL",


"POSTED",


"APPLICATIONS",


"SCREENING",


"INTERVIEW",


"JOB_OFFER",


"PRE_EMPLOYMENT",


"ONBOARDING",


"ACTIVE"

        ],

      },

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

  job_requisitions.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity





































    db.job_requisitions.hasMany(db.applicants, {
      as: 'applicants_jobRequisition',
      foreignKey: {
          name: 'jobRequisitionId',
      },
      constraints: false,
    });









//end loop



    db.job_requisitions.belongsTo(db.users, {
      as: 'approvedByManagerUser',
      foreignKey: {
        name: 'approvedByManagerUserId',
      },
      constraints: false,
    });

    db.job_requisitions.belongsTo(db.users, {
      as: 'approvedByHrUser',
      foreignKey: {
        name: 'approvedByHrUserId',
      },
      constraints: false,
    });




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

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



  return job_requisitions;
};


