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

name: {
        type: DataTypes.TEXT,
      
      

      },

module: {
        type: DataTypes.ENUM,
      
      

        values: [

"budget",


"procurement",


"contract",


"project",


"grant",


"finance",


"vendor",


"document"

        ],

      },

record_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"requisitions",


"tenders",


"awards",


"contracts",


"contract_amendments",


"projects",


"grants",


"invoices",


"payment_requests",


"vendors",


"budget_reallocations"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"active",


"inactive"

        ],

      },

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

  approval_workflows.associate = (db) => {


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










    db.approval_workflows.hasMany(db.approval_steps, {
      as: 'approval_steps_workflow',
      foreignKey: {
          name: 'workflowId',
      },
      constraints: false,
    });


    db.approval_workflows.hasMany(db.approvals, {
      as: 'approvals_workflow',
      foreignKey: {
          name: 'workflowId',
      },
      constraints: false,
    });










































//end loop



    db.approval_workflows.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return approval_workflows;
};


