"""Tests for the export module — wrapped SVG, markdown report, and badge SVG."""

from huntd.analytics import (
    ActivityPattern,
    Analytics,
    CodeVelocity,
    FocusScore,
    LanguageEvolution,
    RepoRanking,
    Streaks,
    WorkdaySplit,
)
from huntd.export import generate_badge_svg, generate_report_md, generate_wrapped_svg


def _base_analytics(**overrides) -> Analytics:
    defaults = dict(
        total_repos=3,
        total_commits=150,
        total_languages=4,
        streaks=Streaks(current=14, longest=31, today_commits=3),
        heatmap=[[1, 0, 2, 3] + [0] * 48 for _ in range(7)],
        languages={"Python": 5000, "Go": 2000, "TypeScript": 800},
        activity=ActivityPattern(
            busiest_day="Tuesday",
            busiest_hour=14,
            avg_commits_per_day=3.2,
            commits_by_hour=[0] * 24,
            commits_by_dow=[0] * 7,
        ),
        repo_rankings=[
            RepoRanking(path="/a", name="alpha", commits=80, primary_language="Python",
                        health_score=90, lines_added=3000, lines_removed=500),
            RepoRanking(path="/b", name="beta", commits=50, primary_language="Go",
                        health_score=75, lines_added=1500, lines_removed=200),
        ],
        language_evolution=LanguageEvolution(),
        code_velocity=CodeVelocity(
            commits_by_week={"2025-W01": 5, "2025-W02": 8},
            trend="up", peak_week="2025-W02", peak_commits=8,
        ),
        focus_score=FocusScore(
            avg_repos_per_day=2.1, most_focused_day="2025-01-15",
            most_scattered_day="2025-01-20", interpretation="balanced",
        ),
        workday_split=WorkdaySplit(
            weekday_commits=120, weekend_commits=30,
            weekday_pct=80.0, weekend_pct=20.0,
            weekday_lines=4000, weekend_lines=1000,
        ),
        file_hotspots=[],
    )
    defaults.update(overrides)
    return Analytics(**defaults)


# --- Wrapped SVG ---

def test_wrapped_svg_is_valid_svg():
    a = _base_analytics()
    svg = generate_wrapped_svg(a)
    assert svg.startswith("<svg")
    assert svg.strip().endswith("</svg>")


def test_wrapped_svg_contains_stats():
    a = _base_analytics()
    svg = generate_wrapped_svg(a)
    assert "150" in svg  # total commits
    assert "3" in svg  # total repos
    assert "Python" in svg  # top language
    assert "alpha" in svg  # top repo
    assert "14 days" in svg  # current streak


def test_wrapped_svg_contains_heatmap_rects():
    a = _base_analytics()
    svg = generate_wrapped_svg(a)
    assert "<rect" in svg


def test_wrapped_svg_contains_achievements():
    a = _base_analytics()
    svg = generate_wrapped_svg(a)
    assert "ACHIEVEMENTS" in svg


def test_wrapped_svg_empty_languages():
    a = _base_analytics(languages={})
    svg = generate_wrapped_svg(a)
    assert svg.startswith("<svg")


# --- Markdown Report ---

def test_report_md_has_header():
    a = _base_analytics()
    md = generate_report_md(a)
    assert md.startswith("# huntd Report")


def test_report_md_has_overview_table():
    a = _base_analytics()
    md = generate_report_md(a)
    assert "| Repositories | 3 |" in md
    assert "| Total Commits | 150 |" in md
    assert "| Current Streak | 14 days |" in md


def test_report_md_has_repos_section():
    a = _base_analytics()
    md = generate_report_md(a)
    assert "## Top Repositories" in md
    assert "alpha" in md
    assert "beta" in md


def test_report_md_has_languages_section():
    a = _base_analytics()
    md = generate_report_md(a)
    assert "## Languages" in md
    assert "Python" in md


def test_report_md_has_velocity():
    a = _base_analytics()
    md = generate_report_md(a)
    assert "## Code Velocity" in md
    assert "up" in md


def test_report_md_has_focus():
    a = _base_analytics()
    md = generate_report_md(a)
    assert "## Focus Score" in md
    assert "balanced" in md


def test_report_md_has_workday_split():
    a = _base_analytics()
    md = generate_report_md(a)
    assert "## Weekday vs Weekend" in md
    assert "80.0%" in md


def test_report_md_has_achievements():
    a = _base_analytics()
    md = generate_report_md(a)
    assert "## Achievements" in md
    assert "Century" in md


def test_report_md_has_footer():
    a = _base_analytics()
    md = generate_report_md(a)
    assert "Generated by" in md
    assert "huntd" in md


# --- Badge SVG ---

def test_badge_svg_is_valid_svg():
    a = _base_analytics()
    svg = generate_badge_svg(a)
    assert svg.strip().startswith("<svg")
    assert svg.strip().endswith("</svg>")


def test_badge_svg_contains_streak():
    a = _base_analytics()
    svg = generate_badge_svg(a)
    assert "14d streak" in svg


def test_badge_svg_contains_top_language():
    a = _base_analytics()
    svg = generate_badge_svg(a)
    assert "Python" in svg


def test_badge_svg_contains_huntd_label():
    a = _base_analytics()
    svg = generate_badge_svg(a)
    assert "huntd" in svg
