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

      title: {
        type: DataTypes.TEXT,
      },

      employment_type: {
        type: DataTypes.ENUM,

        values: ['Full-time', 'Part-time', 'Contractor'],
      },

      openings: {
        type: DataTypes.INTEGER,
      },

      status: {
        type: DataTypes.ENUM,

        values: ['Open', 'Closed', 'OnHold'],
      },

      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.applications, {
      as: 'applications_requisition',
      foreignKey: {
        name: 'requisitionId',
      },
      constraints: false,
    });

    //end loop

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

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

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

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

  return job_requisitions;
};
