from django.db import models
from .category import FormulaCategory

class Symbol(models.Model):
    symbol_id = models.CharField(max_length=10)  # np. F
    name = models.CharField(max_length=255)
    unit = models.CharField(max_length=50)
    description = models.TextField(blank=True)
    category = models.ForeignKey(FormulaCategory, on_delete=models.PROTECT)
    tags = models.JSONField(default=list, blank=True)
    revision = models.PositiveIntegerField(default=1)
    code = models.CharField(max_length=10, unique=True, editable=False)
    meta = models.TextField(blank=True)  # np. meta-opis dla SEO / edytora


#Zaspis zmiennej z code 
    def save(self, *args, **kwargs):
        if not self.code:
            count = Symbol.objects.filter(category=self.category).count() + 1
            self.code = f"{self.category.prefix:02d}{count:02d}{self.revision:02d}"
        super().save(*args, **kwargs)

#Definiowanie czym jest "self"
    def __str__(self):
        return f"{self.name} ({self.symbol_id}) [{self.code}]"