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

product_name: {
        type: DataTypes.TEXT,
      
      

      },

sku: {
        type: DataTypes.TEXT,
      
      

      },

barcode: {
        type: DataTypes.TEXT,
      
      

      },

product_type: {
        type: DataTypes.ENUM,
      
      

        values: [

"stock_item",


"service",


"bundle"

        ],

      },

unit_of_measure: {
        type: DataTypes.TEXT,
      
      

      },

tax_rate: {
        type: DataTypes.DECIMAL,
      
      

      },

default_cost: {
        type: DataTypes.DECIMAL,
      
      

      },

default_price: {
        type: DataTypes.DECIMAL,
      
      

      },

minimum_price: {
        type: DataTypes.DECIMAL,
      
      

      },

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

      },

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

      },

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

      },

reorder_level: {
        type: DataTypes.INTEGER,
      
      

      },

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

      },

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

  products.associate = (db) => {


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













    db.products.hasMany(db.price_list_items, {
      as: 'price_list_items_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.inventory_batches, {
      as: 'inventory_batches_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });


    db.products.hasMany(db.inventory_transactions, {
      as: 'inventory_transactions_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.stock_adjustment_lines, {
      as: 'stock_adjustment_lines_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.transfer_lines, {
      as: 'transfer_lines_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.supplier_products, {
      as: 'supplier_products_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.purchase_order_lines, {
      as: 'purchase_order_lines_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



    db.products.hasMany(db.goods_receipt_lines, {
      as: 'goods_receipt_lines_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });








    db.products.hasMany(db.sale_lines, {
      as: 'sale_lines_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });
























    db.products.hasMany(db.ai_reorder_suggestions, {
      as: 'ai_reorder_suggestions_product',
      foreignKey: {
          name: 'productId',
      },
      constraints: false,
    });



//end loop



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

    db.products.belongsTo(db.organizations, {
      as: 'organizations',
      foreignKey: {
        name: 'organizationsId',
      },
      constraints: false,
    });



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


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

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



  return products;
};


