from django.db import models
from django.utils import timezone  # if needed elsewhere


class Category(models.Model):
    icon = models.FileField(upload_to="category/icon")
    category_name = models.CharField(max_length=50, null=True, blank=True)

    def __str__(self):
        return self.category_name or ""


class Product(models.Model):
    category = models.ForeignKey(
        Category,
        on_delete=models.CASCADE,
        null=True,          # allow empty if you want
        blank=True,         # allow empty in forms
        default=None,       # or remove default completely
        # OR: default=1  # only if you have Category with id=1
    )
    name = models.CharField(max_length=100)
    slug = models.SlugField(unique=True)
    short_description = models.TextField()
    family = models.CharField(max_length=100, blank=True)

    def __str__(self):
        return self.name
        

class ProductFeature(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE,
                                related_name="features")
    name = models.CharField(max_length=100)                   # e.g. "Waterproof", "LED", "IP65" [image:1]
    icon = models.ImageField(upload_to="features/", blank=True, null=True)

    class Meta:
        unique_together = ("product", "name")

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

class ProductDescription(models.Model):
    """
    Structured description block: Housing, Diffuser, Reflector, LED, Standards, etc.
    """
    product = models.OneToOneField(Product, on_delete=models.CASCADE,
                                   related_name="description")
    housing = models.CharField(max_length=255)                # "Die cast aluminum powder coated..." [image:1]
    diffuser = models.CharField(max_length=255)               # "Clear or frosted tempered glass" [image:1]
    reflector = models.CharField(max_length=255)              # "Pure anodized aluminium reflector" [image:1]
    led = models.CharField(max_length=255)                    # lifetime, efficiency text [web:8]
    standards = models.CharField(max_length=255)              # PTPS IEC / EN numbers [image:1]




class ProductImage(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE,
                                related_name="images")
    image = models.ImageField(upload_to="products/")
    is_drawing = models.BooleanField(default=False)           # technical drawing (dimension sketch) [image:1]
    angle = models.CharField(max_length=50, blank=True)       # e.g. "24°", "36°" icon images [image:1]
    order = models.PositiveSmallIntegerField(default=0)

    class Meta:
        ordering = ["order"]

    def __str__(self):
        return f"{self.product.name} image {self.id}"
