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

step_order: {
        type: DataTypes.INTEGER,
      
      

      },

step_name: {
        type: DataTypes.TEXT,
      
      

      },

min_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

max_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

  approval_steps.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_steps.hasMany(db.approvals, {
      as: 'approvals_approval_step',
      foreignKey: {
          name: 'approval_stepId',
      },
      constraints: false,
    });





//end loop



    db.approval_steps.belongsTo(db.approval_workflows, {
      as: 'approval_workflow',
      foreignKey: {
        name: 'approval_workflowId',
      },
      constraints: false,
    });

    db.approval_steps.belongsTo(db.roles, {
      as: 'approver_role',
      foreignKey: {
        name: 'approver_roleId',
      },
      constraints: false,
    });

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




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

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



  return approval_steps;
};


