"""
E2E Acceptance Tests: US-004 Turn Counting Integration

PERSONA: Marcus (Senior Developer)
STORY: As a senior developer, I want TurnCounter integrated into DESOrchestrator
       so that turn counts are tracked and persisted during command execution.

PROBLEM: TurnCounter exists but isn't wired into the orchestrator's execution loop.
         Without integration, turn counts aren't tracked during real command execution.

SOLUTION: Wire TurnCounter into DESOrchestrator.execute_step() method to:
          - Initialize counter at phase start
          - Increment on each agent call iteration
          - Persist to step file in real-time
          - Restore state on resume

BUSINESS VALUE:
- Track execution progress through turn counting
- Persist turn counts for audit trail
- Enable execution resume with preserved state
- Support timeout monitoring

SOURCE:
- docs/feature/des-us006/steps/02-01.json (Step 02-01)
"""


class TestTurnCountingIntegration:
    """
    E2E acceptance tests for TurnCounter integration with DESOrchestrator.

    Validates that turn counting happens during actual command execution
    and persists to step file for audit trail.
    """

    def test_scenario_011_turn_count_increments_during_execution(
        self,
        tmp_project_root,
        minimal_step_file,
        des_orchestrator,
        in_memory_filesystem,
    ):
        """
        AC-004.1: Turn count increments on each agent call during execute_step().

        GIVEN /nw:execute or /nw:develop command invoked
        WHEN orchestrator executes step via execute_step()
        AND sub-agent makes multiple invocation iterations
        THEN turn_count field increments for each iteration
        AND turn_count persisted to step file phase_execution_log

        Business Context:
        Marcus runs `/nw:execute @software-crafter steps/01-01.json`.
        The orchestrator must track how many turns the agent uses during
        execution and persist this to the step file for audit trail.

        This enables:
        1. Progress tracking (agent at turn 15, expected ~25 for this phase)
        2. Timeout monitoring (agent approaching turn limit)
        3. Resume capability (restore turn count on interrupted execution)
        """
        # GIVEN: /nw:execute command with step file

        command = "/nw:execute"
        agent = "@software-crafter"
        step_file_path = str(minimal_step_file.relative_to(tmp_project_root))

        # Create initial step file structure with required fields
        step_data = {
            "task_id": "02-01",
            "project_id": "test-project",
            "state": {
                "status": "IN_PROGRESS",
                "started_at": "2026-01-26T10:00:00Z",
                "completed_at": None,
            },
            "tdd_cycle": {
                "max_turns": 50,
                "duration_minutes": 30,
                "total_extensions_minutes": 0,
                "phase_execution_log": [
                    {
                        "phase_name": "PREPARE",
                        "phase_index": 0,
                        "status": "IN_PROGRESS",
                        "turn_count": 0,
                    }
                ],
            },
        }

        # Write initial data to in-memory filesystem
        in_memory_filesystem.write_json(minimal_step_file, step_data)

        # WHEN: Orchestrator executes step (simulating agent iterations)
        expected_iterations = 3

        result = des_orchestrator.execute_step(
            command=command,
            agent=agent,
            step_file=step_file_path,
            project_root=tmp_project_root,
            simulated_iterations=expected_iterations,  # Test parameter
        )

        # THEN: Turn count incremented for each iteration
        assert result.turn_count == expected_iterations, (
            f"Expected turn_count={expected_iterations} after {expected_iterations} iterations, "
            f"got {result.turn_count}"
        )

        # AND: Turn count persisted to step file
        step_data = in_memory_filesystem.read_json(minimal_step_file)

        # Verify turn_count in phase_execution_log
        phase_log = step_data["tdd_cycle"]["phase_execution_log"]
        current_phase = next(
            (p for p in phase_log if p["status"] == "IN_PROGRESS"), None
        )

        assert current_phase is not None, "No IN_PROGRESS phase found in execution log"
        assert "turn_count" in current_phase, "turn_count field missing from phase log"
        assert current_phase["turn_count"] == expected_iterations, (
            f"turn_count in step file should be {expected_iterations}, "
            f"got {current_phase.get('turn_count')}"
        )
