"""WF02 command entry points used by helper dispatcher."""

from __future__ import annotations

from collections.abc import Callable

from wf02_test_generation_common import (
    auth_module_fixture,
    create_trusted_plan,
    create_wf02_action,
    setup_lifecycle,
    validated_relative_test_path,
)
from wf02_test_generation_lifecycle import assert_rejected_generated_path

from cleveragents.domain.models.core.automation_profile import BUILTIN_PROFILES
from cleveragents.domain.models.core.plan import PlanPhase, ProcessingState


def wf02_trusted_lifecycle() -> None:
    """Verify trusted profile auto-runs strategize+execute only."""
    lifecycle = setup_lifecycle()
    create_wf02_action(lifecycle)
    plan_id = create_trusted_plan(lifecycle)

    trusted_profile = BUILTIN_PROFILES["trusted"]
    # Spec-aligned task-threshold fields (legacy phase names were removed).
    assert trusted_profile.decompose_task == 0.0
    assert trusted_profile.create_tool == 0.0
    assert trusted_profile.select_tool == 1.0

    plan = lifecycle.try_auto_run(plan_id)
    assert plan.phase == PlanPhase.EXECUTE, (
        f"Expected execute phase for trusted profile, got {plan.phase.value}"
    )
    assert plan.state == ProcessingState.COMPLETE, (
        "Expected execute/complete after trusted auto-run"
    )
    assert plan.automation_profile is not None
    assert plan.automation_profile.profile_name == "trusted"

    print("wf02-trusted-lifecycle-ok")


def wf02_path_safety_guardrails() -> None:
    """Verify generated file guardrails reject unsafe output paths."""
    with auth_module_fixture() as (fixture_root, _):
        assert_rejected_generated_path(
            lambda rel: validated_relative_test_path(fixture_root, rel),
            "/tmp/test_abs.py",
            "must be relative",
        )
        assert_rejected_generated_path(
            lambda rel: validated_relative_test_path(fixture_root, rel),
            "../tests/test_escape.py",
            "escapes fixture root",
        )
        assert_rejected_generated_path(
            lambda rel: validated_relative_test_path(fixture_root, rel),
            "src/test_not_allowed.py",
            "must stay in tests/",
        )

    print("wf02-path-safety-guardrails-ok")


def command_map(
    mocked_generation_artifacts: Callable[[], None],
    coverage_validation: Callable[[], None],
) -> dict[str, Callable[[], None]]:
    """Build command map for helper dispatcher."""
    return {
        "trusted-lifecycle": wf02_trusted_lifecycle,
        "mocked-generation-artifacts": mocked_generation_artifacts,
        "coverage-validation": coverage_validation,
        "path-safety-guardrails": wf02_path_safety_guardrails,
    }
