"""High-value tests for benchmarks/metrics/visualization.py.

Covers matplotlib unavailable path, figure creation, file saving,
Pareto frontier edge cases, and parameter importance sorting.
"""

import pytest
from unittest.mock import patch


class TestPlotOptimizationHistory:
    """Tests for plot_optimization_history."""

    def test_returns_none_when_matplotlib_unavailable(self):
        """Returns None when MATPLOTLIB_AVAILABLE is False."""
        with patch(
            "local_deep_research.benchmarks.metrics.visualization.MATPLOTLIB_AVAILABLE",
            False,
        ):
            from local_deep_research.benchmarks.metrics.visualization import (
                plot_optimization_history,
            )

            result = plot_optimization_history([0.5, 0.6], [0.5, 0.6])
            assert result is None

    def test_returns_figure_with_valid_data(self):
        """Returns a matplotlib Figure with valid inputs."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_optimization_history,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        fig = plot_optimization_history([0.3, 0.5, 0.7], [0.3, 0.5, 0.7])
        assert fig is not None
        import matplotlib.pyplot as plt

        plt.close(fig)

    def test_saves_to_file_when_output_file_given(self, tmp_path):
        """Saves figure to disk when output_file is provided."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_optimization_history,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        out = str(tmp_path / "history.png")
        fig = plot_optimization_history([0.1, 0.2], [0.1, 0.2], output_file=out)
        assert fig is not None
        assert (tmp_path / "history.png").exists()
        import matplotlib.pyplot as plt

        plt.close(fig)

    def test_custom_title(self):
        """Custom title is set on the axes."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_optimization_history,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        fig = plot_optimization_history([0.5], [0.5], title="Custom Title")
        assert fig is not None
        ax = fig.get_axes()[0]
        assert ax.get_title() == "Custom Title"
        import matplotlib.pyplot as plt

        plt.close(fig)

    def test_single_trial(self):
        """Works with a single trial value."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_optimization_history,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        fig = plot_optimization_history([0.9], [0.9])
        assert fig is not None
        import matplotlib.pyplot as plt

        plt.close(fig)


class TestPlotParameterImportance:
    """Tests for plot_parameter_importance."""

    def test_returns_none_when_matplotlib_unavailable(self):
        """Returns None when MATPLOTLIB_AVAILABLE is False."""
        with patch(
            "local_deep_research.benchmarks.metrics.visualization.MATPLOTLIB_AVAILABLE",
            False,
        ):
            from local_deep_research.benchmarks.metrics.visualization import (
                plot_parameter_importance,
            )

            result = plot_parameter_importance(["a", "b"], [0.3, 0.7])
            assert result is None

    def test_sorts_by_importance(self):
        """Parameters are sorted by importance value."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_parameter_importance,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        fig = plot_parameter_importance(["low", "high", "mid"], [0.1, 0.9, 0.5])
        assert fig is not None
        import matplotlib.pyplot as plt

        plt.close(fig)

    def test_saves_to_file(self, tmp_path):
        """Saves figure when output_file provided."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_parameter_importance,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        out = str(tmp_path / "importance.png")
        fig = plot_parameter_importance(["a"], [0.5], output_file=out)
        assert (tmp_path / "importance.png").exists()
        import matplotlib.pyplot as plt

        plt.close(fig)


class TestPlotQualityVsSpeed:
    """Tests for plot_quality_vs_speed."""

    def test_returns_none_when_matplotlib_unavailable(self):
        """Returns None when MATPLOTLIB_AVAILABLE is False."""
        with patch(
            "local_deep_research.benchmarks.metrics.visualization.MATPLOTLIB_AVAILABLE",
            False,
        ):
            from local_deep_research.benchmarks.metrics.visualization import (
                plot_quality_vs_speed,
            )

            result = plot_quality_vs_speed([0.5], [0.5])
            assert result is None

    def test_pareto_frontier_with_three_points(self):
        """Pareto frontier calculation works with 3+ points."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_quality_vs_speed,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        fig = plot_quality_vs_speed([0.5, 0.8, 0.3, 0.9], [0.9, 0.5, 0.3, 0.8])
        assert fig is not None
        import matplotlib.pyplot as plt

        plt.close(fig)

    def test_two_points_skips_pareto(self):
        """With only 2 points, Pareto frontier is skipped."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_quality_vs_speed,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        fig = plot_quality_vs_speed([0.5, 0.8], [0.5, 0.8])
        assert fig is not None
        import matplotlib.pyplot as plt

        plt.close(fig)

    def test_saves_to_file(self, tmp_path):
        """Saves figure when output_file provided."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_quality_vs_speed,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        out = str(tmp_path / "qvs.png")
        fig = plot_quality_vs_speed(
            [0.5, 0.8, 0.9], [0.9, 0.5, 0.8], output_file=out
        )
        assert (tmp_path / "qvs.png").exists()
        import matplotlib.pyplot as plt

        plt.close(fig)

    def test_identical_points(self):
        """Handles identical quality/speed scores gracefully."""
        from local_deep_research.benchmarks.metrics.visualization import (
            plot_quality_vs_speed,
            MATPLOTLIB_AVAILABLE,
        )

        if not MATPLOTLIB_AVAILABLE:
            pytest.skip("matplotlib not installed")

        fig = plot_quality_vs_speed([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
        assert fig is not None
        import matplotlib.pyplot as plt

        plt.close(fig)
