#!/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_complex_tool():
    
    # Test just the outer_tool that's failing
    complex_tool = '''
tool outer_tool takes x returns result {
    inner_result, inner_error = inner_tool(x)
    when inner_error then {
        result = (null, inner_error)
    } otherwise {
        result = inner_result + "_outer"
    }
}
'''
    
    print("=== Testing complex tool ===")
    print(repr(complex_tool))
    
    try:
        ast = parser.parse(complex_tool)
        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")
                    for i, stmt in enumerate(tool_statements):
                        print(f"  Statement {i}: {stmt[0]}")
                else:
                    print(f"ERROR: Tool body is not a block")
            else:
                print(f"ERROR: No tool statement found")
        else:
            print("ERROR: Failed to parse")
            print("AST:", ast)
    except Exception as e:
        print(f"PARSER ERROR: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    test_complex_tool()