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

awarded_amount_usd: {
        type: DataTypes.DECIMAL,
      
      

      },

award_rationale: {
        type: DataTypes.TEXT,
      
      

      },

awarded_at: {
        type: DataTypes.DATE,
      
      

      },

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

  awards.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.awards.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

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

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

    db.awards.belongsTo(db.suppliers, {
      as: 'awarded_supplier',
      foreignKey: {
        name: 'awarded_supplierId',
      },
      constraints: false,
    });

    db.awards.belongsTo(db.users, {
      as: 'awarded_by',
      foreignKey: {
        name: 'awarded_byId',
      },
      constraints: false,
    });

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



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


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

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



  return awards;
};


