import matplotlib
import matplotlib.font_manager as fm
fm._load_fontmanager(try_read_cache=False)
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import patheffects

plt.rcParams.update({
    "font.family": "Comic Neue",
    "font.size": 14.0,
    "path.sketch": (1, 100, 2),
    "path.effects": [patheffects.withStroke(linewidth=4, foreground="w")],
    "axes.linewidth": 1.5,
    "lines.linewidth": 2.0,
    "figure.facecolor": "white",
    "grid.linewidth": 0.0,
    "axes.grid": False,
    "axes.unicode_minus": False,
    "axes.edgecolor": "black",
    "xtick.major.size": 8,
    "xtick.major.width": 3,
    "ytick.major.size": 8,
    "ytick.major.width": 3,
})

categories = ["Ansible", "Helm", "Go Lint GHA"]
without = [900, 210, 450]
with_dervish = [60, 70, 30]
compression = [f"{w//d}×" for w, d in zip(without, with_dervish)]

x = np.arange(len(categories))
width = 0.3

fig, ax = plt.subplots(figsize=(8, 4.5))

bars1 = ax.bar(x - width/2, without, width, label="Without Dervish", color="#888888", edgecolor="black")
bars2 = ax.bar(x + width/2, with_dervish, width, label="With Dervish", color="#853E91", edgecolor="black")

for bar, val in zip(bars1, without):
    ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 10,
            str(val), ha="center", va="bottom", fontsize=9)
for bar, val, comp in zip(bars2, with_dervish, compression):
    ax.text(bar.get_x() + bar.get_width()/2, bar.get_height() + 10,
            f"{val} ({comp})", ha="center", va="bottom", fontsize=9)

ax.set_ylabel("Tokens")
ax.set_title("Token Savings per Dataset")
ax.set_xticks(x)
ax.set_xticklabels(categories)
ax.legend(frameon=True)
ax.set_ylim(0, 1100)
fig.tight_layout()
fig.savefig("chart_token_savings.png", dpi=200)
plt.close()
print("chart_token_savings.png regenerated")
