"""
Dynamic badge generation for README sharing.

Generates shields.io badge URLs and markdown for embedding
in README files.
"""

from urllib.parse import quote

# Badge colors by grade
GRADE_COLORS: dict[str, str] = {
    "A+": "10B981",
    "A": "22C55E",
    "A-": "34D399",
    "B+": "6366F1",
    "B": "818CF8",
    "B-": "A5B4FC",
    "C+": "F59E0B",
    "C": "FBBF24",
    "C-": "FCD34D",
    "D": "EF4444",
    "F": "DC2626",
}


def generate_badge_url(
    grade: str,
    score: int,
) -> str:
    """
    Generate shields.io badge URL.

    Args:
        grade: Letter grade (A+, A, B, etc.).
        score: Numeric score 0-100.

    Returns:
        A shields.io badge URL string.
    """
    color = GRADE_COLORS.get(grade, "6366F1")
    label = "DeepSweep"
    message = quote(f"{grade} ({score})")

    base_url = f"https://img.shields.io/badge/{label}-{message}-{color}"
    params = "?style=flat-square"

    return f"{base_url}{params}"


def generate_badge_markdown(
    grade: str,
    score: int,
    project_name: str = "my-project",
) -> str:
    """
    Generate markdown for a README badge.

    Args:
        grade: Letter grade.
        score: Numeric score 0-100.
        project_name: Project name for the profile URL.

    Returns:
        Markdown string like ``[![DeepSweep Security Grade](url)](profile)``.
    """
    badge_url = generate_badge_url(grade, score)
    profile_url = f"https://deepsweep.ai/badge/{quote(project_name)}"

    return f"[![DeepSweep Security Grade]({badge_url})]({profile_url})"


def generate_badge_html(
    grade: str,
    score: int,
    project_name: str = "my-project",
) -> str:
    """Generate HTML embed for badge."""
    badge_url = generate_badge_url(grade, score)
    profile_url = f"https://deepsweep.ai/badge/{quote(project_name)}"

    return (
        f'<a href="{profile_url}">'
        f'<img src="{badge_url}" alt="DeepSweep Security Grade" />'
        f"</a>"
    )
