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

legal_name: {
        type: DataTypes.TEXT,
      
      

      },

commercial_name: {
        type: DataTypes.TEXT,
      
      

      },

rccm_number: {
        type: DataTypes.TEXT,
      
      

      },

tax_id: {
        type: DataTypes.TEXT,
      
      

      },

country: {
        type: DataTypes.ENUM,
      
      

        values: [

"CD",


"CM",


"GA",


"MG",


"KE",


"TZ",


"FR",


"BE",


"US",


"UK",


"OTHER"

        ],

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

primary_contact_name: {
        type: DataTypes.TEXT,
      
      

      },

primary_contact_email: {
        type: DataTypes.TEXT,
      
      

      },

primary_contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

bank_name: {
        type: DataTypes.TEXT,
      
      

      },

bank_account_number: {
        type: DataTypes.TEXT,
      
      

      },

bank_swift: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"active",


"blocked"

        ],

      },

prequalification_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"not_started",


"in_review",


"validated",


"rejected"

        ],

      },

performance_score: {
        type: DataTypes.DECIMAL,
      
      

      },

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

  suppliers.associate = (db) => {


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

















    db.suppliers.hasMany(db.supplier_diligence_checks, {
      as: 'supplier_diligence_checks_supplier',
      foreignKey: {
          name: 'supplierId',
      },
      constraints: false,
    });



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




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


    db.suppliers.hasMany(db.purchase_orders, {
      as: 'purchase_orders_supplier',
      foreignKey: {
          name: 'supplierId',
      },
      constraints: false,
    });














//end loop



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

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



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


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

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



  return suppliers;
};


