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

workflow_name: {
        type: DataTypes.TEXT,
      
      

      },

workflow_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"joiner",


"mover",


"leaver",


"privileged_access_assignment",


"privileged_access_removal",


"mfa_enforcement",


"quarterly_access_review",


"service_account_review",


"break_glass_exception",


"emergency_access_use"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

risk_level: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

owner_role: {
        type: DataTypes.ENUM,
      
      

        values: [

"org_admin",


"compliance_manager",


"security_manager",


"iam_operator",


"auditor",


"read_only_client"

        ],

      },

source_trigger_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"HR",


"Ticket",


"Manual",


"Automated",


"Scheduled"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"deprecated"

        ],

      },

recurrence_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"none",


"weekly",


"monthly",


"quarterly",


"semi_annual",


"annual"

        ],

      },

target_sla_hours: {
        type: DataTypes.INTEGER,
      
      

      },

coverage_gap_notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  identity_workflows.associate = (db) => {

    db.identity_workflows.belongsToMany(db.connected_systems, {
      as: 'linked_systems',
      foreignKey: {
        name: 'identity_workflows_linked_systemsId',
      },
      constraints: false,
      through: 'identity_workflowsLinked_systemsConnected_systems',
    });

    db.identity_workflows.belongsToMany(db.connected_systems, {
      as: 'linked_systems_filter',
      foreignKey: {
        name: 'identity_workflows_linked_systemsId',
      },
      constraints: false,
      through: 'identity_workflowsLinked_systemsConnected_systems',
    });

    db.identity_workflows.belongsToMany(db.control_requirements, {
      as: 'linked_controls',
      foreignKey: {
        name: 'identity_workflows_linked_controlsId',
      },
      constraints: false,
      through: 'identity_workflowsLinked_controlsControl_requirements',
    });

    db.identity_workflows.belongsToMany(db.control_requirements, {
      as: 'linked_controls_filter',
      foreignKey: {
        name: 'identity_workflows_linked_controlsId',
      },
      constraints: false,
      through: 'identity_workflowsLinked_controlsControl_requirements',
    });


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity











    db.identity_workflows.hasMany(db.evidence_models, {
      as: 'evidence_models_workflow',
      foreignKey: {
          name: 'workflowId',
      },
      constraints: false,
    });


    db.identity_workflows.hasMany(db.artifacts, {
      as: 'artifacts_workflow',
      foreignKey: {
          name: 'workflowId',
      },
      constraints: false,
    });


    db.identity_workflows.hasMany(db.sampled_subjects, {
      as: 'sampled_subjects_workflow',
      foreignKey: {
          name: 'workflowId',
      },
      constraints: false,
    });


    db.identity_workflows.hasMany(db.proof_packets, {
      as: 'proof_packets_workflow',
      foreignKey: {
          name: 'workflowId',
      },
      constraints: false,
    });


    db.identity_workflows.hasMany(db.remediation_items, {
      as: 'remediation_items_linked_workflow',
      foreignKey: {
          name: 'linked_workflowId',
      },
      constraints: false,
    });











//end loop



    db.identity_workflows.belongsTo(db.organizations, {
      as: 'organization',
      foreignKey: {
        name: 'organizationId',
      },
      constraints: false,
    });




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

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



  return identity_workflows;
};


