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,
      
      

      },

supplier_lot_number: {
        type: DataTypes.TEXT,
      
      

      },

received_on: {
        type: DataTypes.DATE,
      
      

      },

manufactured_on: {
        type: DataTypes.DATE,
      
      

      },

expires_on: {
        type: DataTypes.DATE,
      
      

      },

lot_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"quarantine",


"released",


"blocked",


"consumed",


"scrapped"

        ],

      },

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


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








    db.inventory_lots.hasMany(db.production_material_issues, {
      as: 'production_material_issues_inventory_lot',
      foreignKey: {
          name: 'inventory_lotId',
      },
      constraints: false,
    });



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





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





//end loop



    db.inventory_lots.belongsTo(db.materials, {
      as: 'material',
      foreignKey: {
        name: 'materialId',
      },
      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;
};


