"""Domain model for inspected codal.ir media files.

PostgreSQL-only — no Elasticsearch or Doris backing.
One row per inspection — re-inspecting the same ``web_media_id``
upserts the existing row.
"""

from __future__ import annotations

from datetime import datetime

from repo_core.base.models import DomainModel


class CodalMedia(DomainModel):
    """Inspected codal.ir media file metadata.

    Keyed by ``web_media_id`` — one row per download.  Re-inspecting
    the same media file upserts the row.

    PostgreSQL-only — no warehouse or search backends.

    Future fields: ``width``, ``height`` (image dimensions),
    ``parent_id`` (self-referencing for related media).
    """

    # -- Identity -------------------------------------------------------
    id: str
    web_media_id: str | None = None

    # -- Filename / extension -------------------------------------------
    source_url: str = ""
    source_filename: str | None = None
    stored_filename: str | None = None
    content_type: str | None = None
    content_disposition: str | None = None
    extension_raw: str | None = None
    extension_detected: str | None = None
    format: str | None = None  # xlsx, xls, pdf

    # -- Content checksums ----------------------------------------------
    sha256: str | None = None
    byte_size: int | None = None
    etag: str | None = None

    # -- Response metadata ----------------------------------------------
    final_url: str | None = None
    status_code: int | None = None
    latency_ms: int | None = None
    request_method: str | None = None
    retry_count: int = 0
    redirect_chain: list | None = None

    # -- Timing ---------------------------------------------------------
    downloaded_at: datetime | None = None
    requested_at: datetime | None = None
    discovered_at: datetime | None = None

    # -- PDF-specific ---------------------------------------------------
    pages: int | None = None
    author: str | None = None
    subject: str | None = None
    creator: str | None = None
    producer: str | None = None

    # -- Excel-specific -------------------------------------------------
    encoding: str | None = None
    sheets: dict | None = None  # {sheet_name: [headers]}

    # -- Validation results ---------------------------------------------
    validations: dict | None = None  # {rule: {status, ...}, passed, failed, skipped}

    # -- Codal relation --------------------------------------------------
    statement_id: str | None = None  # numeric code from /statement/<code>/
    publisher_id: int | None = None  # numeric publisher ID (future)

    # -- Future use -----------------------------------------------------
    width: int | None = None
    height: int | None = None
    parent_id: str | None = None  # self-ref → codal_media.id

    # -- Timestamps -----------------------------------------------------
    created_at: datetime | None = None
    updated_at: datetime | None = None

    # -- Backend field filtering ----------------------------------------
    _db_include: set[str] | None = {
        "id",
        "web_media_id",
        "source_url",
        "source_filename",
        "stored_filename",
        "content_type",
        "content_disposition",
        "extension_raw",
        "extension_detected",
        "format",
        "sha256",
        "byte_size",
        "etag",
        "final_url",
        "status_code",
        "latency_ms",
        "request_method",
        "retry_count",
        "redirect_chain",
        "downloaded_at",
        "requested_at",
        "discovered_at",
        "pages",
        "author",
        "subject",
        "creator",
        "producer",
        "encoding",
        "sheets",
        "validations",
        "statement_id",
        "publisher_id",
        "width",
        "height",
        "parent_id",
        "created_at",
        "updated_at",
    }

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