"""Artist — unified entity that is both the gallery's ``artist:`` tag concept
and GallerySubscriber's Subscription. ``is_subscription`` is True if any
Sources are attached.
"""

from datetime import datetime

from sqlalchemy import Boolean, DateTime, Integer, String, Text, func
from sqlalchemy.orm import Mapped, mapped_column, relationship

from .base import Base


class Artist(Base):
    __tablename__ = "artist"

    id: Mapped[int] = mapped_column(Integer, primary_key=True)
    # Display name: freely editable, NON-unique (two real creators can share a
    # name). Decoupled from identity/storage in migration 0077 (#130) — renaming
    # touches ONLY this. Was unique until then.
    name: Mapped[str] = mapped_column(String(255), nullable=False)
    # Storage/identity key: IMMUTABLE + unique. This is the on-disk path
    # component (download_service artist_slug = artist.slug → images_root/<slug>/
    # <platform>/…), so it is set once at creation (collision-suffixed) and NEVER
    # changes — a rename must not move files. Existing artists keep their slug.
    slug: Mapped[str] = mapped_column(String(255), nullable=False, unique=True)
    notes: Mapped[str | None] = mapped_column(Text, nullable=True)

    # True once a Source is attached; flips false if all sources removed.
    is_subscription: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)

    # Per-artist scheduling overrides; null means "use global default".
    auto_check: Mapped[bool] = mapped_column(Boolean, nullable=False, default=True)
    check_interval_seconds: Mapped[int | None] = mapped_column(Integer, nullable=True)

    created_at: Mapped[datetime] = mapped_column(
        DateTime(timezone=True), nullable=False, server_default=func.now()
    )

    sources = relationship("Source", back_populates="artist", cascade="all, delete-orphan")
