"""Generate the code reference pages and navigation.

This script is executed by mkdocs-gen-files during the docs build.
It walks the `src/cleveragents` package tree and generates a `.md`
page for every public Python module. Each page contains an
mkdocstrings `::: module.path` directive so the API docs are
rendered from live docstrings.

A `SUMMARY.md` file is also written so that mkdocs-literate-nav can
build the Reference section of the sidebar automatically.
"""

from pathlib import Path

import mkdocs_gen_files

nav = mkdocs_gen_files.Nav()

SRC_DIR = Path("src")
PACKAGE = "cleveragents"


def _is_proper_package(py_file: Path) -> bool:
    """Return True only if every ancestor directory (up to SRC_DIR) has an __init__.py.

    This filters out stray .py files that live in directories without
    __init__.py (e.g. templates/renderer.py) which griffe cannot resolve.
    """
    parent = py_file.parent
    while parent != SRC_DIR:
        if not (parent / "__init__.py").exists():
            return False
        parent = parent.parent
    return True


# Walk every .py file under src/cleveragents
for path in sorted(SRC_DIR.rglob("*.py")):
    # Only include modules that belong to proper Python packages
    if not _is_proper_package(path):
        continue

    # Compute the dotted module path and the docs destination path
    module_path = path.relative_to(SRC_DIR).with_suffix(
        ""
    )  # e.g. cleveragents/domain/models/core/plan
    doc_path = path.relative_to(SRC_DIR).with_suffix(
        ".md"
    )  # e.g. cleveragents/domain/models/core/plan.md
    full_doc_path = Path(
        "reference", doc_path
    )  # e.g. reference/cleveragents/domain/models/core/plan.md

    parts = tuple(module_path.parts)

    # Skip __main__ — not useful API surface
    if parts[-1] == "__main__":
        continue

    # For __init__.py files, use the package itself as the page
    if parts[-1] == "__init__":
        parts = parts[:-1]
        doc_path = doc_path.with_name("index.md")
        full_doc_path = full_doc_path.with_name("index.md")

    # Build the nav entry
    nav_parts = parts  # e.g. ("cleveragents", "domain", "models", "core", "plan")
    nav[nav_parts] = doc_path.as_posix()

    # Write the stub .md page with the mkdocstrings directive
    dotted = ".".join(parts)  # e.g. "cleveragents.domain.models.core.plan"
    with mkdocs_gen_files.open(full_doc_path, "w") as fd:
        fd.write(f"# `{dotted}`\n\n")
        fd.write(f"::: {dotted}\n")

    # Tell mkdocs-gen-files where the edit link should point
    mkdocs_gen_files.set_edit_path(full_doc_path, path.as_posix())

# Include manually-authored reference pages that live alongside the
# auto-generated API docs.
nav[("actors_schema",)] = "actors_schema.md"
nav[("automation_profile_service",)] = "automation_profile_service.md"
nav[("automation_profiles",)] = "automation_profiles.md"
nav[("changeset_model",)] = "changeset_model.md"
nav[("cli_system_commands",)] = "cli_system_commands.md"
nav[("database_schema",)] = "database_schema.md"
nav[("diagnostics_checks",)] = "diagnostics_checks.md"
nav[("project_context_cli",)] = "project_context_cli.md"
nav[("project_context_policy",)] = "project_context_policy.md"
nav[("resource_dag",)] = "resource_dag.md"
nav[("resource_types_builtin",)] = "resource_types_builtin.md"
nav[("audit_logging",)] = "audit_logging.md"
nav[("cleanup",)] = "cleanup.md"
nav[("resource_cli",)] = "resource_cli.md"
nav[("secrets_handling",)] = "secrets_handling.md"
nav[("session_model",)] = "session_model.md"
nav[("skill_cli",)] = "skill_cli.md"
nav[("skill_resolution",)] = "skill_resolution.md"
nav[("skills_context",)] = "skills_context.md"
nav[("skills_inline",)] = "skills_inline.md"
nav[("skills_protocol",)] = "skills_protocol.md"
nav[("tool_bindings",)] = "tool_bindings.md"
nav[("tool_cli",)] = "tool_cli.md"
nav[("tool_model",)] = "tool_model.md"
nav[("actors_examples",)] = "actors_examples.md"
nav[("actors_loading",)] = "actors_loading.md"
nav[("change_tracking",)] = "change_tracking.md"
nav[("config_cli",)] = "config_cli.md"
nav[("output_rendering",)] = "output_rendering.md"
nav[("plan_apply",)] = "plan_apply.md"
nav[("plan_execute",)] = "plan_execute.md"
nav[("security_eval",)] = "security_eval.md"
nav[("session_cli",)] = "session_cli.md"
nav[("skill_registry",)] = "skill_registry.md"
nav[("skills_file",)] = "skills_file.md"
nav[("skills_git",)] = "skills_git.md"
nav[("skills_search",)] = "skills_search.md"
nav[("tool_router",)] = "tool_router.md"
nav[("action_cli",)] = "action_cli.md"
nav[("actor_runtime",)] = "actor_runtime.md"
nav[("config_resolution",)] = "config_resolution.md"
nav[("decision_correction",)] = "decision_correction.md"
nav[("decision_model",)] = "decision_model.md"
nav[("invariants",)] = "invariants.md"
nav[("lsp",)] = "lsp.md"
nav[("phase_reversion",)] = "phase_reversion.md"
nav[("plan_cli",)] = "plan_cli.md"
nav[("repl",)] = "repl.md"
nav[("resource_handlers",)] = "resource_handlers.md"
nav[("resources",)] = "resources.md"
nav[("actor_cli",)] = "actor_cli.md"
nav[("actor_compiler",)] = "actor_compiler.md"
nav[("actor_config",)] = "actor_config.md"
nav[("actor_hierarchy",)] = "actor_hierarchy.md"
nav[("async_safety",)] = "async_safety.md"
nav[("changeset",)] = "changeset.md"
nav[("concurrency",)] = "concurrency.md"
nav[("cost_controls",)] = "cost_controls.md"
nav[("definition_of_done",)] = "definition_of_done.md"
nav[("diff_review",)] = "diff_review.md"
nav[("error_handling",)] = "error_handling.md"
nav[("error_recovery",)] = "error_recovery.md"
nav[("template_security",)] = "template_security.md"
nav[("permissions",)] = "permissions.md"
nav[("validation_pipeline",)] = "validation_pipeline.md"
nav[("agent_skills",)] = "agent_skills.md"
nav[("di",)] = "di.md"
nav[("mcp_adapter",)] = "mcp_adapter.md"
nav[("read_only_actions",)] = "read_only_actions.md"
nav[("sandbox",)] = "sandbox.md"
nav[("semantic_validation",)] = "semantic_validation.md"
nav[("skill_refresh",)] = "skill_refresh.md"
nav[("subplans",)] = "subplans.md"
nav[("checkpointing",)] = "checkpointing.md"
nav[("crp",)] = "crp.md"
nav[("safety_profile",)] = "safety_profile.md"
nav[("server_client_stubs",)] = "server_client_stubs.md"
nav[("subplan_service",)] = "subplan_service.md"
nav[("acms_backends",)] = "acms_backends.md"
nav[("context_tiers",)] = "context_tiers.md"
nav[("decision_service",)] = "decision_service.md"
nav[("devcontainer_resources",)] = "devcontainer_resources.md"
nav[("large_project_decomposition",)] = "large_project_decomposition.md"
nav[("multi_project_plans",)] = "multi_project_plans.md"
nav[("observability",)] = "observability.md"
nav[("safety_profiles",)] = "safety_profiles.md"
nav[("semantic_validation_coverage",)] = "semantic_validation_coverage.md"
nav[("skeleton_compressor",)] = "skeleton_compressor.md"
nav[("acms",)] = "acms.md"
nav[("async_architecture",)] = "async_architecture.md"
nav[("uko",)] = "uko.md"
nav[("context_strategies",)] = "context_strategies.md"
nav[("event_bus",)] = "event_bus.md"
nav[("lsp_stub",)] = "lsp_stub.md"
nav[("scoped_backend_view",)] = "scoped_backend_view.md"
nav[("acms_fusion",)] = "acms_fusion.md"
nav[("context_indexing",)] = "context_indexing.md"
nav[("resource_type_inheritance",)] = "resource_type_inheritance.md"
nav[("temporal_data_model",)] = "temporal_data_model.md"
nav[("uko_indexer",)] = "uko_indexer.md"
nav[("a2a",)] = "a2a.md"
nav[("retry_policy",)] = "retry_policy.md"
nav[("execution_environment",)] = "execution_environment.md"

# Write the literate-nav summary file for the Reference section
with mkdocs_gen_files.open("reference/SUMMARY.md", "w") as nav_file:
    nav_file.writelines(nav.build_literate_nav())
