#!/usr/bin/env python3
"""Genera versione standalone della Emma Dashboard con dati inline"""
import json, os, re

HOME = os.path.expanduser("~/.hermes")
HTML_FILE = os.path.join(HOME, "emma-dashboard.html")
DATA_FILE = os.path.join(HOME, "dashboard_data.json")
OUTPUT_FILE = os.path.join(HOME, "fak-comparison", "emma-dashboard-standalone.html")

def main():
    with open(HTML_FILE) as f:
        html = f.read()
    with open(DATA_FILE) as f:
        data = json.load(f)
    
    data_json = json.dumps(data)
    
    # Sostituisci il fetch con dati inline
    old = """async function loadData() {
  try {
    const r = await fetch('/dashboard_data.json?_t='+Date.now());
    if (!r.ok) throw new Error('HTTP '+r.status);
    const d = await r.json();
    render(d);
  } catch(e) {
    document.getElementById('dati-ts').textContent = '\u26a0\ufe0f errore caricamento';
  }
}"""
    
    new = """var DASHBOARD_DATA = """ + data_json + """;

function loadData() {
  render(DASHBOARD_DATA);
}"""
    
    html = html.replace(old, new)
    
    # Rimuovi setInterval
    html = html.replace("setInterval(loadData, 30000);", "// setInterval(loadData, 30000); (standalone)")
    
    with open(OUTPUT_FILE, "w") as f:
        f.write(html)
    
    size = os.path.getsize(OUTPUT_FILE)
    print(f"[OK] Standalone generata: {OUTPUT_FILE} ({size} byte)")

if __name__ == "__main__":
    main()
