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

review_name: {
        type: DataTypes.TEXT,
      
      

      },

review_scope: {
        type: DataTypes.TEXT,
      
      

      },

review_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"quarterly_user_access",


"privileged_access",


"app_access",


"service_account"

        ],

      },

start_date: {
        type: DataTypes.DATE,
      
      

      },

due_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"launched",


"in_progress",


"completed",


"overdue",


"archived"

        ],

      },

completion_percentage: {
        type: DataTypes.INTEGER,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  access_reviews.associate = (db) => {


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

















    db.access_reviews.hasMany(db.access_review_items, {
      as: 'access_review_items_access_review',
      foreignKey: {
          name: 'access_reviewId',
      },
      constraints: false,
    });









//end loop



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

    db.access_reviews.belongsTo(db.users, {
      as: 'reviewer_user',
      foreignKey: {
        name: 'reviewer_userId',
      },
      constraints: false,
    });




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

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



  return access_reviews;
};


