"""Unit tests for extract_custom_report_data transformer function.

Tests the extraction and flattening of custom report data from API bug responses.
"""

import json

import pytest

from testio_mcp.transformers.bug_transformers import extract_custom_report_data


@pytest.mark.unit
class TestExtractCustomReportData:
    """Tests for extract_custom_report_data()."""

    def test_extracts_flat_dict_from_report_data(self) -> None:
        """Verify key/value pairs are flattened into a JSON string dict."""
        api_bug = {
            "report": {
                "content": {
                    "data": [
                        {"key": "accuracy", "value": "8"},
                        {"key": "guardrails", "value": "No"},
                        {"key": "course", "value": "Physics B"},
                    ]
                },
                "custom_report_configuration_id": 82,
            }
        }

        result = extract_custom_report_data(api_bug, exclude_fields=set())

        assert result is not None
        data = json.loads(result["report_data"])
        assert data == {"accuracy": "8", "guardrails": "No", "course": "Physics B"}
        assert result["configuration_id"] == 82

    def test_excludes_specified_fields(self) -> None:
        """Verify excluded fields are not present in output."""
        api_bug = {
            "report": {
                "content": {
                    "data": [
                        {"key": "accuracy", "value": "8"},
                        {"key": "plain_text_interactions", "value": "very long text..."},
                    ]
                }
            }
        }

        result = extract_custom_report_data(api_bug, exclude_fields={"plain_text_interactions"})

        assert result is not None
        data = json.loads(result["report_data"])
        assert "accuracy" in data
        assert "plain_text_interactions" not in data

    def test_returns_none_for_no_report(self) -> None:
        """Verify returns None when no report key exists."""
        assert extract_custom_report_data({}) is None
        assert extract_custom_report_data({"report": None}) is None

    def test_returns_none_for_no_content(self) -> None:
        """Verify returns None when report has no content."""
        api_bug = {"report": {"other_field": "value"}}
        assert extract_custom_report_data(api_bug) is None

    def test_returns_none_for_no_data_array(self) -> None:
        """Verify returns None when content has no data array."""
        api_bug = {"report": {"content": {"other": "value"}}}
        assert extract_custom_report_data(api_bug) is None

    def test_returns_none_for_empty_data_array(self) -> None:
        """Verify returns None when data array is empty."""
        api_bug = {"report": {"content": {"data": []}}}
        assert extract_custom_report_data(api_bug) is None

    def test_returns_none_when_all_fields_excluded(self) -> None:
        """Verify returns None when all fields are in exclude set."""
        api_bug = {
            "report": {
                "content": {
                    "data": [
                        {"key": "plain_text_interactions", "value": "text"},
                    ]
                }
            }
        }

        result = extract_custom_report_data(api_bug, exclude_fields={"plain_text_interactions"})
        assert result is None

    def test_handles_missing_value_gracefully(self) -> None:
        """Verify items without 'value' key default to empty string."""
        api_bug = {
            "report": {
                "content": {
                    "data": [
                        {"key": "accuracy"},
                    ]
                }
            }
        }

        result = extract_custom_report_data(api_bug, exclude_fields=set())
        assert result is not None
        data = json.loads(result["report_data"])
        assert data["accuracy"] == ""

    def test_skips_non_dict_items_in_data_array(self) -> None:
        """Verify non-dict items in data array are skipped."""
        api_bug = {
            "report": {
                "content": {
                    "data": [
                        "not a dict",
                        42,
                        {"key": "accuracy", "value": "8"},
                    ]
                }
            }
        }

        result = extract_custom_report_data(api_bug, exclude_fields=set())
        assert result is not None
        data = json.loads(result["report_data"])
        assert data == {"accuracy": "8"}

    def test_skips_items_without_key(self) -> None:
        """Verify dict items without 'key' field are skipped."""
        api_bug = {
            "report": {
                "content": {
                    "data": [
                        {"value": "orphan value"},
                        {"key": "accuracy", "value": "8"},
                    ]
                }
            }
        }

        result = extract_custom_report_data(api_bug, exclude_fields=set())
        assert result is not None
        data = json.loads(result["report_data"])
        assert data == {"accuracy": "8"}

    def test_configuration_id_omitted_when_absent(self) -> None:
        """Verify configuration_id is not in result when not in API response."""
        api_bug = {"report": {"content": {"data": [{"key": "accuracy", "value": "8"}]}}}

        result = extract_custom_report_data(api_bug, exclude_fields=set())
        assert result is not None
        assert "configuration_id" not in result

    def test_handles_non_dict_report(self) -> None:
        """Verify returns None when report is not a dict."""
        assert extract_custom_report_data({"report": "string"}) is None
        assert extract_custom_report_data({"report": 42}) is None

    def test_handles_non_dict_content(self) -> None:
        """Verify returns None when content is not a dict."""
        api_bug = {"report": {"content": "string"}}
        assert extract_custom_report_data(api_bug) is None

    def test_handles_non_list_data(self) -> None:
        """Verify returns None when data is not a list."""
        api_bug = {"report": {"content": {"data": "not a list"}}}
        assert extract_custom_report_data(api_bug) is None
