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

requisition_number: {
        type: DataTypes.TEXT,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

scope_of_work: {
        type: DataTypes.TEXT,
      
      

      },

procurement_method: {
        type: DataTypes.ENUM,
      
      

        values: [

"open_tender",


"restricted_tender",


"rfq",


"direct_award",


"consultation"

        ],

      },

estimated_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

needed_by_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"under_review",


"approved",


"rejected",


"cancelled",


"in_tender",


"awarded",


"converted_to_contract"

        ],

      },

submitted_at: {
        type: DataTypes.DATE,
      
      

      },

rejection_reason: {
        type: DataTypes.TEXT,
      
      

      },

      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.tenders, {
      as: 'tenders_requisition',
      foreignKey: {
          name: 'requisitionId',
      },
      constraints: false,
    });































//end loop



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

    db.requisitions.belongsTo(db.procurement_plans, {
      as: 'procurement_plan',
      foreignKey: {
        name: 'procurement_planId',
      },
      constraints: false,
    });

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

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

    db.requisitions.belongsTo(db.provinces, {
      as: 'province',
      foreignKey: {
        name: 'provinceId',
      },
      constraints: false,
    });

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




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

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



  return requisitions;
};


