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

code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

daysPerYear: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

      },

minServiceMonths: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  leave_types.associate = (db) => {


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



















    db.leave_types.hasMany(db.leave_balances, {
      as: 'leave_balances_leaveType',
      foreignKey: {
          name: 'leaveTypeId',
      },
      constraints: false,
    });


    db.leave_types.hasMany(db.leave_applications, {
      as: 'leave_applications_leaveType',
      foreignKey: {
          name: 'leaveTypeId',
      },
      constraints: false,
    });


























//end loop






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

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



  return leave_types;
};


