"""Raw fetched HTML stored in Doris warehouse.

Keyed by a UUID ``id`` column — every fetch creates a new unique row.
"""

from __future__ import annotations

from repo_core.base.models import DomainModel


class WebHtmlRaw(DomainModel):
    """Raw fetched HTML stored in Doris warehouse.

    Keyed by a UUID ``id`` column — auto-generated on insert.

    Usage::

        row = WebHtmlRaw(
            id="550e8400-e29b-41d4-a716-446655440000",
            seed_id="seed-uuid",
            url="https://example.com",
            raw_html="<!DOCTYPE html>...",
            status_code=200,
            depth=1,
            fetched_at="2026-07-18T12:00:00+00:00",
        )
        data = row.to_warehouse()
    """

    id: str
    seed_id: str
    url: str
    raw_html: str | None = None
    status_code: int | None = None
    headers: dict | None = None
    depth: int = 0
    fetched_at: str | None = None  # ISO 8601 string
    # -- Request metadata -----------------------------------------------
    final_url: str | None = None  # URL after redirects
    request_method: str | None = "GET"  # HTTP method used
    redirect_chain: list | None = None  # Intermediate URLs during redirects
    retry_count: int | None = 0  # Number of retry attempts before success
    latency_ms: int | None = None  # HTTP request duration in milliseconds
    # -- Content hash & versioning --------------------------------------
    raw_hash: str | None = None  # SHA-256 hex digest of the HTML content
    version: int = 1  # Monotonically increasing per URL
    created_at: int | None = None  # Unix int seconds (Doris convention)

    _warehouse_include: set[str] | None = {
        "id",
        "seed_id",
        "url",
        "raw_html",
        "status_code",
        "headers",
        "depth",
        "fetched_at",
        "final_url",
        "request_method",
        "redirect_chain",
        "retry_count",
        "latency_ms",
        "raw_hash",
        "version",
        "created_at",
    }
    _db_include: set[str] | None = set()  # warehouse-only → skip DB
    _search_include: set[str] | None = set()  # warehouse-only → skip search
    _timestamps = {"created_at"}
