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

entity_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"purchase_requisition",


"purchase_order",


"vendor_invoice",


"document"

        ],

      },

entity_reference: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"approved",


"rejected",


"cancelled"

        ],

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

decided_at: {
        type: DataTypes.DATE,
      
      

      },

decision_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  approvals.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.approvals.belongsTo(db.approval_workflows, {
      as: 'approval_workflow',
      foreignKey: {
        name: 'approval_workflowId',
      },
      constraints: false,
    });

    db.approvals.belongsTo(db.approval_steps, {
      as: 'approval_step',
      foreignKey: {
        name: 'approval_stepId',
      },
      constraints: false,
    });

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

    db.approvals.belongsTo(db.users, {
      as: 'assigned_to',
      foreignKey: {
        name: 'assigned_toId',
      },
      constraints: false,
    });

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




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

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



  return approvals;
};


