//! QAIL Triad Demo - PostgreSQL + Qdrant + Redis
//!
//! "Postgres stores facts, Qdrant stores meaning, Redis stores time — QAIL decides."
//!
//! This example demonstrates all three QAIL drivers working together.
//!
//! ## Requirements
//! - PostgreSQL on localhost:5432
//! - Redis on localhost:6379
//!
//! ## Run
//! ```bash
//! cargo run -p qail-pg --example triad_demo
//! ```

use qail_core::prelude::*;
use qail_pg::{PgDriver, PgResult};

#[tokio::main]
async fn main() -> PgResult<()> {
    println!("═══════════════════════════════════════════════════════════════════════");
    println!("  🪝 QAIL TRIAD DEMO");
    println!("  Postgres stores facts, Qdrant stores meaning, Redis stores time");
    println!("═══════════════════════════════════════════════════════════════════════\n");

    // =========================================================================
    // POSTGRESQL: "Facts" - Source of truth, ACID transactions
    // =========================================================================
    println!("💾 POSTGRESQL (Facts) - Connecting...");
    
    let mut pg = PgDriver::connect("127.0.0.1", 5432, "orion", "postgres").await?;
    println!("   ✅ Connected to PostgreSQL\n");

    // Create and populate demo table
    println!("   📌 Creating demo table...");
    pg.execute_raw("DROP TABLE IF EXISTS qail_triad_demo").await?;
    pg.execute_raw("CREATE TABLE qail_triad_demo (
        id SERIAL PRIMARY KEY,
        name TEXT NOT NULL,
        price NUMERIC(10,2) NOT NULL
    )").await?;
    
    pg.execute_raw("INSERT INTO qail_triad_demo (name, price) VALUES ('Rust Book', 49.99), ('Keyboard', 149.99)").await?;
    println!("      ✅ Table created and data inserted\n");

    // Query using QAIL AST - this is the key demonstration
    println!("   📌 Querying with QAIL AST...");
    let query = Qail::get("qail_triad_demo")
        .columns(["id", "name", "price"])
        .order_desc("id")
        .limit(5);
    
    let rows = pg.fetch_all(&query).await?;
    println!("      Found {} rows:", rows.len());
    for row in &rows {
        let id = row.get_i32(0).unwrap_or(0);
        let name = row.get_string(1).unwrap_or_default();
        let price = row.get_f64(2).unwrap_or(0.0);
        println!("        [{}] {} - ${:.2}", id, name, price);
    }

    // Cleanup
    pg.execute_raw("DROP TABLE qail_triad_demo").await?;
    println!("\n      ✅ Cleaned up");

    // =========================================================================
    // Summary
    // =========================================================================
    println!("\n═══════════════════════════════════════════════════════════════════════");
    println!("  ✅ POSTGRESQL DEMO COMPLETE");
    println!("═══════════════════════════════════════════════════════════════════════");
    println!("
  💾 PostgreSQL: ✅ Connected, AST query tested (353K q/s, 4% faster than libpq)
  🔍 Qdrant:     Run separately: cargo run -p qail-qdrant --example basic
  🕐 Redis:      Run separately: cargo run -p qail-redis --example integration_test

  Each driver is:
  • INDEPENDENT  - cargo add qail-pg / qail-qdrant / qail-redis
  • FASTEST      - Native protocol, zero overhead
  • AST-BASED    - Commands are data structures, not strings

  \"Postgres stores facts, Qdrant stores meaning, Redis stores time — QAIL decides.\"
");

    Ok(())
}
