from __future__ import annotations

from datetime import datetime

from repo_core.base.models import DomainModel


class CodalError(DomainModel):
    """An error that occurred during crawling — fetch failure or download failure.

    Write-once (insert-only).  One row per error event.
    PostgreSQL-only — no search or warehouse backends.
    """

    # -- Identity -------------------------------------------------------
    id: str

    # -- Context --------------------------------------------------------
    url: str
    statement_id: str | None = None
    publisher_id: int | None = None

    # -- Error details --------------------------------------------------
    status_code: int | None = None
    error_message: str | None = None
    error_type: str | None = None  # "fetch" | "download" | "parse"

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

    # -- Backend field filtering ----------------------------------------
    _db_include: set[str] | None = {
        "id", "url", "statement_id", "publisher_id",
        "status_code", "error_message", "error_type",
        "created_at", "updated_at",
    }
    _search_include: set[str] | None = set()
    _warehouse_include: set[str] | None = set()
    _timestamps = {"created_at", "updated_at"}
