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,
      },

item_code: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

item_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"raw_material",


"component",


"subassembly",


"finished_good",


"consumable"

        ],

      },

tracking: {
        type: DataTypes.ENUM,
      
      

        values: [

"none",


"lot",


"serial"

        ],

      },

default_scrap_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

shelf_life_days: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

      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.item_supplier_links, {
      as: 'item_supplier_links_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });




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


    db.items.hasMany(db.inventory_balances, {
      as: 'inventory_balances_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.bom_headers, {
      as: 'bom_headers_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });


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




    db.items.hasMany(db.routing_headers, {
      as: 'routing_headers_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_issues, {
      as: 'material_issues_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });


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


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


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


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





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



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



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




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




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




//end loop



    db.items.belongsTo(db.uoms, {
      as: 'uom',
      foreignKey: {
        name: 'uomId',
      },
      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: 'attachments',
      foreignKey: 'belongsToId',
      constraints: false,
      scope: {
        belongsTo: db.items.getTableName(),
        belongsToColumn: 'attachments',
      },
    });


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

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



  return items;
};


