import csv, json, re, os, glob
DS="/var/www/2.SOURCES/GAB/GAB-001-390-DATA-SOURCES/GAB-DATA-SOURCES"
KIT="/var/www/2.SOURCES/GAB/KIT-CONVERSION-GAB-DATA-DRIVEN/KIT-CONVERSION-GAB"
OUT="/var/www/2.SOURCES/GAB/CONVERSION-OUTPUT"

# 1) source file ranges
files={}  # gabnum -> (abspath, lotcore)
with open(f"{DS}/_INDEX-SOURCES.csv") as f:
    for r in csv.DictReader(f):
        fn=r["fichier_html"]; deb=int(r["gab_debut"].split("-")[1]); fin=int(r["gab_fin"].split("-")[1])
        core=fn.replace("INDEX-300-","").replace("-PLAYABLE.html","")
        for n in range(deb,fin+1):
            files[n]=(os.path.join(DS,fn), "LOT-"+core)

# 2) cartography
carto={}
with open(f"{KIT}/04-CARTOGRAPHIE-GAB.csv") as f:
    for r in csv.DictReader(f):
        m=re.match(r"GAB-(\d+)", r["gab_id"])
        if not m: continue
        carto[int(m.group(1))]={"canonical_name":r["canonical_name"],"module_owner":r["module_owner"],
                                "renderer_key":r["renderer_key"],"flag":r.get("flag","").strip()}

DONE={301,302,303,304,305}
gabs=[]
missing_src=[]
for n in range(1,391):
    gid=f"GAB-{n:03d}"
    c=carto.get(n,{})
    src=files.get(n)
    if not src: missing_src.append(gid)
    lot = src[1] if src else "LOT-UNKNOWN"
    gabs.append({
        "gab_id":gid,"num":n,
        "source_file": src[0] if src else None,
        "lot_dir": f"{OUT}/{lot}",
        "renderer_key": c.get("renderer_key","?"),
        "canonical_name": c.get("canonical_name","?"),
        "module_owner": c.get("module_owner","?"),
        "sensible": (c.get("flag","")=="SENSIBLE"),
        "done": n in DONE,
    })

man={"gabs":gabs}
with open(f"{OUT}/_manifest-390.json","w") as f: json.dump(man,f,ensure_ascii=False,indent=1)

# summary
todo=[g for g in gabs if not g["done"]]
sens=[g for g in gabs if g["sensible"]]
from collections import Counter
rk=Counter(g["renderer_key"] for g in todo)
lots=Counter(g["lot_dir"].split("/")[-1] for g in gabs)
print("TOTAL gabs:",len(gabs)," done:",len(gabs)-len(todo)," à convertir:",len(todo))
print("sans fichier source:",missing_src or "AUCUN")
print("SENSIBLES (flag):",len(sens))
print("renderer_key à convertir:",dict(rk))
print("nb lots:",len(lots))
print("waves de 5:", (len(todo)+4)//5)
print("ex. descripteur:",json.dumps(todo[0],ensure_ascii=False))
