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

name: {
        type: DataTypes.TEXT,
      
      

      },

item_number: {
        type: DataTypes.TEXT,
      
      

      },

item_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"raw_material",


"component",


"subassembly",


"finished_good",


"packaging",


"consumable"

        ],

      },

lot_tracked: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

serial_tracked: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

standard_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

active: {
        type: DataTypes.BOOLEAN,
      
        allowNull: false,
        defaultValue: false,
      
      

      },

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

  items.associate = (db) => {


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















    db.items.hasMany(db.boms, {
      as: 'boms_parent_item',
      foreignKey: {
          name: 'parent_itemId',
      },
      constraints: false,
    });


    db.items.hasMany(db.bom_lines, {
      as: 'bom_lines_component_item',
      foreignKey: {
          name: 'component_itemId',
      },
      constraints: false,
    });


    db.items.hasMany(db.routings, {
      as: 'routings_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });



    db.items.hasMany(db.production_orders, {
      as: 'production_orders_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });



    db.items.hasMany(db.material_requirements, {
      as: 'material_requirements_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });


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


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


    db.items.hasMany(db.inventory_transactions, {
      as: 'inventory_transactions_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });


    db.items.hasMany(db.inspections, {
      as: 'inspections_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });



    db.items.hasMany(db.nonconformances, {
      as: 'nonconformances_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });


    db.items.hasMany(db.quality_holds, {
      as: 'quality_holds_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });



    db.items.hasMany(db.documents, {
      as: 'documents_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });



//end loop



    db.items.belongsTo(db.uoms, {
      as: 'default_uom',
      foreignKey: {
        name: 'default_uomId',
      },
      constraints: false,
    });

    db.items.belongsTo(db.warehouses, {
      as: 'default_warehouse',
      foreignKey: {
        name: 'default_warehouseId',
      },
      constraints: false,
    });



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

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


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

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



  return items;
};


