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,
      
      

      },

applies_to: {
        type: DataTypes.ENUM,
      
      

        values: [

"sales_order",


"purchase_order",


"invoice",


"vendor_bill",


"journal_entry",


"return",


"inventory_transfer"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"archived"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

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

  approval_workflows.associate = (db) => {

    db.approval_workflows.belongsToMany(db.approval_steps, {
      as: 'steps',
      foreignKey: {
        name: 'approval_workflows_stepsId',
      },
      constraints: false,
      through: 'approval_workflowsStepsApproval_steps',
    });

    db.approval_workflows.belongsToMany(db.approval_steps, {
      as: 'steps_filter',
      foreignKey: {
        name: 'approval_workflows_stepsId',
      },
      constraints: false,
      through: 'approval_workflowsStepsApproval_steps',
    });


/// 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.approval_requests, {
      as: 'approval_requests_workflow',
      foreignKey: {
          name: 'workflowId',
      },
      constraints: false,
    });














































//end loop



    db.approval_workflows.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      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;
};


