"""Tests for D3: UserScorecardReportRenderer HTML generation."""
from __future__ import annotations

import pytest

from agentkit_cli.user_scorecard import UserScorecardResult, RepoResult
from agentkit_cli.user_scorecard_report import UserScorecardReportRenderer, _grade_color


# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------

def _make_result(username="alice", grade="B", avg=72.0, repos=None):
    if repos is None:
        repos = [
            RepoResult("repo-0", "alice/repo-0", 90.0, "A", True, stars=10),
            RepoResult("repo-1", "alice/repo-1", 65.0, "B", False, stars=5),
            RepoResult("repo-2", "alice/repo-2", 55.0, "C", True, stars=2),
            RepoResult("repo-3", "alice/repo-3", 30.0, "D", False, stars=1),
            RepoResult("repo-4", "alice/repo-4", 20.0, "D", False, stars=0),
        ]
    return UserScorecardResult(
        username=username,
        total_repos=len(repos),
        analyzed_repos=len([r for r in repos if r.score is not None]),
        skipped_repos=0,
        avg_score=avg,
        grade=grade,
        context_coverage_pct=50.0,
        top_repos=repos[:3],
        bottom_repos=repos[-3:],
        all_repos=repos,
    )


renderer = UserScorecardReportRenderer()


# ---------------------------------------------------------------------------
# Grade color system
# ---------------------------------------------------------------------------

def test_grade_color_A():
    assert _grade_color("A") == "#3fb950"  # green

def test_grade_color_B():
    assert _grade_color("B") == "#58a6ff"  # blue

def test_grade_color_C():
    assert _grade_color("C") == "#d29922"  # yellow

def test_grade_color_D():
    assert _grade_color("D") == "#f85149"  # red


# ---------------------------------------------------------------------------
# HTML structure
# ---------------------------------------------------------------------------

def test_render_returns_html():
    result = _make_result()
    html = renderer.render(result)
    assert html.startswith("<!DOCTYPE html>")
    assert "<html" in html
    assert "</html>" in html


def test_render_has_username():
    result = _make_result(username="bob")
    html = renderer.render(result)
    assert "@bob" in html
    assert "bob" in html


def test_render_has_avatar():
    result = _make_result(username="alice")
    html = renderer.render(result)
    assert "https://github.com/alice.png" in html


def test_render_has_profile_link():
    result = _make_result(username="alice")
    html = renderer.render(result)
    assert "https://github.com/alice" in html


def test_render_has_overall_grade():
    result = _make_result(grade="B")
    html = renderer.render(result)
    # Grade badge should appear with grade color for B (blue)
    assert "#58a6ff" in html
    assert ">B<" in html


def test_render_stats_panel():
    result = _make_result(avg=72.0)
    html = renderer.render(result)
    assert "72.0" in html or "72" in html
    assert "Repos Analyzed" in html
    assert "Avg Score" in html
    assert "Context Coverage" in html


def test_render_repo_table_present():
    result = _make_result()
    html = renderer.render(result)
    assert "Ranked Repositories" in html
    assert "alice/repo-0" in html


def test_render_repo_score_bars():
    result = _make_result()
    html = renderer.render(result)
    assert "score-bar" in html
    assert "score-bar-wrap" in html


def test_render_improve_section():
    result = _make_result()
    html = renderer.render(result)
    assert "Needs Improvement" in html
    assert "agentkit analyze github:alice/" in html


def test_render_footer_credit():
    result = _make_result()
    html = renderer.render(result)
    assert "agentkit-cli" in html
    assert "mikiships/agentkit-cli" in html or "Generated by" in html


def test_render_dark_background():
    result = _make_result()
    html = renderer.render(result)
    assert "#0d1117" in html


def test_render_no_improve_section_when_few_repos():
    repos = [
        RepoResult("r-0", "alice/r-0", 80.0, "A", True),
        RepoResult("r-1", "alice/r-1", 70.0, "B", False),
    ]
    result = UserScorecardResult(
        username="alice",
        total_repos=2,
        analyzed_repos=2,
        skipped_repos=0,
        avg_score=75.0,
        grade="B",
        context_coverage_pct=50.0,
        top_repos=repos,
        bottom_repos=[],  # no bottom repos
        all_repos=repos,
    )
    html = renderer.render(result)
    assert "Needs Improvement" not in html
