from __future__ import annotations

from datetime import datetime

from repo_core.base.models import DomainModel


class CodalStatementListing(DomainModel):
    """A single listing page from codal.ir.

    Each page contains an array of statements (typically ~12 rows from
    the listing table).  The same statement may appear across multiple
    pages and statement types.  Keyed by ``(statement_type, page)``.
    """

    # -- Identity -------------------------------------------------------
    id: str
    statement_type: str    # e.g. "104", "102", "21"
    page: int = 1
    version: int = 1       # Incremented on re-crawl of same page

    # -- Raw HTML -------------------------------------------------------
    raw_html: str | None = None

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

    # -- Statements (JSON array) ----------------------------------------
    statements: list[dict] | None = None

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

    # -- Backend field filtering ----------------------------------------
    _db_include: set[str] | None = {
        "id", "statement_type", "page", "version",
        "raw_html", "crawl_time",
        "statements", "created_at", "updated_at",
    }
    _search_include: set[str] | None = set()
    _warehouse_include: set[str] | None = None
    _timestamps = {"created_at", "updated_at"}
