"""Additional Behave steps for TDD validation scenarios for PlanGenerationGraph.

The shared step file ``plan_generation_langgraph_coverage_steps.py`` already
provides ``@given("I have a langgraph PlanGenerationGraph instance")`` and
``@then('the langgraph validation status should be "{status}"')`` — those are
NOT redefined here to avoid behave's ``AmbiguousStep`` error. This file only
contributes the ``@when`` step that feeds the docstring as the LLM's
validation response so each scenario can assert ``_validate``'s interpretation
of a specific PASS/FAIL signal.
"""

from __future__ import annotations

from typing import Any

from behave import when
from langchain_community.llms import FakeListLLM

from cleveragents.agents.plan_generation import PlanGenerationGraph
from cleveragents.domain.models.core import Change, OperationType


@when("I execute the langgraph validate node with generated code:")
def step_execute_langgraph_validate_with_code(context: Any) -> None:
    """Execute validate node using the step docstring as the LLM validation response.

    ``FakeListLLM`` returns its ``responses`` in round-robin order regardless of
    input, so the docstring content represents what the LLM emits when
    ``_validate()`` invokes it — i.e. the PASS/FAIL verdict text — not the
    generated code itself. The ``Change`` payload uses a fixed code string so
    the test focuses purely on how ``_validate()`` parses the LLM response.
    """
    validation_response = (context.text or "").strip()
    llm = FakeListLLM(responses=[validation_response])
    context.graph = PlanGenerationGraph(llm=llm)
    change = Change(
        id=None,
        plan_id=1,
        file_path="generated.py",
        operation=OperationType.CREATE,
        original_content=None,
        new_content="def some_function(): pass",
        applied=False,
        applied_at=None,
        new_path=None,
    )
    state = {"generated_changes": [change]}
    context.node_result = context.graph._validate(state)
