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

serial_number: {
        type: DataTypes.TEXT,
      
      

      },

status: {
        type: DataTypes.ENUM,
      
      

        values: [

"in_process",


"in_stock",


"shipped",


"scrapped",


"returned"

        ],

      },

created_on: {
        type: DataTypes.DATE,
      
      

      },

notes: {
        type: DataTypes.TEXT,
      
      

      },

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

  serial_numbers.associate = (db) => {


/// loop through entities and it's fields, and if ref === current e[name] and create relation has many on parent entity
























    db.serial_numbers.hasMany(db.inventory_transactions, {
      as: 'inventory_transactions_serial_number',
      foreignKey: {
          name: 'serial_numberId',
      },
      constraints: false,
    });


    db.serial_numbers.hasMany(db.inspections, {
      as: 'inspections_serial_number',
      foreignKey: {
          name: 'serial_numberId',
      },
      constraints: false,
    });



    db.serial_numbers.hasMany(db.nonconformances, {
      as: 'nonconformances_serial_number',
      foreignKey: {
          name: 'serial_numberId',
      },
      constraints: false,
    });






//end loop



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

    db.serial_numbers.belongsTo(db.lots, {
      as: 'lot',
      foreignKey: {
        name: 'lotId',
      },
      constraints: false,
    });

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




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

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



  return serial_numbers;
};


