#!/usr/bin/env python3

import sys
import os

# Add the src directory to the Python path
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'src'))

from alloy import parser

def test_identifier_conflict():
    
    tests = [
        # Test various tool names with the exact same body
        ("Tool name: 'test'", "test"),
        ("Tool name: 'process'", "process"), 
        ("Tool name: 'order'", "order"),
        ("Tool name: 'process_order'", "process_order"),
        ("Tool name: 'processOrder'", "processOrder"),
        ("Tool name: 'handle_order'", "handle_order"),
    ]
    
    body = '''takes order returns order_result {
    tax_amount = order + 0.08
    total = order + tax_amount
    order_result = {
        subtotal: order,
        tax: tax_amount,
        total: total
    }
}'''
    
    for name, tool_name in tests:
        code = f"tool {tool_name} {body}"
        print(f"=== {name} ===")
        print(repr(code))
        
        try:
            ast = parser.parse(code)
            if ast and ast[0] == 'program' and len(ast) > 1:
                statements = ast[1]
                if len(statements) > 0 and statements[0][0] == 'tool':
                    tool_body = statements[0][4]
                    if tool_body[0] == 'block':
                        tool_statements = tool_body[1]
                        print(f"SUCCESS: Tool body has {len(tool_statements)} statements")
                    else:
                        print(f"ERROR: Tool body is not a block: {tool_body}")
                else:
                    print(f"ERROR: No tool statement found")
            else:
                print("ERROR: Failed to parse or no statements found")
        except Exception as e:
            print(f"PARSER ERROR: {e}")
        
        print()

if __name__ == "__main__":
    test_identifier_conflict()