"""Tests for stage 090 documentation."""

from accountable_record.stage_080_generated.model import (
    GeneratedElementRecord,
    GeneratedRegistry,
)
from accountable_record.stage_090_docs.index import build_documentation
from accountable_record.stage_090_docs.model import (
    DocsArtifact,
    DocsReconciliation,
    DocsRenderPlan,
    PlannedPage,
    RenderedPage,
)


def test_planned_page_model() -> None:
    page = PlannedPage(
        rel_path="docs/elements/order.md",
        title="order",
        compact_id="ar.example.order",
    )

    assert page.rel_path == "docs/elements/order.md"
    assert page.title == "order"
    assert page.compact_id == "ar.example.order"


def test_docs_render_plan_model() -> None:
    page = PlannedPage(
        rel_path="docs/elements/order.md",
        title="order",
        compact_id="ar.example.order",
    )
    plan = DocsRenderPlan(pages=(page,))

    assert plan.pages == (page,)
    assert len(plan.pages) == 1


def test_rendered_page_model() -> None:
    page = RenderedPage(
        rel_path="docs/elements/order.md",
        content="# order\n",
    )

    assert page.rel_path == "docs/elements/order.md"
    assert page.content == "# order\n"


def test_docs_reconciliation_consistent_when_empty() -> None:
    reconciliation = DocsReconciliation()

    assert reconciliation.is_consistent


def test_docs_reconciliation_inconsistent_when_missing_page() -> None:
    reconciliation = DocsReconciliation(
        generated_missing_page=("ar.example.order",),
    )

    assert not reconciliation.is_consistent


def test_docs_artifact_model() -> None:
    planned = PlannedPage(
        rel_path="docs/elements/order.md",
        title="order",
        compact_id="ar.example.order",
    )
    rendered = RenderedPage(
        rel_path="docs/elements/order.md",
        content="# order\n",
    )
    artifact = DocsArtifact(
        target_root=".",
        plan=DocsRenderPlan(pages=(planned,)),
        rendered=(rendered,),
        reconciliation=DocsReconciliation(),
    )

    assert artifact.target_root == "."
    assert artifact.plan.pages == (planned,)
    assert artifact.rendered == (rendered,)
    assert artifact.reconciliation.is_consistent


def test_build_documentation_for_empty_generated_registry() -> None:
    build = build_documentation(
        registry=GeneratedRegistry(),
    )

    assert build.plan.pages == ()
    assert build.rendered == ()
    assert build.reconciliation.is_consistent


def test_build_documentation_for_one_generated_element() -> None:
    registry = GeneratedRegistry(
        records=(
            GeneratedElementRecord(
                category="example",
                name="order",
                rel_dir="data/export/elements/example/order",
                compact_id="ar.example.order",
            ),
        )
    )

    build = build_documentation(registry=registry)

    assert len(build.plan.pages) == 1
    assert len(build.rendered) == 1
    assert build.reconciliation.is_consistent
