"""Tests for stepper API routes."""

from __future__ import annotations

import pytest
from httpx import ASGITransport, AsyncClient

from api.main import create_app


@pytest.mark.asyncio
async def test_set_breakpoint():
    """Test setting a breakpoint."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post(
            "/api/sessions/test_session_123/breakpoints",
            params={
                "breakpoint_type": "event_type",
                "condition_value": "decision",
                "description": "Break on decisions",
            },
        )
        # Will return 404 if session doesn't exist, but we test the endpoint structure
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_clear_breakpoint():
    """Test clearing a breakpoint."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        session_id = "test_session_123"
        breakpoint_id = "test_breakpoint_123"

        response = await client.delete(f"/api/sessions/{session_id}/breakpoints/{breakpoint_id}")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_clear_all_breakpoints():
    """Test clearing all breakpoints."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.delete("/api/sessions/test_session_123/breakpoints")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_list_breakpoints():
    """Test listing breakpoints."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.get("/api/sessions/test_session_123/breakpoints")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_step_execution():
    """Test stepping through execution."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post(
            "/api/sessions/test_session_123/step",
            params={"action": "step_into"},
        )
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_step_with_target():
    """Test stepping to specific event."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post(
            "/api/sessions/test_session_123/step",
            params={"action": "run_to", "target_event_id": "event_123"},
        )
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_get_stepper_state():
    """Test getting stepper state."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.get("/api/sessions/test_session_123/state")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_create_branch():
    """Test creating a branch."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post(
            "/api/sessions/test_session_123/branch",
            params={
                "name": "Test Branch",
                "parent_event_id": "event_123",
                "description": "Test branch description",
            },
        )
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_list_branches():
    """Test listing branches."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.get("/api/sessions/test_session_123/branches")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_get_branch():
    """Test getting a specific branch."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        session_id = "test_session_123"
        branch_id = "test_branch_123"

        response = await client.get(f"/api/sessions/{session_id}/branches/{branch_id}")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_delete_branch():
    """Test deleting a branch."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        session_id = "test_session_123"
        branch_id = "test_branch_123"

        response = await client.delete(f"/api/sessions/{session_id}/branches/{branch_id}")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_reset_stepper():
    """Test resetting stepper."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.post("/api/sessions/test_session_123/stepper/reset")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_get_execution_context():
    """Test getting execution context."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        response = await client.get("/api/sessions/test_session_123/stepper/context")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_breakpoint_workflow():
    """Test complete breakpoint workflow."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        session_id = "workflow_test_session"

        # Set breakpoint
        await client.post(
            f"/api/sessions/{session_id}/breakpoints",
            params={"breakpoint_type": "event_type", "condition_value": "decision"},
        )

        # List breakpoints
        response = await client.get(f"/api/sessions/{session_id}/breakpoints")
        assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_step_workflow():
    """Test step execution workflow."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        session_id = "step_test_session"

        # Try different step actions
        actions = ["step_into", "step_over", "step_out", "continue"]

        for action in actions:
            response = await client.post(
                f"/api/sessions/{session_id}/step",
                params={"action": action},
            )
            assert response.status_code in [200, 404]


@pytest.mark.asyncio
async def test_branch_workflow():
    """Test branch management workflow."""
    app = create_app()
    transport = ASGITransport(app=app)
    async with AsyncClient(transport=transport, base_url="http://test") as client:
        session_id = "branch_test_session"

        # Create branch
        await client.post(
            f"/api/sessions/{session_id}/branch",
            params={
                "name": "Test Branch",
                "parent_event_id": "event_123",
            },
        )

        # List branches
        response = await client.get(f"/api/sessions/{session_id}/branches")
        assert response.status_code in [200, 404]

        # Delete branch (if it was created)
        # This would need the actual branch_id from creation response
        branch_id = "test_branch_123"
        response = await client.delete(f"/api/sessions/{session_id}/branches/{branch_id}")
        assert response.status_code in [200, 404]
