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

workflow_name: {
        type: DataTypes.TEXT,
      
      

      },

applies_to: {
        type: DataTypes.ENUM,
      
      

        values: [

"purchase_requisition",


"purchase_order",


"vendor_invoice",


"document"

        ],

      },

is_active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

      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_approval_workflow',
      foreignKey: {
          name: 'approval_workflowId',
      },
      constraints: false,
    });


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





//end loop



    db.approval_workflows.belongsTo(db.companies, {
      as: 'company',
      foreignKey: {
        name: 'companyId',
      },
      constraints: false,
    });

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




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

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



  return approval_workflows;
};


