from __future__ import annotations

from datetime import datetime

from repo_core.base.models import DomainModel


class CodalPublisherPage(DomainModel):
    """A single tab's worth of raw HTML + extracted data from a codal.ir publisher page.

    One publisher can have multiple page rows — one per tab.
    Keyed by ``(publisher_id, tab_slug)``.

    Mirrors ``CodalStatementDetail`` — same pattern, different entity
    (publisher instead of statement).
    """

    # -- Identity -------------------------------------------------------
    id: str
    publisher_id: int
    tab_slug: str             # "main", "board", "contact"
    tab_name: str | None = None

    # -- Tab content ----------------------------------------------------
    tab_url: str | None = None
    raw_html: str | None = None
    tab_data: dict | None = None   # JSONB — extracted key-value pairs

    # -- Timing ---------------------------------------------------------
    crawl_time: datetime | None = None

    # -- Status ---------------------------------------------------------
    status: str | None = None        # "fetched", "error", "not_found"
    error_message: str | None = None

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

    # -- Backend field filtering ----------------------------------------
    _db_include: set[str] | None = {
        "id", "publisher_id", "tab_slug", "tab_name",
        "tab_url", "raw_html", "tab_data",
        "crawl_time", "status", "error_message",
        "created_at", "updated_at",
    }
    _search_include: set[str] | None = set()
    _warehouse_include: set[str] | None = None
    _timestamps = {"created_at", "updated_at"}
