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

req_number: {
        type: DataTypes.TEXT,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

quantity: {
        type: DataTypes.INTEGER,
      
      

      },

estimated_budget_usd: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"CDF",


"EUR",


"GBP"

        ],

      },

estimated_budget_original: {
        type: DataTypes.DECIMAL,
      
      

      },

fx_rate_to_usd: {
        type: DataTypes.DECIMAL,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"in_approval",


"approved",


"rejected",


"in_procurement",


"awarded",


"in_delivery",


"received",


"closed",


"returned"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

approved_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

return_reason: {
        type: DataTypes.TEXT,
      
      

      },

reject_reason: {
        type: DataTypes.TEXT,
      
      

      },

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

      },

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

  requisitions.associate = (db) => {


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













    db.requisitions.hasMany(db.approval_steps, {
      as: 'approval_steps_requisition',
      foreignKey: {
          name: 'requisitionId',
      },
      constraints: false,
    });


    db.requisitions.hasMany(db.procurement_cases, {
      as: 'procurement_cases_requisition',
      foreignKey: {
          name: 'requisitionId',
      },
      constraints: false,
    });










    db.requisitions.hasMany(db.purchase_orders, {
      as: 'purchase_orders_requisition',
      foreignKey: {
          name: 'requisitionId',
      },
      constraints: false,
    });














//end loop



    db.requisitions.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.requisitions.belongsTo(db.users, {
      as: 'requester',
      foreignKey: {
        name: 'requesterId',
      },
      constraints: false,
    });

    db.requisitions.belongsTo(db.departments, {
      as: 'department',
      foreignKey: {
        name: 'departmentId',
      },
      constraints: false,
    });

    db.requisitions.belongsTo(db.projects, {
      as: 'project',
      foreignKey: {
        name: 'projectId',
      },
      constraints: false,
    });

    db.requisitions.belongsTo(db.budget_lines, {
      as: 'budget_line',
      foreignKey: {
        name: 'budget_lineId',
      },
      constraints: false,
    });

    db.requisitions.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return requisitions;
};


