"""Web download metric — per-media operational telemetry (warehouse-only).

Stores download/upload timing and status for each media file downloaded
by web-downloader.  INSERT-only via DUPLICATE KEY — every download
creates a new row regardless of ``source_url``.  Query the latest by
``ORDER BY created_at DESC LIMIT 1``.

Warehouse-only — no PostgreSQL or Elasticsearch backing.
"""

from __future__ import annotations

from datetime import datetime

from repo_core.base.models import DomainModel


class WebDownloadMetric(DomainModel):
    """Per-media operational metadata for dashboard metrics.

    INSERT-only — every download creates a new row.  The table uses
    DUPLICATE KEY so ``source_url`` is not unique; re-downloads produce
    separate rows.  Query the latest by ``ORDER BY created_at DESC
    LIMIT 1``.

    Warehouse-only — no PostgreSQL or Elasticsearch backing.

    Usage::

        metric = WebDownloadMetric(
            source_url="https://example.com/photo.jpg",
            seed_id="seed-uuid",
            media_type="image",
            status="downloaded",
            content_length=102400,
            download_duration_ms=1500,
        )
        row = metric.to_warehouse()
    """

    source_url: str
    seed_id: str
    media_type: str
    status: str
    content_type: str | None = None
    content_length: int | None = None
    download_duration_ms: int | None = None
    upload_duration_ms: int | None = None
    total_duration_ms: int | None = None
    error_message: str | None = None
    downloaded_at: datetime | None = None
    created_at: datetime | None = None

    _warehouse_include: set[str] | None = {
        "source_url",
        "seed_id",
        "media_type",
        "status",
        "content_type",
        "content_length",
        "download_duration_ms",
        "upload_duration_ms",
        "total_duration_ms",
        "error_message",
        "downloaded_at",
        "created_at",
    }

    _db_include: set[str] | None = set()
    _search_include: set[str] | None = set()
    _timestamps: set[str] = {"created_at"}
