"""FIA (Fact-Impact-Act) battle card data model and Markdown assembly."""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Optional


@dataclass
class FIASection:
    """One section of a Fact-Impact-Act battle card.

    Attributes:
        name: Section name (e.g. "Fact", "Impact", "Act").
        content: List of bullet-point strings.
        chart_reference: Optional path to a mini-chart PNG file.
    """

    name: str
    content: list[str]
    chart_reference: Optional[str] = None


@dataclass
class BattleCard:
    """A single FIA battle card for AI bubble research.

    Attributes:
        card_number: Integer 1-8 identifying the card.
        title: Human-readable title for the card.
        cluster: Cluster grouping — "bubble" or "value".
        summary: One-line summary rendered as a Markdown blockquote.
        fact: The Fact section of the FIA model.
        impact: The Impact section of the FIA model.
        act: The Act section of the FIA model.
        sources: List of source references.
        last_updated: Timestamp string for the card.
    """

    card_number: int
    title: str
    cluster: str
    summary: str
    fact: FIASection
    impact: FIASection
    act: FIASection
    sources: list[str]
    last_updated: str


def render_card(card: BattleCard) -> str:
    """Assemble a BattleCard into a Markdown string.

    Parameters
    ----------
    card : BattleCard
        The card instance to render.

    Returns
    -------
    str
        Complete Markdown string for the card.
    """
    lines: list[str] = []

    # Header
    lines.append(f"# Card {card.card_number}: {card.title}")
    lines.append("")

    # Summary blockquote
    lines.append(f"> {card.summary}")
    lines.append("")

    # Fact section
    lines.append("## Fact")
    lines.append("")
    for bullet in card.fact.content:
        lines.append(f"- {bullet}")
    lines.append("")

    # Chart reference (if any)
    if card.fact.chart_reference:
        chart_filename = card.fact.chart_reference.split("/")[-1]
        lines.append(f"![]({chart_filename})")
        lines.append("")

    # Impact section
    lines.append("## Impact")
    lines.append("")
    for bullet in card.impact.content:
        lines.append(f"- {bullet}")
    lines.append("")

    # Act section
    lines.append("## Act")
    lines.append("")
    for bullet in card.act.content:
        lines.append(f"- {bullet}")
    lines.append("")

    # Footer
    lines.append("---")
    lines.append("")
    sources_str = ", ".join(card.sources)
    lines.append(f"*Last updated: {card.last_updated} | Sources: {sources_str}*")
    lines.append("")

    return "\n".join(lines)
