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

name: {
        type: DataTypes.TEXT,
      
      

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"investigating",


"contained",


"eradicated",


"recovered",


"closed"

        ],

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

summary: {
        type: DataTypes.TEXT,
      
      

      },

business_impact_score: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  incidents.associate = (db) => {

    db.incidents.belongsToMany(db.detections, {
      as: 'detections',
      foreignKey: {
        name: 'incidents_detectionsId',
      },
      constraints: false,
      through: 'incidentsDetectionsDetections',
    });

    db.incidents.belongsToMany(db.detections, {
      as: 'detections_filter',
      foreignKey: {
        name: 'incidents_detectionsId',
      },
      constraints: false,
      through: 'incidentsDetectionsDetections',
    });


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














    db.incidents.hasMany(db.response_actions, {
      as: 'response_actions_incident',
      foreignKey: {
          name: 'incidentId',
      },
      constraints: false,
    });













    db.incidents.hasMany(db.ai_recommendations, {
      as: 'ai_recommendations_incident',
      foreignKey: {
          name: 'incidentId',
      },
      constraints: false,
    });



//end loop



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

    db.incidents.belongsTo(db.users, {
      as: 'assignee',
      foreignKey: {
        name: 'assigneeId',
      },
      constraints: false,
    });




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

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



  return incidents;
};


