"""High-value tests for benchmarks/comparison/results.py.

Covers Benchmark_results: add_result, get_all, get_best sorting,
file loading, and edge cases.
"""

import json
import pytest
from unittest.mock import patch

from local_deep_research.benchmarks.comparison.results import Benchmark_results


class TestBenchmarkResultsInit:
    """Test Benchmark_results initialization."""

    def test_default_results_file(self, tmp_path, monkeypatch):
        """Default results file is benchmark_results.json."""
        monkeypatch.chdir(tmp_path)
        br = Benchmark_results()
        assert br.results_file == "benchmark_results.json"

    def test_custom_results_file(self, tmp_path):
        """Custom results file is used."""
        f = tmp_path / "custom.json"
        f.write_text("[]")
        br = Benchmark_results(results_file=str(f))
        assert br.results_file == str(f)

    def test_empty_results_when_file_missing(self, tmp_path):
        """Missing file results in empty list."""
        br = Benchmark_results(results_file=str(tmp_path / "nonexistent.json"))
        assert br.results == []

    def test_loads_existing_results(self, tmp_path):
        """Existing file's results are loaded."""
        data = [{"model": "test", "accuracy_focused": 0.9}]
        f = tmp_path / "results.json"
        f.write_text(json.dumps(data))
        br = Benchmark_results(results_file=str(f))
        assert len(br.results) == 1
        assert br.results[0]["model"] == "test"


class TestBenchmarkResultsAddResult:
    """Test add_result method."""

    def test_add_result_appends_and_saves(self, tmp_path):
        """add_result appends to results and calls _save_results."""
        with patch.object(Benchmark_results, "_save_results"):
            br = Benchmark_results(results_file=str(tmp_path / "nonexist.json"))
            result = br.add_result(
                model="gpt-4",
                hardware="RTX 4090",
                accuracy_focused=0.95,
                accuracy_source=0.90,
                avg_time_per_question=5.0,
                context_window=128000,
                temperature=0.0,
                ldr_version="1.0",
                date_tested="2024-01-01",
                notes="test run",
            )
            assert result is True
            assert len(br.results) == 1
            assert br.results[0]["model"] == "gpt-4"
            assert br.results[0]["notes"] == "test run"

    def test_add_result_default_notes_empty(self, tmp_path):
        """Notes defaults to empty string."""
        with patch.object(Benchmark_results, "_save_results"):
            br = Benchmark_results(results_file=str(tmp_path / "nonexist.json"))
            br.add_result(
                "m", "h", 0.5, 0.5, 1.0, 8192, 0.0, "1.0", "2024-01-01"
            )
            assert br.results[0]["notes"] == ""


class TestBenchmarkResultsGetBest:
    """Test get_best sorting."""

    def _make_results(self, tmp_path):
        f = tmp_path / "results.json"
        data = [
            {
                "model": "fast",
                "accuracy_focused": 0.7,
                "accuracy_source": 0.6,
                "avg_time_per_question": 1.0,
                "hardware": "h",
                "context_window": 8192,
                "temperature": 0.0,
                "ldr_version": "1.0",
                "date_tested": "2024-01-01",
                "notes": "",
            },
            {
                "model": "accurate",
                "accuracy_focused": 0.95,
                "accuracy_source": 0.9,
                "avg_time_per_question": 10.0,
                "hardware": "h",
                "context_window": 128000,
                "temperature": 0.0,
                "ldr_version": "1.0",
                "date_tested": "2024-01-01",
                "notes": "",
            },
        ]
        f.write_text(json.dumps(data))
        return Benchmark_results(results_file=str(f))

    def test_sort_by_accuracy_descending(self, tmp_path):
        """Default sort by accuracy_focused is descending."""
        br = self._make_results(tmp_path)
        best = br.get_best()
        assert best[0]["model"] == "accurate"

    def test_sort_by_time_ascending(self, tmp_path):
        """Sort by avg_time_per_question is ascending."""
        br = self._make_results(tmp_path)
        best = br.get_best(sort_by="avg_time_per_question")
        assert best[0]["model"] == "fast"

    def test_invalid_sort_key_raises(self, tmp_path):
        """Invalid sort_by key raises ValueError."""
        br = self._make_results(tmp_path)
        with pytest.raises(ValueError, match="Invalid sort_by key"):
            br.get_best(sort_by="nonexistent_key")

    def test_get_all_returns_all(self, tmp_path):
        """get_all returns all results unchanged."""
        br = self._make_results(tmp_path)
        assert len(br.get_all()) == 2

    def test_get_best_empty_results(self, tmp_path):
        """get_best on empty results returns empty list."""
        br = Benchmark_results(results_file=str(tmp_path / "nonexist.json"))
        assert br.get_best() == []
