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

finding_number: {
        type: DataTypes.TEXT,
      
      

      },

severity: {
        type: DataTypes.ENUM,
      
      

        values: [

"observation",


"minor",


"major",


"critical"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"open",


"action_required",


"in_progress",


"verified",


"closed"

        ],

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

objective_evidence: {
        type: DataTypes.TEXT,
      
      

      },

due_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

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

  audit_findings.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.audit_findings.belongsTo(db.audits, {
      as: 'audit',
      foreignKey: {
        name: 'auditId',
      },
      constraints: false,
    });

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

    db.audit_findings.belongsTo(db.capas, {
      as: 'related_capa',
      foreignKey: {
        name: 'related_capaId',
      },
      constraints: false,
    });



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


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

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



  return audit_findings;
};


