#!/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_exact_diff():
    
    tests = [
        ("Working simple case", '''
tool test takes x returns result {
    a = x + 1
    b = a + 1
    result = {a: a}
}'''),
        
        ("Use decimal literal", '''
tool test takes x returns result {
    a = x + 0.08
    b = a + 1
    result = {a: a}
}'''),
        
        ("Use exact variable names", '''
tool test takes order returns result {
    tax_amount = order + 0.08
    total = tax_amount + 1
    result = {a: tax_amount}
}'''),
        
        ("Use exact object keys but simple values", '''
tool test takes order returns result {
    tax_amount = order + 0.08
    total = tax_amount + 1
    result = {
        subtotal: order,
        tax: tax_amount,
        total: total
    }
}'''),
        
        ("Change the last assignment", '''
tool test takes order returns result {
    tax_amount = order + 0.08
    total = order + tax_amount
    result = {
        subtotal: order,
        tax: tax_amount,
        total: total
    }
}'''),
        
        ("EXACT failing case", '''
tool process_order 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, code in tests:
        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")
                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_exact_diff()