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

risk_file_name: {
        type: DataTypes.TEXT,
      
      

      },

risk_file_code: {
        type: DataTypes.TEXT,
      
      

      },

methodology: {
        type: DataTypes.ENUM,
      
      

        values: [

"iso_14971",


"fmea",


"fta",


"hazop",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"in_review",


"approved",


"effective",


"superseded",


"archived"

        ],

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

review_due_at: {
        type: DataTypes.DATE,
      
      

      },

scope: {
        type: DataTypes.TEXT,
      
      

      },

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

  risk_assessments.associate = (db) => {


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
























    db.risk_assessments.hasMany(db.risk_items, {
      as: 'risk_items_risk_assessment',
      foreignKey: {
          name: 'risk_assessmentId',
      },
      constraints: false,
    });











//end loop



    db.risk_assessments.belongsTo(db.products, {
      as: 'product',
      foreignKey: {
        name: 'productId',
      },
      constraints: false,
    });

    db.risk_assessments.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });



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


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

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



  return risk_assessments;
};


