#!/usr/bin/env python3
"""
Test SQL Grammar Integration
Tests the new grammar-based SQL strategy against the old hand-written parser.
Demonstrates the integration of xwsystem.syntax with xwquery.
Company: eXonware.com
Author: eXonware Backend Team
Email: connect@exonware.com
Version: 0.0.1
Generation Date: January 2, 2025
"""

import sys
import os
from typing import Any
# Add xwquery to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..', '..', '..', '..'))
from exonware.xwquery.compiler.strategies.sql_grammar import SQLStrategy
from exonware.xwquery.compiler.adapters.syntax_adapter import GrammarBasedSQLStrategy


def test_sql_queries() -> None:
    """Test various SQL queries with the new grammar-based parser."""
    print("=" * 60)
    print("SQL GRAMMAR INTEGRATION TEST")
    print("=" * 60)
    # Test queries
    test_queries = [
        "SELECT * FROM users",
        "SELECT id, name FROM users WHERE age > 18",
        "SELECT COUNT(*) FROM orders WHERE status = 'completed'",
        "INSERT INTO users (name, email) VALUES ('John', 'john@example.com')",
        "UPDATE users SET status = 'active' WHERE id = 1",
        "DELETE FROM users WHERE status = 'inactive'",
        "CREATE TABLE products (id INT PRIMARY KEY, name VARCHAR(100))",
        "ALTER TABLE users ADD COLUMN phone VARCHAR(20)",
        "DROP TABLE temp_table"
    ]
    # Initialize grammar-based strategy
    try:
        strategy = SQLStrategy()
        print(f"[OK] Grammar-based SQL strategy initialized")
    except Exception as e:
        print(f"[FAIL] Failed to initialize strategy: {e}")
        return
    # Test each query
    for i, query in enumerate(test_queries, 1):
        print(f"\nTest {i}: {query}")
        print("-" * 40)
        try:
            # Test validation
            is_valid = strategy.validate_query(query)
            print(f"Validation: {'[OK]' if is_valid else '[FAIL]'}")
            if is_valid:
                # Test parsing and execution
                result = strategy.execute(query)
                print(f"Execution: [OK]")
                print(f"Result: {result}")
                # Test query plan
                plan = strategy.get_query_plan(query)
                print(f"Query Plan: {plan}")
            else:
                print(f"Query failed validation")
        except Exception as e:
            print(f"[FAIL] Error: {e}")
    # Test Monaco export
    print(f"\n" + "=" * 40)
    print("MONACO GRAMMAR EXPORT TEST")
    print("=" * 40)
    try:
        monaco_grammar = strategy.export_monaco_grammar()
        print(f"[OK] Monaco grammar exported successfully")
        print(f"Grammar keys: {list(monaco_grammar.keys())}")
        # Show grammar info
        grammar_info = strategy.get_grammar_info()
        print(f"\nGrammar Info:")
        for key, value in grammar_info.items():
            print(f"  {key}: {value}")
    except Exception as e:
        print(f"[FAIL] Monaco export failed: {e}")


def test_grammar_parser_directly() -> None:
    """Test the grammar parser directly."""
    print(f"\n" + "=" * 40)
    print("DIRECT GRAMMAR PARSER TEST")
    print("=" * 40)
    try:
        parser = GrammarBasedSQLStrategy()
        print(f"[OK] Grammar parser initialized")
        # Test simple query
        query = "SELECT * FROM users"
        query_action = parser.parse(query)
        print(f"[OK] Query parsed successfully")
        print(f"QueryAction: {query_action}")
        # Test validation
        is_valid = parser.validate(query)
        print(f"Validation: {'[OK]' if is_valid else '[FAIL]'}")
    except Exception as e:
        print(f"[FAIL] Direct parser test failed: {e}")


def compare_with_old_parser() -> None:
    """Compare new grammar parser with old hand-written parser."""
    print(f"\n" + "=" * 40)
    print("COMPARISON WITH OLD PARSER")
    print("=" * 40)
    # This would compare with the old sql_parser.py
    # For now, just show the benefits
    print("Old Parser (sql_parser.py):")
    print("  - Lines of code: 817")
    print("  - sql_tokenizer.py: 745 lines")
    print("  - Total: 1,562 lines")
    print("  - Hand-written parsing logic")
    print("  - Complex tokenization")
    print("  - Manual AST construction")
    print("\nNew Grammar Parser:")
    print("  - sql.grammar: ~50 lines")
    print("  - sql_grammar.py: ~200 lines")
    print("  - syntax_adapter.py: ~300 lines")
    print("  - Total: ~550 lines")
    print("  - Grammar-driven parsing")
    print("  - Automatic tokenization")
    print("  - Automatic AST generation")
    print("  - Monaco integration")
    print("  - Code reduction: 65%")
if __name__ == "__main__":
    print("Starting SQL Grammar Integration Tests...")
    try:
        test_sql_queries()
        test_grammar_parser_directly()
        compare_with_old_parser()
        print(f"\n" + "=" * 60)
        print("ALL TESTS COMPLETED")
        print("=" * 60)
    except Exception as e:
        print(f"\n[FAIL] Test suite failed: {e}")
        import traceback
        traceback.print_exc()
