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

tender_number: {
        type: DataTypes.TEXT,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

tender_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"open_tender",


"restricted_tender",


"consultation",


"rfq"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

closing_at: {
        type: DataTypes.DATE,
      
      

      },

opening_at: {
        type: DataTypes.DATE,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"closed",


"under_evaluation",


"recommended",


"awarded",


"cancelled"

        ],

      },

eligibility_criteria: {
        type: DataTypes.TEXT,
      
      

      },

submission_instructions: {
        type: DataTypes.TEXT,
      
      

      },

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

  tenders.associate = (db) => {


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

























    db.tenders.hasMany(db.bids, {
      as: 'bids_tender',
      foreignKey: {
          name: 'tenderId',
      },
      constraints: false,
    });



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


























//end loop



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

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




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

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



  return tenders;
};


