import json

file_path = 'workflows/Binance Altcoin Price Logger.json'
with open(file_path, 'r', encoding='utf-8') as f:
    data = json.load(f)

nodes = {n['name']: n for n in data['nodes']}
connections = data.get('connections', {})

print("--- Node Audit ---")
print(f"Total nodes: {len(nodes)}")

print("\n--- Connection Audit ---")
for source_node, outputs in connections.items():
    if source_node not in nodes:
        print(f"❌ ERROR: Source node '{source_node}' is in connections but NOT in nodes list.")
        continue
    
    # Check if 'main' exists and is a list
    main_outputs = outputs.get('main')
    if main_outputs is None:
        print(f"⚠️ WARNING: Node '{source_node}' has no 'main' output list.")
        continue
    
    if not isinstance(main_outputs, list):
        print(f"❌ ERROR: Node '{source_node}' main output is not a list (type: {type(main_outputs)}).")
        continue

    # Check each output branch
    for i, branch in enumerate(main_outputs):
        if not isinstance(branch, list):
            print(f"❌ ERROR: Branch {i} of node '{source_node}' is not a list.")
            continue
        
        for target in branch:
            target_name = target.get('node')
            if target_name not in nodes:
                print(f"❌ ERROR: Node '{source_node}' points to '{target_name}', which does NOT exist in nodes list.")

