# Exporta varias corridas como escenarios seleccionables en el visor: web/esc/<id>/{h,v,p,t}.png + run.json
# (parámetros, resumen, flechas e impacto por edificio en el mismo orden de escena.json) y web/esc/index.json.
# Uso: python exportar_escenarios.py id1=res/x.npz:"Etiqueta 1" id2=res/y.npz:"Etiqueta 2" ...   (el primero queda por defecto)
import sys, json, os, shutil, subprocess
lista = []
for arg in sys.argv[1:]:
    k, rest = arg.split('=', 1); f, et = rest.split(':', 1)
    if os.path.exists(f'web/esc/{k}/run.json') and os.path.getmtime(f'web/esc/{k}/run.json') > os.path.getmtime(f):   # ya exportado y vigente
        lista.append({'id': k, 'etiqueta': et}); print('reutilizado', k); continue
    subprocess.run([sys.executable, 'exportar_visor.py', f], check=True, stderr=subprocess.DEVNULL)
    d = f'web/esc/{k}'; os.makedirs(d, exist_ok=True)
    for c in ('h', 'v', 'p', 't'): shutil.move(f'web/{c}.png', f'{d}/{c}.png')
    E = json.load(open('web/escena.json')); m = E['meta']
    json.dump({'id': k, 'etiqueta': et, 'params': m['params'], 'area_ha': m['area_ha'], 'h_max': m['h_max'], 'v_max': m['v_max'],
               'flechas': E['flechas'], 'imp': [b['imp'] for b in E['bloques']]}, open(f'{d}/run.json', 'w'), separators=(',', ':'))
    lista.append({'id': k, 'etiqueta': et}); print('exportado', k, et, m['area_ha'], 'ha')
json.dump(lista, open('web/esc/index.json', 'w'), ensure_ascii=False)
