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

sku: {
        type: DataTypes.TEXT,
      
      

      },

name: {
        type: DataTypes.TEXT,
      
      

      },

description: {
        type: DataTypes.TEXT,
      
      

      },

item_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"stock",


"non_stock",


"service"

        ],

      },

tracking_mode: {
        type: DataTypes.ENUM,
      
      

        values: [

"none",


"lot",


"serial"

        ],

      },

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

      },

sales_price: {
        type: DataTypes.DECIMAL,
      
      

      },

purchase_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

reorder_point: {
        type: DataTypes.DECIMAL,
      
      

      },

reorder_qty: {
        type: DataTypes.DECIMAL,
      
      

      },

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


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



    db.items.hasMany(db.quotation_lines, {
      as: 'quotation_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.invoice_lines, {
      as: 'invoice_lines_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });



    db.items.hasMany(db.customer_return_lines, {
      as: 'customer_return_lines_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.receipt_lines, {
      as: 'receipt_lines_item',
      foreignKey: {
          name: 'itemId',
      },
      constraints: false,
    });



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

















//end loop



    db.items.belongsTo(db.tenants, {
      as: 'tenant',
      foreignKey: {
        name: 'tenantId',
      },
      constraints: false,
    });

    db.items.belongsTo(db.item_categories, {
      as: 'category',
      foreignKey: {
        name: 'categoryId',
      },
      constraints: false,
    });

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

    db.items.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      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;
};


