"""Telegram media file domain model — downloaded media tracking.

Each row records a successfully downloaded Telegram media file
(photo, video, document, gif, audio, sticker) stored in S3-compatible
object storage.

Keyed by ``(peer_id, message_id)`` — the natural Telegram business key.
This is a logical relationship to ``telegram_messages``; no DB-level FK
constraint is enforced.
"""

from __future__ import annotations

from datetime import datetime

from repo_core.base.models import DomainModel


class TelegramMediaFile(DomainModel):
    """Downloaded Telegram media file stored in object storage.

    Composite logical key ``(peer_id, message_id)`` links to
    ``telegram_messages``.  One row per message — multiple photos
    in a single message (album) would each get their own row.

    Usage::

        media = TelegramMediaFile(
            peer_id=123,
            message_id=456,
            storage_key="telegram/123/456/photo.jpg",
            bucket="dream-media",
            file_size=1024000,
            mime_type="image/jpeg",
            media_type="photo",
            downloaded_at=datetime.now(timezone.utc),
        )
        row = media.to_db()
    """

    # ------------------------------------------------------------------
    # Composite logical FK — references telegram_messages
    # ------------------------------------------------------------------
    peer_id: int
    message_id: int

    # ------------------------------------------------------------------
    # S3 storage
    # ------------------------------------------------------------------
    storage_key: str
    """S3 object key, e.g. ``"telegram/123/456/photo.jpg"``."""

    bucket: str
    """S3 bucket name."""

    # ------------------------------------------------------------------
    # File metadata
    # ------------------------------------------------------------------
    file_size: int
    """Downloaded file size in bytes."""

    mime_type: str
    """MIME type, e.g. ``"image/jpeg"``, ``"video/mp4"``."""

    media_type: str
    """Telegram media kind: ``photo``, ``video``, ``document``, ``gif``,
    ``audio``, ``sticker``."""

    # ------------------------------------------------------------------
    # Optional dimensions / duration
    # ------------------------------------------------------------------
    width: int | None = None
    """Image or video width in pixels."""

    height: int | None = None
    """Image or video height in pixels."""

    duration_seconds: float | None = None
    """Video or audio duration in seconds."""

    # ------------------------------------------------------------------
    # Timestamps
    # ------------------------------------------------------------------
    downloaded_at: datetime
    """When the file was successfully downloaded and stored (UTC)."""

    created_at: datetime | None = None
    updated_at: datetime | None = None

    # ------------------------------------------------------------------
    # Auto-managed timestamps (UTC datetime).
    # downloaded_at is injected by the caller (tele-media worker).
    # ------------------------------------------------------------------
    _timestamps: set[str] = {"created_at", "updated_at"}

    # ------------------------------------------------------------------
    # PostgreSQL fields — all fields persisted
    # ------------------------------------------------------------------
    _db_include: set[str] = {
        "peer_id",
        "message_id",
        "storage_key",
        "bucket",
        "file_size",
        "mime_type",
        "media_type",
        "width",
        "height",
        "duration_seconds",
        "downloaded_at",
        "created_at",
        "updated_at",
    }

    # ------------------------------------------------------------------
    # Warehouse fields — all metadata, no PK
    # ------------------------------------------------------------------
    _warehouse_include: set[str] = {
        "peer_id",
        "message_id",
        "storage_key",
        "bucket",
        "file_size",
        "mime_type",
        "media_type",
        "width",
        "height",
        "duration_seconds",
        "downloaded_at",
        "created_at",
        "updated_at",
    }
