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

request_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"expense",


"invoice_void",


"credit_note",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"approved",


"rejected",


"cancelled"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

decided_at: {
        type: DataTypes.DATE,
      
      

      },

subject: {
        type: DataTypes.TEXT,
      
      

      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

decision_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.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });

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

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



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


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

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



  return approval_requests;
};


