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

supplier_name: {
        type: DataTypes.TEXT,
      
      

      },

supplier_code: {
        type: DataTypes.TEXT,
      
      

      },

supplier_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"manufacturer",


"critical_supplier",


"service_provider",


"distributor",


"laboratory",


"other"

        ],

      },

contact_name: {
        type: DataTypes.TEXT,
      
      

      },

contact_email: {
        type: DataTypes.TEXT,
      
      

      },

contact_phone: {
        type: DataTypes.TEXT,
      
      

      },

address: {
        type: DataTypes.TEXT,
      
      

      },

qualification_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"prospect",


"qualified",


"conditionally_qualified",


"disqualified"

        ],

      },

last_audit_at: {
        type: DataTypes.DATE,
      
      

      },

next_audit_at: {
        type: DataTypes.DATE,
      
      

      },

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

      },

      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.certificates_of_analysis, {
      as: 'certificates_of_analysis_supplier',
      foreignKey: {
          name: 'supplierId',
      },
      constraints: false,
    });






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



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







//end loop





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


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

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



  return suppliers;
};


