"""Slide 5: Q-factor themes — % of banks citing + median bps when cited."""
from __future__ import annotations
from pathlib import Path

import matplotlib.pyplot as plt

from .style import BC_BLUE, BC_BLUE_LIGHT, BC_GRAY, apply_style
from .data import fetch_theme_prevalence


def plot_themes(shortqtr: str, out_dir: Path) -> None:
    apply_style()
    rows = fetch_theme_prevalence(shortqtr)
    if not rows:
        print("  no theme data — did LLM passes run?")
        return

    # Suppress themes with N<5 per spec privacy rules.
    rows = [r for r in rows if r["count_true"] and r["count_true"] >= 5]
    if not rows:
        return

    rows = rows[:20]  # top 20 by prevalence
    rows = rows[::-1]  # horizontal bars plot bottom-to-top

    names = [r["name"] for r in rows]
    pcts = [100.0 * r["count_true"] / r["count_total"] for r in rows]
    # amount is stored as a percentage (e.g. 0.05 = 0.05% = 5 bps); *100 → bps.
    medians = [float(r["median_amount"] or 0) * 100 for r in rows]

    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(14, max(5, 0.4 * len(rows))),
                                    gridspec_kw={"width_ratios": [3, 2]})
    ax1.barh(names, pcts, color=BC_BLUE)
    for i, p in enumerate(pcts):
        ax1.text(p + 1, i, f"{p:.0f}%", va="center", fontsize=10, color=BC_GRAY)
    ax1.set_xlabel("% of banks citing theme")
    ax1.set_title("Theme prevalence")

    ax2.barh(names, medians, color=BC_BLUE_LIGHT)
    for i, m in enumerate(medians):
        ax2.text(m + 0.5, i, f"{m:.0f} bps", va="center", fontsize=10, color=BC_GRAY)
    ax2.set_xlabel("Median bps assigned (when cited)")
    ax2.set_title("Median size of adjustment")
    ax2.set_yticklabels([])

    fig.suptitle(f"Q-factor themes — {shortqtr.upper()}", fontsize=16)
    plt.tight_layout()
    out = out_dir / "05-qfactor-themes.png"
    fig.savefig(out)
    plt.close(fig)
    print(f"  wrote {out}")
