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

lot_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"quarantine",


"released",


"on_hold",


"scrapped",


"shipped"

        ],

      },

produced_at: {
        type: DataTypes.DATE,
      
      

      },

quantity_produced: {
        type: DataTypes.DECIMAL,
      
      

      },

uom: {
        type: DataTypes.TEXT,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  production_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.production_lots.hasMany(db.inspections, {
      as: 'inspections_production_lot',
      foreignKey: {
          name: 'production_lotId',
      },
      constraints: false,
    });



    db.production_lots.hasMany(db.nonconformances, {
      as: 'nonconformances_production_lot',
      foreignKey: {
          name: 'production_lotId',
      },
      constraints: false,
    });









    db.production_lots.hasMany(db.shipment_lines, {
      as: 'shipment_lines_production_lot',
      foreignKey: {
          name: 'production_lotId',
      },
      constraints: false,
    });



//end loop



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

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

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

    db.production_lots.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return production_lots;
};


