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,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

min_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

max_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

requires_comment: {
        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_step',
      foreignKey: {
          name: 'stepId',
      },
      constraints: false,
    });










































//end loop



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

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

    db.approval_steps.belongsTo(db.departments, {
      as: 'approver_department',
      foreignKey: {
        name: 'approver_departmentId',
      },
      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;
};


