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

hazard_id: {
        type: DataTypes.TEXT,
      
      

      },

hazard_description: {
        type: DataTypes.TEXT,
      
      

      },

sequence_of_events: {
        type: DataTypes.TEXT,
      
      

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"negligible",


"minor",


"serious",


"critical",


"catastrophic"

        ],

      },

occurrence: {
        type: DataTypes.ENUM,
      
      

        values: [

"remote",


"unlikely",


"possible",


"likely",


"frequent"

        ],

      },

detectability: {
        type: DataTypes.ENUM,
      
      

        values: [

"high",


"medium",


"low",


"unknown"

        ],

      },

risk_level_initial: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"unacceptable"

        ],

      },

risk_controls: {
        type: DataTypes.TEXT,
      
      

      },

risk_level_residual: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"unacceptable"

        ],

      },

verification_of_controls: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"mitigating",


"verified",


"accepted",


"closed"

        ],

      },

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

  risk_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.risk_items.belongsTo(db.risk_assessments, {
      as: 'risk_assessment',
      foreignKey: {
        name: 'risk_assessmentId',
      },
      constraints: false,
    });



    db.risk_items.hasMany(db.file, {
      as: 'evidence_files',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.risk_items.getTableName(),
        belongsToColumn: 'evidence_files',
      },
    });


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

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



  return risk_items;
};


