"""ORM model for albums."""

import uuid

from sqlalchemy import ForeignKey, Integer, String, UniqueConstraint
from sqlalchemy.orm import Mapped, mapped_column

from app.infrastructure.db.base import Base
from app.infrastructure.db.models.mixins import TimestampMixin, UUIDPrimaryKeyMixin


class AlbumModel(UUIDPrimaryKeyMixin, TimestampMixin, Base):
    __tablename__ = "albums"
    __table_args__ = (
        # Binds a remote (browsable) album to its local row for re-browse/save
        # dedup. Multiple NULLs are allowed by Postgres, so locally-created
        # albums (source/source_id both NULL) never collide on this.
        UniqueConstraint("source", "source_id", name="uq_albums_source_source_id"),
    )

    title: Mapped[str] = mapped_column(String(1024), index=True, nullable=False)
    artist_id: Mapped[uuid.UUID] = mapped_column(
        ForeignKey("artists.id", ondelete="CASCADE"),
        index=True,
        nullable=False,
    )
    year: Mapped[int | None] = mapped_column(Integer, nullable=True)
    cover_path: Mapped[str | None] = mapped_column(String(1024), nullable=True)
    musicbrainz_id: Mapped[str | None] = mapped_column(String(36), index=True, nullable=True)

    # -- remote identity (lazy materialization) --------------------------
    source: Mapped[str | None] = mapped_column(String(32), nullable=True)
    source_id: Mapped[str | None] = mapped_column(String(512), nullable=True)
