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

lot_number: {
        type: DataTypes.TEXT,
      
      

      },

manufactured_at: {
        type: DataTypes.DATE,
      
      

      },

received_at: {
        type: DataTypes.DATE,
      
      

      },

expiration_at: {
        type: DataTypes.DATE,
      
      

      },

quality_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"quarantine",


"released",


"rejected",


"on_hold"

        ],

      },

quantity_on_hand: {
        type: DataTypes.DECIMAL,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  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.lots.hasMany(db.serial_numbers, {
      as: 'serial_numbers_lot',
      foreignKey: {
          name: 'lotId',
      },
      constraints: false,
    });


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


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



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


    db.lots.hasMany(db.quality_holds, {
      as: 'quality_holds_lot',
      foreignKey: {
          name: 'lotId',
      },
      constraints: false,
    });





//end loop



    db.lots.belongsTo(db.items, {
      as: 'item',
      foreignKey: {
        name: 'itemId',
      },
      constraints: false,
    });

    db.lots.belongsTo(db.plants, {
      as: 'plant',
      foreignKey: {
        name: 'plantId',
      },
      constraints: false,
    });

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

    db.lots.belongsTo(db.production_orders, {
      as: 'production_order',
      foreignKey: {
        name: 'production_orderId',
      },
      constraints: false,
    });




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

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



  return lots;
};


