import pytest
from httpx import AsyncClient

pytestmark = pytest.mark.asyncio


@pytest.fixture
async def test_session(client: AsyncClient, auth_headers: dict, test_tree: dict):
    """Create a test session from the test tree."""
    response = await client.post(
        "/api/v1/sessions",
        json={"tree_id": test_tree["id"]},
        headers=auth_headers,
    )
    assert response.status_code == 201, f"Failed to create session: {response.text}"
    return response.json()


@pytest.fixture
async def completed_session(client: AsyncClient, auth_headers: dict, test_session: dict):
    """Complete a session so it can be rated."""
    response = await client.post(
        f"/api/v1/sessions/{test_session['id']}/complete",
        json={"outcome": "resolved", "outcome_notes": "Test resolved"},
        headers=auth_headers,
    )
    assert response.status_code == 200, f"Failed to complete session: {response.text}"
    return response.json()


async def test_rate_session_success(client: AsyncClient, auth_headers: dict, completed_session: dict):
    """Rate a completed session with CSAT score."""
    response = await client.post(
        f"/api/v1/sessions/{completed_session['id']}/rate",
        json={"rating": 4, "comment": "Very helpful flow"},
        headers=auth_headers,
    )
    assert response.status_code == 201
    data = response.json()
    assert data["rating"] == 4
    assert data["comment"] == "Very helpful flow"


async def test_rate_session_no_comment(client: AsyncClient, auth_headers: dict, completed_session: dict):
    """Rate without a comment."""
    response = await client.post(
        f"/api/v1/sessions/{completed_session['id']}/rate",
        json={"rating": 5},
        headers=auth_headers,
    )
    assert response.status_code == 201
    data = response.json()
    assert data["rating"] == 5
    assert data["comment"] is None


async def test_rate_session_duplicate(client: AsyncClient, auth_headers: dict, completed_session: dict):
    """Cannot rate same session twice."""
    await client.post(
        f"/api/v1/sessions/{completed_session['id']}/rate",
        json={"rating": 4},
        headers=auth_headers,
    )
    response = await client.post(
        f"/api/v1/sessions/{completed_session['id']}/rate",
        json={"rating": 5},
        headers=auth_headers,
    )
    assert response.status_code == 409


async def test_rate_session_invalid_rating(client: AsyncClient, auth_headers: dict, completed_session: dict):
    """Rating must be 1-5."""
    response = await client.post(
        f"/api/v1/sessions/{completed_session['id']}/rate",
        json={"rating": 6},
        headers=auth_headers,
    )
    assert response.status_code == 422


async def test_rate_incomplete_session(client: AsyncClient, auth_headers: dict, test_session: dict):
    """Cannot rate a session that hasn't been completed."""
    response = await client.post(
        f"/api/v1/sessions/{test_session['id']}/rate",
        json={"rating": 4},
        headers=auth_headers,
    )
    assert response.status_code == 400


# --- Step Feedback Tests ---

@pytest.fixture
async def test_step(client: AsyncClient, auth_headers: dict):
    """Create a step in the step library."""
    response = await client.post(
        "/api/v1/steps",
        json={
            "title": "Test Step",
            "step_type": "action",
            "content": {
                "instructions": "Run the diagnostic command",
                "commands": [{"label": "Echo test", "command": "echo hello"}]
            },
        },
        headers=auth_headers,
    )
    assert response.status_code == 201, f"Failed to create step: {response.text}"
    return response.json()


async def test_step_feedback_thumbs_up(client: AsyncClient, auth_headers: dict, test_step: dict, test_session: dict):
    """Submit thumbs-up feedback for a step."""
    response = await client.post(
        f"/api/v1/steps/{test_step['id']}/feedback",
        json={"session_id": test_session["id"], "was_helpful": True},
        headers=auth_headers,
    )
    assert response.status_code == 201
    data = response.json()
    assert data["was_helpful"] is True
    assert data["status"] == "created"


async def test_step_feedback_thumbs_down(client: AsyncClient, auth_headers: dict, test_step: dict, test_session: dict):
    """Submit thumbs-down feedback for a step."""
    response = await client.post(
        f"/api/v1/steps/{test_step['id']}/feedback",
        json={"session_id": test_session["id"], "was_helpful": False},
        headers=auth_headers,
    )
    assert response.status_code == 201
    data = response.json()
    assert data["was_helpful"] is False
    assert data["status"] == "created"


async def test_step_feedback_toggle(client: AsyncClient, auth_headers: dict, test_step: dict, test_session: dict):
    """Submitting again for same step+session updates the rating."""
    await client.post(
        f"/api/v1/steps/{test_step['id']}/feedback",
        json={"session_id": test_session["id"], "was_helpful": True},
        headers=auth_headers,
    )
    response = await client.post(
        f"/api/v1/steps/{test_step['id']}/feedback",
        json={"session_id": test_session["id"], "was_helpful": False},
        headers=auth_headers,
    )
    assert response.status_code == 201
    data = response.json()
    assert data["was_helpful"] is False
    assert data["status"] == "updated"


async def test_step_feedback_not_found(client: AsyncClient, auth_headers: dict, test_session: dict):
    """Feedback on non-existent step returns 404."""
    import uuid
    fake_id = str(uuid.uuid4())
    response = await client.post(
        f"/api/v1/steps/{fake_id}/feedback",
        json={"session_id": test_session["id"], "was_helpful": True},
        headers=auth_headers,
    )
    assert response.status_code == 404


# --- /ratings Alias Route Tests ---

async def test_ratings_alias_post(client: AsyncClient, auth_headers: dict, test_step: dict):
    """POST /steps/{id}/ratings works as alias for /rate."""
    response = await client.post(
        f"/api/v1/steps/{test_step['id']}/ratings",
        json={"rating": 4, "was_helpful": True},
        headers=auth_headers,
    )
    assert response.status_code == 201
    data = response.json()
    assert data["rating"] == 4


async def test_ratings_alias_put(client: AsyncClient, auth_headers: dict, test_step: dict):
    """PUT /steps/{id}/ratings works as alias for /rate."""
    # First create a rating
    await client.post(
        f"/api/v1/steps/{test_step['id']}/rate",
        json={"rating": 3, "was_helpful": True},
        headers=auth_headers,
    )
    # Update via alias
    response = await client.put(
        f"/api/v1/steps/{test_step['id']}/ratings",
        json={"rating": 5},
        headers=auth_headers,
    )
    assert response.status_code == 200
    data = response.json()
    assert data["rating"] == 5


async def test_ratings_alias_delete(client: AsyncClient, auth_headers: dict, test_step: dict):
    """DELETE /steps/{id}/ratings works as alias for /rate."""
    # First create a rating
    await client.post(
        f"/api/v1/steps/{test_step['id']}/rate",
        json={"rating": 4, "was_helpful": True},
        headers=auth_headers,
    )
    # Delete via alias
    response = await client.delete(
        f"/api/v1/steps/{test_step['id']}/ratings",
        headers=auth_headers,
    )
    assert response.status_code == 204
