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,
      
      

      },

started_on: {
        type: DataTypes.DATE,
      
      

      },

completed_on: {
        type: DataTypes.DATE,
      
      

      },

quantity_produced: {
        type: DataTypes.DECIMAL,
      
      

      },

release_status: {
        type: DataTypes.ENUM,
      
      

        values: [

"quarantine",


"released",


"blocked",


"scrapped"

        ],

      },

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.qa_inspections, {
      as: 'qa_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,
    });





//end loop



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

    db.production_lots.belongsTo(db.storage_locations, {
      as: 'stored_location',
      foreignKey: {
        name: 'stored_locationId',
      },
      constraints: false,
    });




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

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



  return production_lots;
};


