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

request_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"sakit",


"izin",


"dinas",


"koreksi_absen"

        ],

      },

start_at: {
        type: DataTypes.DATE,
      
      

      },

end_at: {
        type: DataTypes.DATE,
      
      

      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

approval_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"rejected",


"cancelled"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

decided_at: {
        type: DataTypes.DATE,
      
      

      },

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

  attendance_requests.associate = (db) => {


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


























//end loop



    db.attendance_requests.belongsTo(db.employees, {
      as: 'employee',
      foreignKey: {
        name: 'employeeId',
      },
      constraints: false,
    });

    db.attendance_requests.belongsTo(db.outlets, {
      as: 'outlet',
      foreignKey: {
        name: 'outletId',
      },
      constraints: false,
    });

    db.attendance_requests.belongsTo(db.users, {
      as: 'decided_by_user',
      foreignKey: {
        name: 'decided_by_userId',
      },
      constraints: false,
    });

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



    db.attendance_requests.hasMany(db.file, {
      as: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.attendance_requests.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return attendance_requests;
};


