from django.db import models
from api.models.data.base import BaseModel



class Product(BaseModel):
    """Product model for inventory items (fuel, lubricants, etc.)"""
    name = models.CharField(max_length=255)
    barcode = models.CharField(max_length=100, blank=True, null=True)
    description = models.TextField(blank=True, null=True)
    image = models.ImageField(upload_to='products/', blank=True, null=True)
    bottle_capacity_liters = models.DecimalField(
        max_digits=10,
        decimal_places=4,
        null=True,
        blank=True,
        help_text='Liquid capacity of one bottle for packaged liquid products.',
    )
    carton_capacity_liters = models.DecimalField(
        max_digits=12,
        decimal_places=4,
        null=True,
        blank=True,
        help_text='Liquid capacity of one carton for packaged liquid products.',
    )
    bottles_per_carton = models.PositiveIntegerField(
        default=8,
        help_text='Number of bottles per carton for this product variant.',
    )


    class Meta:
        constraints = [
            models.UniqueConstraint(
                fields=['barcode'],
                condition=models.Q(barcode__isnull=False) & ~models.Q(barcode=''),
                name='uniq_product_barcode',
            ),
        ]

    def __str__(self):
        return f"{self.name} ({self.barcode})"
