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

classification: {
        type: DataTypes.ENUM,
      
      

        values: [

"direct_purchase",


"pre_qualified",


"multiple_quotations",


"public_tender",


"single_source"

        ],

      },

classification_justification: {
        type: DataTypes.TEXT,
      
      

      },

pipeline_stage: {
        type: DataTypes.ENUM,
      
      

        values: [

"a_classifier",


"sourcing",


"analyse_offres",


"attribuee",


"cloturee"

        ],

      },

started_at: {
        type: DataTypes.DATE,
      
      

      },

sla_due_at: {
        type: DataTypes.DATE,
      
      

      },

awarded_at: {
        type: DataTypes.DATE,
      
      

      },

closed_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

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

  procurement_cases.associate = (db) => {


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


















    db.procurement_cases.hasMany(db.tender_events, {
      as: 'tender_events_procurement_case',
      foreignKey: {
          name: 'procurement_caseId',
      },
      constraints: false,
    });


    db.procurement_cases.hasMany(db.supplier_offers, {
      as: 'supplier_offers_procurement_case',
      foreignKey: {
          name: 'procurement_caseId',
      },
      constraints: false,
    });


    db.procurement_cases.hasMany(db.bid_criteria, {
      as: 'bid_criteria_procurement_case',
      foreignKey: {
          name: 'procurement_caseId',
      },
      constraints: false,
    });



    db.procurement_cases.hasMany(db.awards, {
      as: 'awards_procurement_case',
      foreignKey: {
          name: 'procurement_caseId',
      },
      constraints: false,
    });















//end loop



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

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

    db.procurement_cases.belongsTo(db.users, {
      as: 'owner',
      foreignKey: {
        name: 'ownerId',
      },
      constraints: false,
    });

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




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

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



  return procurement_cases;
};


