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

lot_number: {
        type: DataTypes.TEXT,
      
      

      },

lot_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"available",


"quarantine",


"blocked",


"consumed",


"expired"

        ],

      },

quantity_on_hand: {
        type: DataTypes.DECIMAL,
      
      

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

manufactured_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

certificate_number: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  inventory_lots.associate = (db) => {


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

























    db.inventory_lots.hasMany(db.inventory_transactions, {
      as: 'inventory_transactions_lot',
      foreignKey: {
          name: 'lotId',
      },
      constraints: false,
    });






    db.inventory_lots.hasMany(db.inspections, {
      as: 'inspections_lot',
      foreignKey: {
          name: 'lotId',
      },
      constraints: false,
    });


    db.inventory_lots.hasMany(db.nonconformances, {
      as: 'nonconformances_lot',
      foreignKey: {
          name: 'lotId',
      },
      constraints: false,
    });



    db.inventory_lots.hasMany(db.alerts, {
      as: 'alerts_related_lot',
      foreignKey: {
          name: 'related_lotId',
      },
      constraints: false,
    });




//end loop



    db.inventory_lots.belongsTo(db.materials, {
      as: 'material',
      foreignKey: {
        name: 'materialId',
      },
      constraints: false,
    });

    db.inventory_lots.belongsTo(db.warehouses, {
      as: 'warehouse',
      foreignKey: {
        name: 'warehouseId',
      },
      constraints: false,
    });

    db.inventory_lots.belongsTo(db.storage_locations, {
      as: 'location',
      foreignKey: {
        name: 'locationId',
      },
      constraints: false,
    });

    db.inventory_lots.belongsTo(db.units_of_measure, {
      as: 'uom',
      foreignKey: {
        name: 'uomId',
      },
      constraints: false,
    });

    db.inventory_lots.belongsTo(db.suppliers, {
      as: 'supplier',
      foreignKey: {
        name: 'supplierId',
      },
      constraints: false,
    });



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


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

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



  return inventory_lots;
};


