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

exception_name: {
        type: DataTypes.TEXT,
      
      

      },

exception_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"break_glass",


"emergency_access",


"policy_exception",


"temporary_privilege",


"other"

        ],

      },

subject: {
        type: DataTypes.TEXT,
      
      

      },

reason: {
        type: DataTypes.TEXT,
      
      

      },

granted_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

revoked_at: {
        type: DataTypes.DATE,
      
      

      },

followup_review_required: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"active",


"expired",


"revoked",


"closed"

        ],

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  exceptions.associate = (db) => {

    db.exceptions.belongsToMany(db.artifacts, {
      as: 'linked_artifacts',
      foreignKey: {
        name: 'exceptions_linked_artifactsId',
      },
      constraints: false,
      through: 'exceptionsLinked_artifactsArtifacts',
    });

    db.exceptions.belongsToMany(db.artifacts, {
      as: 'linked_artifacts_filter',
      foreignKey: {
        name: 'exceptions_linked_artifactsId',
      },
      constraints: false,
      through: 'exceptionsLinked_artifactsArtifacts',
    });


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

























//end loop



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

    db.exceptions.belongsTo(db.users, {
      as: 'approver_user',
      foreignKey: {
        name: 'approver_userId',
      },
      constraints: false,
    });




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

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



  return exceptions;
};


