//! Example: native Postgres RPC calls via `AthenaClient::rpc`.
//!
//! This demonstrates:
//! - calling RPC with no args
//! - calling RPC with JSON args (including arrays)
//! - set-returning filtering/select/order/pagination/count
use athena_rs::{AthenaClient, OrderDirection};
use serde_json::json;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    let client = AthenaClient::new("http://localhost:4052", "secret", "reporting").await?;

    // Equivalent to: POST /gateway/rpc { "function": "hello_world", "args": {} }
    let hello = client.rpc("hello_world", json!({})).execute().await?;
    println!("hello_world rows: {}", hello.rows.len());

    // With scalar argument payload.
    let city = client
        .rpc("echo_city", json!({ "name": "The Shire" }))
        .execute()
        .await?;
    println!("echo_city rows: {}", city.rows.len());

    // With array arguments.
    let cities = client
        .rpc("echo_cities", json!({ "names": ["The Shire", "Mordor"] }))
        .execute()
        .await?;
    println!("echo_cities rows: {}", cities.rows.len());

    // Set-returning RPC with projection, filters, ordering, paging, and exact count.
    let filtered = client
        .rpc("echo_all_cities", json!({}))
        .select("name,population")
        .eq("name", "The Shire")
        .order("name", OrderDirection::Asc)
        .limit(10)
        .offset(0)
        .count_exact()
        .execute()
        .await?;

    println!(
        "echo_all_cities filtered rows: {} (count={:?})",
        filtered.rows.len(),
        filtered.count
    );

    Ok(())
}
