from pyvis.network import Network
import json

# =========================
# CARICA DATI
# =========================
with open("nodes.json") as f:
    nodes = json.load(f)

with open("edges.json") as f:
    edges = json.load(f)

# =========================
# CREA GRAFO INTERATTIVO
# =========================
net = Network(height="900px", width="100%", bgcolor="#111111", font_color="white")
net.barnes_hut()  # fisica più stabile per grafi grandi

# =========================
# AGGIUNGI NODI
# =========================
for n in nodes:
    net.add_node(
        n["_key"],
        label=n["_key"],
        title=str(n.get("type", "")),
        size=5
    )

# =========================
# AGGIUNGI ARCHI
# =========================
for e in edges:
    src = e["_from"].split("/")[1]
    dst = e["_to"].split("/")[1]

    net.add_edge(src, dst)

# =========================
# SALVA HTML (FIX PYVIS)
# =========================
net.write_html("net3.html", open_browser=False)

print("✔ Visualizzazione creata: net3.html")
