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

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

category: {
        type: DataTypes.ENUM,
      
      

        values: [

"missing_approval",


"missing_timestamp",


"no_hr_trigger",


"no_group_removal_proof",


"mfa_gap",


"log_retention_gap",


"manual_process",


"unclear_source_of_truth",


"other"

        ],

      },

due_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"in_progress",


"blocked",


"completed",


"accepted_risk"

        ],

      },

remediation_plan: {
        type: DataTypes.TEXT,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  remediation_items.associate = (db) => {


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

























//end loop



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

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

    db.remediation_items.belongsTo(db.control_requirements, {
      as: 'linked_control_requirement',
      foreignKey: {
        name: 'linked_control_requirementId',
      },
      constraints: false,
    });

    db.remediation_items.belongsTo(db.artifacts, {
      as: 'linked_artifact',
      foreignKey: {
        name: 'linked_artifactId',
      },
      constraints: false,
    });

    db.remediation_items.belongsTo(db.proof_packets, {
      as: 'linked_packet',
      foreignKey: {
        name: 'linked_packetId',
      },
      constraints: false,
    });

    db.remediation_items.belongsTo(db.users, {
      as: 'owner_user',
      foreignKey: {
        name: 'owner_userId',
      },
      constraints: false,
    });




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

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



  return remediation_items;
};


