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

requestType: {
        type: DataTypes.ENUM,
      
      

        values: [

"AP_PAYMENT",


"SALARY_CHANGE",


"JOURNAL_ENTRY",


"EXPORT_REQUEST",


"PAYROLL_POST",


"LOAN_APPROVAL"

        ],

      },

entityId: {
        type: DataTypes.TEXT,
      
      

      },

entityType: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"PENDING",


"APPROVED",


"REJECTED",


"CANCELLED",


"EXPIRED"

        ],

      },

requestedAt: {
        type: DataTypes.DATE,
      
      

      },

approvedAt: {
        type: DataTypes.DATE,
      
      

      },

rejectedAt: {
        type: DataTypes.DATE,
      
      

      },

rejectionReason: {
        type: DataTypes.TEXT,
      
      

      },

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  approval_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.approval_requests.belongsTo(db.users, {
      as: 'requestedByUser',
      foreignKey: {
        name: 'requestedByUserId',
      },
      constraints: false,
    });

    db.approval_requests.belongsTo(db.users, {
      as: 'approvedByUser',
      foreignKey: {
        name: 'approvedByUserId',
      },
      constraints: false,
    });

    db.approval_requests.belongsTo(db.users, {
      as: 'rejectedByUser',
      foreignKey: {
        name: 'rejectedByUserId',
      },
      constraints: false,
    });




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

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



  return approval_requests;
};


