"""Tests for get_batch_status error formatting in batch tool output."""

import pytest
from httpx import Response

from platform_2step_mcp.tools.batches import handle_batch_tool


class TestGetBatchStatusErrorFormat:
    """Tests that get_batch_status formats error data lines correctly."""

    @pytest.mark.anyio
    async def test_partial_batch_shows_error_details(self, client, mock_api):
        mock_api.get("/api/v1/batches/batch-partial").mock(
            return_value=Response(
                200,
                json={
                    "batch_id": "batch-partial",
                    "status": "partial",
                    "description": "Test batch",
                    "operations_count": 2,
                    "executed_count": 1,
                    "failed_count": 1,
                    "expires_at": "2024-01-15T10:30:00Z",
                    "confirmable": False,
                    "operations": [
                        {
                            "position": 0,
                            "operation": "create",
                            "resource": "category",
                            "status": "executed",
                        },
                        {
                            "position": 1,
                            "operation": "create",
                            "resource": "category",
                            "status": "failed",
                            "error": {
                                "code": "validation_error",
                                "message": "Name already taken",
                                "status": 422,
                            },
                        },
                    ],
                },
            )
        )

        result = await handle_batch_tool(
            "get_batch_status", {"batch_id": "batch-partial"}, client
        )

        text = result[0].text
        assert "error_code: validation_error" in text
        assert "error_message: Name already taken" in text
        assert "http_status: 422" in text

    @pytest.mark.anyio
    async def test_successful_batch_shows_no_error_lines(self, client, mock_api):
        mock_api.get("/api/v1/batches/batch-ok").mock(
            return_value=Response(
                200,
                json={
                    "batch_id": "batch-ok",
                    "status": "confirmed",
                    "operations_count": 1,
                    "executed_count": 1,
                    "failed_count": 0,
                    "expires_at": "2024-01-15T10:30:00Z",
                    "confirmable": False,
                    "operations": [
                        {
                            "position": 0,
                            "operation": "create",
                            "resource": "category",
                            "status": "executed",
                        },
                    ],
                },
            )
        )

        result = await handle_batch_tool(
            "get_batch_status", {"batch_id": "batch-ok"}, client
        )

        text = result[0].text
        assert "error_code" not in text
        assert "error_message" not in text
        assert "http_status" not in text

    @pytest.mark.anyio
    async def test_operation_without_error_dict_handled_cleanly(self, client, mock_api):
        mock_api.get("/api/v1/batches/batch-no-error").mock(
            return_value=Response(
                200,
                json={
                    "batch_id": "batch-no-error",
                    "status": "partial",
                    "operations_count": 1,
                    "executed_count": 0,
                    "failed_count": 1,
                    "expires_at": "2024-01-15T10:30:00Z",
                    "confirmable": False,
                    "operations": [
                        {
                            "position": 0,
                            "operation": "create",
                            "resource": "category",
                            "status": "failed",
                        },
                    ],
                },
            )
        )

        result = await handle_batch_tool(
            "get_batch_status", {"batch_id": "batch-no-error"}, client
        )

        text = result[0].text
        assert "[FAILED]" in text
        assert "error_code" not in text

    @pytest.mark.anyio
    async def test_error_with_non_dict_value_ignored(self, client, mock_api):
        mock_api.get("/api/v1/batches/batch-bad-error").mock(
            return_value=Response(
                200,
                json={
                    "batch_id": "batch-bad-error",
                    "status": "partial",
                    "operations_count": 1,
                    "executed_count": 0,
                    "failed_count": 1,
                    "expires_at": "2024-01-15T10:30:00Z",
                    "confirmable": False,
                    "operations": [
                        {
                            "position": 0,
                            "operation": "create",
                            "resource": "category",
                            "status": "failed",
                            "error": "just a string",
                        },
                    ],
                },
            )
        )

        result = await handle_batch_tool(
            "get_batch_status", {"batch_id": "batch-bad-error"}, client
        )

        text = result[0].text
        assert "error_code" not in text
