from app.modules.agent.engine.orchestrator.models import (
    ExecutionPlan,
    OutputContract,
    PlanStep,
    RetryPolicy,
    RoutingMeta,
    Scenario,
    TaskConstraints,
    TaskSpec,
)
from app.modules.agent.engine.orchestrator.plan_validator import PlanValidator


def _task(*, allow_writes: bool) -> TaskSpec:
    return TaskSpec(
        task_id="t1",
        dialog_session_id="d1",
        rag_session_id="r1",
        mode="auto",
        user_message="hello",
        scenario=Scenario.GENERAL_QA,
        routing=RoutingMeta(domain_id="default", process_id="general", confidence=0.9, reason="test"),
        constraints=TaskConstraints(allow_writes=allow_writes, max_steps=10, max_retries_per_step=2, step_timeout_sec=60),
        output_contract=OutputContract(result_type="answer"),
    )


def test_plan_validator_rejects_write_step_when_not_allowed() -> None:
    plan = ExecutionPlan(
        plan_id="p1",
        task_id="t1",
        scenario=Scenario.GENERAL_QA,
        template_id="tmp",
        template_version="1.0",
        steps=[
            PlanStep(
                step_id="s1",
                title="write",
                action_id="collect_state",
                executor="function",
                side_effect="write",
                retry=RetryPolicy(max_attempts=1),
            )
        ],
    )

    errors = PlanValidator().validate(plan, _task(allow_writes=False))

    assert "write_step_not_allowed:s1" in errors
