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

amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"USD",


"CDF",


"EUR",


"GBP"

        ],

      },

amount_usd: {
        type: DataTypes.DECIMAL,
      
      

      },

delivery_lead_time: {
        type: DataTypes.TEXT,
      
      

      },

warranty_terms: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"received",


"evaluated",


"shortlisted",


"rejected",


"awarded"

        ],

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

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

  supplier_offers.associate = (db) => {


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





















    db.supplier_offers.hasMany(db.offer_scores, {
      as: 'offer_scores_supplier_offer',
      foreignKey: {
          name: 'supplier_offerId',
      },
      constraints: false,
    });


    db.supplier_offers.hasMany(db.awards, {
      as: 'awards_winning_offer',
      foreignKey: {
          name: 'winning_offerId',
      },
      constraints: false,
    });















//end loop



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

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

    db.supplier_offers.belongsTo(db.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });

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



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


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

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



  return supplier_offers;
};


