"""Typed models for the artwork pipeline.

Ported from iOpenPod ArtworkDB_Writer/artwork_types.py, simplified.
"""

from dataclasses import dataclass, field


@dataclass(frozen=True)
class IthmbLocation:
    filename: str
    offset: int


@dataclass(frozen=True)
class ExistingFormatRef:
    path: str
    ithmb_offset: int
    size: int
    width: int
    height: int
    hpad: int = 0
    vpad: int = 0
    ithmb_filename: str = ""

    @property
    def stride_pixels(self) -> int:
        return max(1, self.width + self.hpad)

    @property
    def stored_height(self) -> int:
        return max(1, self.height + self.vpad)


@dataclass(frozen=True)
class EncodedFormatPayload:
    data: bytes
    width: int
    height: int
    size: int
    stride_pixels: int
    hpad: int = 0
    vpad: int = 0
    pixel_format: str | None = None

    @classmethod
    def from_existing_ref(cls, ref: ExistingFormatRef, data: bytes) -> "EncodedFormatPayload":
        return cls(
            data=data,
            width=ref.width,
            height=ref.height,
            size=ref.size,
            stride_pixels=ref.stride_pixels,
            hpad=ref.hpad,
            vpad=ref.vpad,
        )


ArtworkFormatPayload = EncodedFormatPayload | ExistingFormatRef


@dataclass
class ArtworkPayload:
    formats: dict[int, ArtworkFormatPayload] = field(default_factory=dict)
    src_img_size: int = 0


@dataclass
class ArtworkEntry:
    img_id: int
    db_track_id: int
    art_hash: str | None
    src_img_size: int
    formats: dict[int, ArtworkFormatPayload] = field(default_factory=dict)
    db_track_ids: list[int] = field(default_factory=list)
