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

reference: {
        type: DataTypes.TEXT,
      
      

      },

title: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"draft",


"published",


"closed",


"cancelled"

        ],

      },

published_at: {
        type: DataTypes.DATE,
      
      

      },

submission_deadline: {
        type: DataTypes.DATE,
      
      

      },

opened_at: {
        type: DataTypes.DATE,
      
      

      },

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

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

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

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



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


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

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



  return tender_events;
};


