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

change_number: {
        type: DataTypes.TEXT,
      
      

      },

change_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"document_change",


"process_change",


"form_change",


"software_change",


"other"

        ],

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"in_assessment",


"approved",


"implemented",


"verified",


"closed",


"rejected"

        ],

      },

reason_for_change: {
        type: DataTypes.TEXT,
      
      

      },

impact_assessment: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

      },

requested_at: {
        type: DataTypes.DATE,
      
      

      },

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,
    },
  );

  change_requests.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.change_requests.belongsTo(db.users, {
      as: 'requester',
      foreignKey: {
        name: 'requesterId',
      },
      constraints: false,
    });

    db.change_requests.belongsTo(db.documents, {
      as: 'document',
      foreignKey: {
        name: 'documentId',
      },
      constraints: false,
    });

    db.change_requests.belongsTo(db.users, {
      as: 'assigned_to',
      foreignKey: {
        name: 'assigned_toId',
      },
      constraints: false,
    });



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


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

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



  return change_requests;
};


