import sys
import pandas as pd
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
D = sys.argv[1] if len(sys.argv) > 1 else "."
c = pd.read_csv(f"{D}/centrales_artesia_5.csv").sort_values("twh_par_an")
fig, (g1, g2) = plt.subplots(1, 2, figsize=(15, 7), sharey=True)
col = ["#2e8b57" if m == "oui" else "#e67e22" if m == "à étudier" else "#7f8c8d" for m in c.max_possible]
g1.barh(c.centrale, c.twh_par_an, color=col)
for y, v in enumerate(c.twh_par_an):
    g1.text(v + 50, y, f"{v:,.0f}".replace(",", " "), va="center", fontsize=9)
g1.set_title("TWh par an livrés au plan", fontsize=11)
g1.set_xlim(0, 8200)
g2.barh(c.centrale, c.md_euros, color=col)
for y, (v, m) in enumerate(zip(c.md_euros, c.meur_par_gwh)):
    t = f"{v:,.0f} Md€".replace(",", " ") + (f", {m:.2f} M€/GWh".replace(".", ",") if pd.notna(m) else "")
    g2.text(v + 40, y, t, va="center", fontsize=9)
g2.set_title("Milliards d'euros sur 25 ans, et M€ par GWh annuel", fontsize=11)
g2.set_xlim(0, 4600)
g1.plot([], [], color="#2e8b57", lw=6, label="Version MAX prévue")
g1.plot([], [], color="#e67e22", lw=6, label="MAX à étudier")
g1.plot([], [], color="#7f8c8d", lw=6, label="Pas de MAX (taille fixée par le site)")
g1.legend(loc="lower right", fontsize=9)
fig.suptitle("Les centrales du plan Artesia 5, version 1, économie mondiale", fontsize=13)
fig.tight_layout(rect=[0, 0, 1, 0.95])
fig.savefig(f"{D}/schema_centrales_artesia_5.png", dpi=110)
