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

award_number: {
        type: DataTypes.TEXT,
      
      

      },

award_amount: {
        type: DataTypes.DECIMAL,
      
      

      },

currency: {
        type: DataTypes.ENUM,
      
      

        values: [

"CDF",


"USD"

        ],

      },

decision_date: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"submitted",


"approved",


"rejected",


"contract_initiated",


"cancelled"

        ],

      },

award_memo: {
        type: DataTypes.TEXT,
      
      

      },

      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


































    db.awards.hasMany(db.contracts, {
      as: 'contracts_award',
      foreignKey: {
          name: 'awardId',
      },
      constraints: false,
    });



















//end loop



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

    db.awards.belongsTo(db.tenders, {
      as: 'tender',
      foreignKey: {
        name: 'tenderId',
      },
      constraints: false,
    });

    db.awards.belongsTo(db.bids, {
      as: 'winning_bid',
      foreignKey: {
        name: 'winning_bidId',
      },
      constraints: false,
    });




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

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



  return awards;
};


