"""Tests for bandito projects command."""

from __future__ import annotations

import json
import os

from bandito.cli.observe import list_projects as _list_projects_from_store
from bandito.storage import LocalStore


def _write_trace(traces_dir, trace_id, project, cost=None):
    project_dir = os.path.join(str(traces_dir), "traces", project)
    os.makedirs(project_dir, exist_ok=True)
    path = os.path.join(project_dir, f"{trace_id}.json")
    span = {
        "span_id": f"sp-{trace_id}",
        "name": "llm",
        "kind": "llm",
        "started_at": "2026-03-19T12:00:00Z",
        "ended_at": "2026-03-19T12:00:01Z",
        "status": "ok",
    }
    if cost is not None:
        span["cost"] = cost
    trace = {
        "trace_id": trace_id,
        "project": project,
        "started_at": "2026-03-19T12:00:00Z",
        "ended_at": "2026-03-19T12:00:01Z",
        "status": "ok",
        "spans": [span],
    }
    with open(path, "w") as f:
        json.dump(trace, f)




class TestListProjects:
    def test_no_projects(self, tmp_path, capsys):
        traces = tmp_path / "traces"
        traces.mkdir()
        _list_projects_from_store(LocalStore(str(tmp_path)))
        captured = capsys.readouterr()
        assert "No projects found" in captured.out

    def test_single_project(self, tmp_path, capsys):
        (tmp_path / "traces").mkdir()
        for i in range(3):
            _write_trace(tmp_path, f"t-{i}", "my-bot", cost=0.01)
        _list_projects_from_store(LocalStore(str(tmp_path)))
        captured = capsys.readouterr()
        assert "my-bot" in captured.out
        assert "3 traces" in captured.out
        assert "1 project" in captured.out

    def test_multiple_projects(self, tmp_path, capsys):
        (tmp_path / "traces").mkdir()
        for i in range(5):
            _write_trace(tmp_path, f"a-{i}", "chatbot", cost=0.02)
        for i in range(3):
            _write_trace(tmp_path, f"b-{i}", "search-agent")
        _write_trace(tmp_path, "c-0", "rag-pipeline", cost=0.10)
        _list_projects_from_store(LocalStore(str(tmp_path)))
        captured = capsys.readouterr()
        assert "3 projects" in captured.out
        assert "chatbot" in captured.out
        assert "search-agent" in captured.out
        assert "rag-pipeline" in captured.out
        assert "bandito diagnostics --project" in captured.out

    def test_shows_cost_when_available(self, tmp_path, capsys):
        (tmp_path / "traces").mkdir()
        _write_trace(tmp_path, "t-0", "bot", cost=0.05)
        _write_trace(tmp_path, "t-1", "bot", cost=0.03)
        _list_projects_from_store(LocalStore(str(tmp_path)))
        captured = capsys.readouterr()
        assert "$0.0800" in captured.out

    def test_no_cost_when_not_reported(self, tmp_path, capsys):
        (tmp_path / "traces").mkdir()
        _write_trace(tmp_path, "t-0", "bot")  # no cost
        _list_projects_from_store(LocalStore(str(tmp_path)))
        captured = capsys.readouterr()
        assert "$" not in captured.out
