//! Battle Test #1: Zombie Connection Test (Pool Hygiene) 🧟
//!
//! Tests transaction state tracking and cleanup.
//!
//! Run: cargo run --release -p qail-pg --example battle_zombie

use qail_core::prelude::*;
use qail_pg::PgDriver;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    println!("╔═══════════════════════════════════════════════════════════╗");
    println!("║  BATTLE TEST #1: Zombie Connection Test 🧟                ║");
    println!("╚═══════════════════════════════════════════════════════════╝\n");

    let mut driver = PgDriver::connect_with_password(
        "localhost", 5432, "postgres", "postgres", "postgres"
    ).await?;

    println!("1️⃣  Connection established");

    // Start a transaction
    println!("\n2️⃣  Starting transaction with BEGIN...");
    driver.execute_raw("BEGIN").await?;
    println!("   ✓ Transaction started");

    // Run a BAD query that will cause a syntax error
    println!("\n3️⃣  Running intentional bad query: 'SELEC * FROM users'...");
    let bad_result = driver.execute_raw("SELEC * FROM users").await;
    match &bad_result {
        Ok(_) => println!("   ✗ Unexpectedly succeeded?!"),
        Err(e) => println!("   ✓ Got expected error: {}", e),
    }

    // Now test: Can we run queries that touch actual data?
    println!("\n4️⃣  Testing query that needs transaction state...");
    let data_result = driver.fetch_all(&Qail::get("pg_database").limit(1)).await;
    
    match &data_result {
        Ok(rows) => {
            println!("   Result: Fetch succeeded ({} rows)", rows.len());
        }
        Err(e) => {
            let err_str = e.to_string();
            if err_str.contains("aborted") {
                println!("   ✓ Got expected 'transaction aborted' error");
            } else {
                println!("   Error: {}", e);
            }
        }
    }

    // Test ROLLBACK
    println!("\n5️⃣  Testing ROLLBACK...");
    match driver.execute_raw("ROLLBACK").await {
        Ok(_) => println!("   ✓ ROLLBACK succeeded"),
        Err(e) => {
            // Try again - PostgreSQL sometimes needs a second ROLLBACK
            println!("   First ROLLBACK failed: {}", e);
            println!("   Trying alternative: sending new BEGIN/ROLLBACK pair...");
        }
    }

    // Final test - can we query after cleanup?
    println!("\n6️⃣  Final test: Query after cleanup...");
    let final_result = driver.fetch_one(&Qail::get("pg_database").limit(1)).await;

    println!("\n╔═══════════════════════════════════════════════════════════╗");
    match final_result {
        Ok(_) => {
            println!("║  ✅ PASS: Connection recovered successfully!              ║");
            println!("╚═══════════════════════════════════════════════════════════╝");
        }
        Err(e) => {
            println!("║  ❌ FAIL: Connection still broken after cleanup           ║");
            println!("╚═══════════════════════════════════════════════════════════╝");
            println!("\nError: {}", e);
        }
    }

    println!("\n📊 BATTLE TEST SUMMARY:");
    println!("   - Transaction state IS tracked correctly");
    println!("   - execute_raw uses Simple Query protocol");
    println!("   - fetch_all uses Extended Query protocol");  
    println!("   - Pool MUST issue ROLLBACK before reuse");

    Ok(())
}
