"""Helper script for Robot Framework plan explain smoke tests."""

from __future__ import annotations

import sys
from collections.abc import Callable
from pathlib import Path

# Ensure src is importable when run from workspace root
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "src"))

from ulid import ULID

from cleveragents.cli.commands.plan import (
    _build_explain_dict,
    build_decision_tree,
)
from cleveragents.domain.models.core.decision import (
    ContextSnapshot,
    Decision,
    DecisionType,
    ResourceRef,
)

_PLAN_ID = str(ULID())


def _test_explain_format() -> None:
    """Create a decision and verify explain formatting."""
    decision = Decision(
        plan_id=_PLAN_ID,
        sequence_number=0,
        decision_type=DecisionType.PROMPT_DEFINITION,
        question="What to build?",
        chosen_option="REST API",
        rationale="Simple and well-understood",
        context_snapshot=ContextSnapshot(
            hot_context_hash="sha256:robot_test",
            relevant_resources=[
                ResourceRef(resource_id=str(ULID()), path="src/app.py"),
            ],
        ),
    )
    data = _build_explain_dict(
        decision,
        show_context=True,
        show_reasoning=True,
    )
    assert "decision_id" in data
    assert "context_snapshot" in data
    assert "rationale" in data
    assert "alternatives_considered" in data
    assert data["question"] == "What to build?"
    print("plan-explain-ok")


def _test_tree_build() -> None:
    """Create decisions and build a tree."""
    root_id = str(ULID())
    child_id = str(ULID())
    decisions = [
        Decision(
            decision_id=root_id,
            plan_id=_PLAN_ID,
            sequence_number=0,
            decision_type=DecisionType.PROMPT_DEFINITION,
            question="Root?",
            chosen_option="Yes",
        ),
        Decision(
            decision_id=child_id,
            plan_id=_PLAN_ID,
            parent_decision_id=root_id,
            sequence_number=1,
            decision_type=DecisionType.STRATEGY_CHOICE,
            question="Child?",
            chosen_option="Also yes",
        ),
    ]
    tree = build_decision_tree(decisions)
    assert len(tree) == 1
    assert len(tree[0]["children"]) == 1
    print("plan-tree-ok")


_TESTS: dict[str, Callable[[], None]] = {
    "explain_format": _test_explain_format,
    "tree_build": _test_tree_build,
}

if __name__ == "__main__":
    if len(sys.argv) < 2:
        print(f"Usage: {sys.argv[0]} <test_name>")
        print(f"Available tests: {', '.join(sorted(_TESTS))}")
        sys.exit(1)

    test_name = sys.argv[1]
    if test_name not in _TESTS:
        print(f"Unknown test: {test_name}")
        print(f"Available: {', '.join(sorted(_TESTS))}")
        sys.exit(1)

    _TESTS[test_name]()
