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,
      
      

      },

manufactured_at: {
        type: DataTypes.DATE,
      
      

      },

expires_at: {
        type: DataTypes.DATE,
      
      

      },

qa_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"pending",


"released",


"rejected",


"on_hold"

        ],

      },

supplier_lot: {
        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_balances, {
      as: 'inventory_balances_lot',
      foreignKey: {
          name: 'lotId',
      },
      constraints: false,
    });


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














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





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



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










//end loop



    db.inventory_lots.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      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;
};


