#!/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_data_pipeline():
    
    # Test the exact code from the failing test
    code = '''
    command Parse means "parse and structure the provided CSV data for analysis"
    command Calculate means "calculate monthly sales figures and totals from parsed data"

    tool clean_data takes raw_data returns cleaned_result {
        cleaned_result = raw_data + "_cleaned"
    }

    data_analyst = agent "gpt-4o-mini"

    raw_data = "sales_csv_data"
    cleaned_data, clean_error = clean_data(raw_data)
    csv_data = cleaned_data
    monthly_sales = "January through December sales figures"

    analysis, analysis_error = data_analyst {
        must Parse csv_data
        must Calculate monthly_sales
    }
    '''
    
    print("=== Testing data processing pipeline ===")
    print("Code length:", len(code))
    
    try:
        ast = parser.parse(code)
        if ast and ast[0] == 'program' and len(ast) > 1:
            statements = ast[1]
            print(f"SUCCESS: Parsed {len(statements)} statements")
            for i, stmt in enumerate(statements):
                if stmt:
                    print(f"  Statement {i}: {stmt[0]}")
                else:
                    print(f"  Statement {i}: None")
        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_data_pipeline()