'use strict';
const {
  Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Product extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // define association here
      Product.hasMany(models.TransactionItem, { foreignKey: 'productId', as: 'transactionitem' });
      Product.hasMany(models.StockMovement, { foreignKey: 'productId', as: 'stockmovements' });
      Product.hasMany(models.PurchaseOrderItem, { foreignKey: 'productId', as: 'purchaseorderitems' });
      Product.hasMany(models.DiscountProduct, { foreignKey: 'productId', as: 'discountproducts' });
      Product.hasMany(models.Inventory, { foreignKey: 'productId', as: 'inventory' });
     
      Product.belongsTo(models.Category, { foreignKey: 'categoryId', as: 'category' }); 
      Product.belongsTo(models.Brand, { foreignKey: 'brandId', as: 'brand' });

      Product.hasMany(models.ReturnItem, { foreignKey: 'productId', as: 'returnitems' });
      
      Product.hasMany(models.ProductVariant, { foreignKey: 'productId', as: 'variants' });

      Product.hasMany(models.ProductImage, { foreignKey: 'productId', as: 'images' });

      


      
      
    }
  }
  Product.init({
    sku: DataTypes.STRING,
    name: DataTypes.STRING,
    description: DataTypes.TEXT,
    categoryId: DataTypes.INTEGER,
    brandId: DataTypes.INTEGER,
    unitPrice: DataTypes.DECIMAL,
    costPrice: DataTypes.DECIMAL,
    barcode: DataTypes.STRING,

    stock: DataTypes.STRING,

    imageUrl: DataTypes.STRING,
    unitOfMeasure: DataTypes.STRING,
    taxRate: DataTypes.DECIMAL,
    taxType: DataTypes.STRING,
    isTrackInventory: DataTypes.BOOLEAN,
    minStock: DataTypes.INTEGER,
    maxStock: DataTypes.INTEGER,
    reorderLevel: DataTypes.INTEGER,
    reorderQuantity: DataTypes.INTEGER,
    isActive: DataTypes.BOOLEAN
  }, {
    sequelize,
    modelName: 'Product',
  });
  return Product;
};