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

object_type: {
        type: DataTypes.TEXT,
      
      

      },

object_ref: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"approved",


"rejected",


"cancelled"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

resolved_at: {
        type: DataTypes.DATE,
      
      

      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

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

  approval_requests.associate = (db) => {

    db.approval_requests.belongsToMany(db.approval_decisions, {
      as: 'decisions',
      foreignKey: {
        name: 'approval_requests_decisionsId',
      },
      constraints: false,
      through: 'approval_requestsDecisionsApproval_decisions',
    });

    db.approval_requests.belongsToMany(db.approval_decisions, {
      as: 'decisions_filter',
      foreignKey: {
        name: 'approval_requests_decisionsId',
      },
      constraints: false,
      through: 'approval_requestsDecisionsApproval_decisions',
    });


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












    db.approval_requests.hasMany(db.approval_decisions, {
      as: 'approval_decisions_request',
      foreignKey: {
          name: 'requestId',
      },
      constraints: false,
    });













































//end loop



    db.approval_requests.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.approval_requests.belongsTo(db.approval_workflows, {
      as: 'workflow',
      foreignKey: {
        name: 'workflowId',
      },
      constraints: false,
    });

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

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




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

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



  return approval_requests;
};


