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

subject_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"document",


"document_version",


"change_request",


"training_assignment",


"record_batch",


"risk_assessment",


"capa",


"nonconformance",


"audit"

        ],

      },

subject_reference: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"in_progress",


"approved",


"rejected",


"cancelled"

        ],

      },

assigned_at: {
        type: DataTypes.DATE,
      
      

      },

completed_at: {
        type: DataTypes.DATE,
      
      

      },

decision_comment: {
        type: DataTypes.TEXT,
      
      

      },

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

  approval_tasks.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.approval_tasks.belongsTo(db.approval_workflows, {
      as: 'workflow',
      foreignKey: {
        name: 'workflowId',
      },
      constraints: false,
    });

    db.approval_tasks.belongsTo(db.approval_steps, {
      as: 'step',
      foreignKey: {
        name: 'stepId',
      },
      constraints: false,
    });

    db.approval_tasks.belongsTo(db.users, {
      as: 'assignee',
      foreignKey: {
        name: 'assigneeId',
      },
      constraints: false,
    });



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


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

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



  return approval_tasks;
};


