"""Tests for the export system."""

import json

import pytest

from agent_scout.export import ExportData, ExportEntry, Exporter


@pytest.fixture
def exporter():
    return Exporter()


@pytest.fixture
def sample_data():
    return ExportData(
        task_description="Find the best laptop under $1000",
        entries=[
            ExportEntry(entry_type="thought", content="Let me search for laptops"),
            ExportEntry(
                entry_type="action",
                content="Searching",
                metadata={"tool_name": "web_search", "tool_input": {"query": "best laptop 2024"}},
            ),
            ExportEntry(
                entry_type="observation",
                content="Found 10 results from various sources",
            ),
            ExportEntry(entry_type="result", content="Top pick: MacBook Air M3"),
            ExportEntry(entry_type="error", content="Rate limit on secondary search"),
        ],
        result={"recommendation": "MacBook Air M3", "price": 999},
        cost=0.0042,
        duration_seconds=15.3,
        model="claude-sonnet-4-20250514",
        created_at=1700000000.0,
    )


def test_to_markdown(exporter, sample_data):
    md = exporter.to_markdown(sample_data)
    assert "# AgentScout Task Report" in md
    assert "Find the best laptop" in md
    assert "Thinking" in md
    assert "`web_search`" in md
    assert "Observation" in md
    assert "Result" in md
    assert "Error" in md
    assert "$0.0042" in md
    assert "15.3s" in md
    assert "claude-sonnet" in md
    assert "Generated by AgentScout" in md


def test_to_html(exporter, sample_data):
    html = exporter.to_html(sample_data)
    assert "<!DOCTYPE html>" in html
    assert "AgentScout Task Report" in html
    assert "Find the best laptop" in html
    assert "web_search" in html
    assert "MacBook Air M3" in html
    assert "$0.0042" in html


def test_to_json(exporter, sample_data):
    json_str = exporter.to_json(sample_data)
    data = json.loads(json_str)
    assert data["task_description"] == "Find the best laptop under $1000"
    assert len(data["entries"]) == 5
    assert data["cost"] == 0.0042
    assert data["result"]["recommendation"] == "MacBook Air M3"


def test_empty_data(exporter):
    data = ExportData(task_description="Empty task")
    md = exporter.to_markdown(data)
    assert "Empty task" in md
    html = exporter.to_html(data)
    assert "Empty task" in html
    json_str = exporter.to_json(data)
    parsed = json.loads(json_str)
    assert parsed["task_description"] == "Empty task"


def test_markdown_no_cost_or_duration(exporter):
    data = ExportData(task_description="No cost")
    md = exporter.to_markdown(data)
    assert "Cost" not in md
    assert "Duration" not in md


def test_html_no_cost_or_duration(exporter):
    data = ExportData(task_description="No cost")
    html = exporter.to_html(data)
    assert "Cost:" not in html
    assert "Duration:" not in html


def test_markdown_with_result(exporter):
    data = ExportData(
        task_description="Task",
        result={"answer": 42},
    )
    md = exporter.to_markdown(data)
    assert "Final Result" in md
    assert '"answer": 42' in md


def test_all_entry_types_in_html(exporter, sample_data):
    html = exporter.to_html(sample_data)
    assert 'class="entry thought"' in html
    assert 'class="entry action"' in html
    assert 'class="entry observation"' in html
    assert 'class="entry result"' in html
    assert 'class="entry error"' in html
