"""Integration tests for the review pipeline."""

from __future__ import annotations

from bandito.review import run_basics
from bandito.review.models import ReviewConfig
from .conftest import write_trace, write_evaluation


class TestBasics:
    def test_no_traces_dir(self, tmp_path, capsys):
        config = ReviewConfig(directory=str(tmp_path / "nonexistent"))
        run_basics(config)
        captured = capsys.readouterr()
        assert "No traces found" in captured.err
        assert "Traces directory:" in captured.err

    def test_empty_traces_dir(self, traces_dir, capsys):
        config = ReviewConfig(directory=str(traces_dir))
        run_basics(config)
        captured = capsys.readouterr()
        assert "No traces found" in captured.err

    def test_basic_output(self, traces_dir, make_trace, capsys):
        for i in range(6):
            write_trace(traces_dir, make_trace(trace_id=f"t-{i}", cost=0.01 * (i + 1)))

        config = ReviewConfig(directory=str(traces_dir))
        run_basics(config)

        captured = capsys.readouterr()
        assert "bandito observe basics" in captured.out
        assert "6 traces" in captured.out
        assert "bandito analyze" in captured.out  # next steps point to analyze

    def test_multiple_projects(self, traces_dir, make_trace, capsys):
        write_trace(traces_dir, make_trace(trace_id="t1", project="proj-a"))
        write_trace(traces_dir, make_trace(trace_id="t2", project="proj-b"))

        config = ReviewConfig(directory=str(traces_dir))
        run_basics(config)

        captured = capsys.readouterr()
        assert "projects found" in captured.err
        assert "bandito observe basics --project" in captured.err
        assert "bandito observe projects" in captured.err

    def test_project_filter(self, traces_dir, make_trace, capsys):
        for i in range(6):
            write_trace(traces_dir, make_trace(trace_id=f"t-{i}", project="my-app"))
        write_trace(traces_dir, make_trace(trace_id="other", project="other"))

        config = ReviewConfig(directory=str(traces_dir), project="my-app")
        run_basics(config)

        captured = capsys.readouterr()
        assert "my-app" in captured.out

    def test_patterns_appear(self, traces_dir, make_trace, capsys):
        for i in range(6):
            write_trace(traces_dir, make_trace(trace_id=f"t-{i}", model="gpt-4o"))

        config = ReviewConfig(directory=str(traces_dir))
        run_basics(config)

        captured = capsys.readouterr()
        assert "Patterns" in captured.out
        assert "Single model" in captured.out

    def test_last_n_limit(self, traces_dir, make_trace, capsys):
        for i in range(20):
            write_trace(traces_dir, make_trace(trace_id=f"t-{i}"))

        config = ReviewConfig(directory=str(traces_dir), last_n=5)
        run_basics(config)

        captured = capsys.readouterr()
        assert "5 traces" in captured.out
        assert "last 5" in captured.out

    def test_with_evaluations(self, traces_dir, make_trace, make_evaluation, capsys):
        for i in range(6):
            write_trace(traces_dir, make_trace(trace_id=f"t-{i}"))

        # Evaluations are now passed directly (no longer loaded from store)
        evals = {
            f"t-{i}": make_evaluation(f"t-{i}", human_score=1 if i % 2 == 0 else 0)
            for i in range(6)
        }

        config = ReviewConfig(directory=str(traces_dir))
        run_basics(config, evaluations=evals)

        captured = capsys.readouterr()
        assert "6 human-graded" in captured.out

    def test_error_traces(self, traces_dir, make_trace, capsys):
        for i in range(6):
            error = "timeout" if i < 2 else None
            write_trace(traces_dir, make_trace(trace_id=f"t-{i}", error=error))

        config = ReviewConfig(directory=str(traces_dir))
        run_basics(config)

        captured = capsys.readouterr()
        assert "2 errors" in captured.out

    def test_project_not_found(self, traces_dir, make_trace, capsys):
        write_trace(traces_dir, make_trace(trace_id="t1", project="real"))

        config = ReviewConfig(directory=str(traces_dir), project="nope")
        run_basics(config)

        captured = capsys.readouterr()
        assert "No traces found for project" in captured.err
