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,
      
      

      },

step_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"review",


"approval",


"quality_review",


"regulatory_review"

        ],

      },

assignment_rule: {
        type: DataTypes.ENUM,
      
      

        values: [

"specific_user",


"document_owner",


"approver_role",


"department",


"any_approver"

        ],

      },

sla_days: {
        type: DataTypes.INTEGER,
      
      

      },

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.approval_tasks, {
      as: 'approval_tasks_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.users, {
      as: 'assignee',
      foreignKey: {
        name: 'assigneeId',
      },
      constraints: false,
    });




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

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



  return approval_steps;
};


