from arango import ArangoClient
import json

# =========================
# CONNESSIONE ARANGO (DOCKER)
# =========================
client = ArangoClient(hosts="http://127.0.0.1:8529")

# database di sistema (serve per creare DB)
sys_db = client.db("_system", username="root", password="passwort")

# =========================
# CREA DATABASE SE NON ESISTE
# =========================
if not sys_db.has_database("waternet"):
    sys_db.create_database("waternet")

# connessione al database vero
db = client.db("waternet", username="root", password="passwort")

# =========================
# CREA COLLECTIONS
# =========================
if not db.has_collection("nodes"):
    db.create_collection("nodes")

if not db.has_collection("edges"):
    db.create_collection("edges", edge=True)

nodes_col = db.collection("nodes")
edges_col = db.collection("edges")

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

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

# =========================
# IMPORT NODI
# =========================
print("Import nodi...")

for n in nodes:
    try:
        nodes_col.insert(n, overwrite=True)
    except Exception as e:
        print(f"Errore nodo {n.get('_key')}: {e}")

# =========================
# IMPORT ARCHI
# =========================
print("Import edges...")

for e in edges:
    try:
        edges_col.insert(e, overwrite=True)
    except Exception as e:
        print(f"Errore edge: {e}")

print("✔ IMPORT COMPLETATO: Net3 caricato in ArangoDB")
