import uuid

from sqlalchemy import ForeignKey, Integer, String
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import Mapped, mapped_column

from app.models.base import Base, TimestampMixin, UUIDPKMixin


class Tag(UUIDPKMixin, TimestampMixin, Base):
    __tablename__ = "tags"

    name: Mapped[str] = mapped_column(String(128), nullable=False)
    slug: Mapped[str] = mapped_column(String(128), nullable=False, unique=True)
    parent_tag_id: Mapped[uuid.UUID | None] = mapped_column(
        UUID(as_uuid=True), ForeignKey("tags.id", ondelete="SET NULL")
    )
    description: Mapped[str | None] = mapped_column(String(1024))
    # Denormalizowany licznik scen z żywym playback (refresh w tle przez
    # _job_refresh_taxonomy_counts). Patrz migracja 0019. NIE źródło prawdy —
    # do sortu "popular" + badge "(N)" w filtrach.
    scene_count: Mapped[int] = mapped_column(
        Integer, nullable=False, default=0, server_default="0"
    )
