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

name: {
        type: DataTypes.TEXT,
      
      

      },

trade_name: {
        type: DataTypes.TEXT,
      
      

      },

vendor_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"company",


"ngo",


"public_entity",


"individual"

        ],

      },

registration_number: {
        type: DataTypes.TEXT,
      
      

      },

tax_number: {
        type: DataTypes.TEXT,
      
      

      },

contact_name: {
        type: DataTypes.TEXT,
      
      

      },

contact_email: {
        type: DataTypes.TEXT,
      
      

      },

contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

bank_name: {
        type: DataTypes.TEXT,
      
      

      },

bank_account_number: {
        type: DataTypes.TEXT,
      
      

      },

bank_account_name: {
        type: DataTypes.TEXT,
      
      

      },

bank_swift: {
        type: DataTypes.TEXT,
      
      

      },

qualification_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"unsubmitted",


"under_review",


"qualified",


"conditionally_qualified",


"suspended",


"disqualified"

        ],

      },

qualification_reviewed_at: {
        type: DataTypes.DATE,
      
      

      },

risk_rating: {
        type: DataTypes.ENUM,
      
      

        values: [

"low",


"medium",


"high",


"critical"

        ],

      },

is_blacklisted: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

compliance_valid_until: {
        type: DataTypes.DATE,
      
      

      },

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

  vendors.associate = (db) => {


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
























    db.vendors.hasMany(db.vendor_compliance_documents, {
      as: 'vendor_compliance_documents_vendor',
      foreignKey: {
          name: 'vendorId',
      },
      constraints: false,
    });


    db.vendors.hasMany(db.bids, {
      as: 'bids_vendor',
      foreignKey: {
          name: 'vendorId',
      },
      constraints: false,
    });










    db.vendors.hasMany(db.contracts, {
      as: 'contracts_vendor',
      foreignKey: {
          name: 'vendorId',
      },
      constraints: false,
    });










    db.vendors.hasMany(db.invoices, {
      as: 'invoices_vendor',
      foreignKey: {
          name: 'vendorId',
      },
      constraints: false,
    });




    db.vendors.hasMany(db.payments, {
      as: 'payments_vendor',
      foreignKey: {
          name: 'vendorId',
      },
      constraints: false,
    });







//end loop



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




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

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



  return vendors;
};


