#!/usr/bin/env python3
"""Debug script to reproduce the COUNT(*) issue."""

import networkx as nx
from nxcypher import NXCypher

# First, verify that the matching works for normal cases
print("=== Test 0: Sanity check - normal match ===")
G0 = nx.DiGraph()
G0.add_node('n1', __labels__={'A'})
G0.add_node('n2', __labels__={'B'})
G0.add_edge('n1', 'n2', __labels__={'REL'})
result0 = NXCypher(G0).run("MATCH (n)-->(m) RETURN n, m")
print(f"Result: {result0}")
print(f"Number of matches: {len(result0.get('n', []))}")
result0_count = NXCypher(G0).run("MATCH ()-->() RETURN count(*)")
print(f"Count result: {result0_count}")
print()

# Test 0b: Check if self-loop is actually same node
print("=== Test 0b: Check if (n)-->(n) requires same node ===")
G0b = nx.DiGraph()
G0b.add_node('n1', __labels__={'A'})
G0b.add_node('n2', __labels__={'B'})
G0b.add_edge('n1', 'n2', __labels__={'REL'})
result0b = NXCypher(G0b).run("MATCH (n)-->(n) RETURN n")
print(f"Result: {result0b}")
print(f"Expected: empty (no self-loops in graph)")
print()

# Test 1: Scenario [1] - Undirected match in self-relationship graph
print("=== Test 1: Undirected match () -- () with self-loop ===")
G1 = nx.DiGraph()
NXCypher(G1).run("CREATE (a:A)-[:LOOP]->(a)")
print(f"Graph nodes: {list(G1.nodes(data=True))}")
print(f"Graph edges: {list(G1.edges(data=True))}")

result1 = NXCypher(G1).run("MATCH ()--() RETURN count(*)")
print(f"Result: {result1}")
print(f"Expected: {{'count(*)': [1]}}")
print()

# Test 2: Scenario [4] - Directed match ()-->() with self-loop
print("=== Test 2: Directed match ()-->() with self-loop ===")
G2 = nx.DiGraph()
NXCypher(G2).run("CREATE (a:A)-[:LOOP]->(a)")
print(f"Graph nodes: {list(G2.nodes(data=True))}")
print(f"Graph edges: {list(G2.edges(data=True))}")

result2 = NXCypher(G2).run("MATCH ()-->() RETURN count(*)")
print(f"Result: {result2}")
print(f"Expected: {{'count(*)': [1]}}")
print()

# Test 3: Check if matches are being found at all
print("=== Test 3: Return nodes instead of count ===")
G3 = nx.DiGraph()
NXCypher(G3).run("CREATE (a:A)-[:LOOP]->(a)")
result3 = NXCypher(G3).run("MATCH (n)-->(m) RETURN n, m")
print(f"Result: {result3}")
print(f"Number of matches: {len(result3.get('n', []))}")
print()

# Test 4: Check if matches work with undirected
print("=== Test 4: Return nodes with undirected match ===")
G4 = nx.DiGraph()
NXCypher(G4).run("CREATE (a:A)-[:LOOP]->(a)")
result4 = NXCypher(G4).run("MATCH (n)--(m) RETURN n, m")
print(f"Result: {result4}")
print(f"Number of matches: {len(result4.get('n', []))}")
