//! Example: typed request structs for gateway CRUD + SQL.
use athena_rs::{
    AthenaClient, Condition, ConditionOperator, GatewayDeleteRequest, GatewayFetchRequest,
    GatewayInsertRequest, GatewaySqlRequest, GatewayUpdateRequest,
};
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?;

    let fetch_request = GatewayFetchRequest::new("users")
        .columns(["id", "email"])
        .where_condition(Condition::new(
            "status",
            ConditionOperator::Eq,
            vec![json!("active")],
        ))
        .limit(20)
        .offset(0);
    let fetched = client.fetch_request(fetch_request).await?;
    println!("Fetched rows: {}", fetched.rows.len());

    let inserted = client
        .insert_request(GatewayInsertRequest::insert(
            "audit_logs",
            json!({ "event": "sdk_typed_insert", "scope": "examples" }),
        ))
        .await?;
    println!("Inserted rows: {}", inserted.rows.len());

    let update_req = GatewayUpdateRequest::update("users", json!({ "status": "active" }))
        .where_condition(Condition::new(
            "email",
            ConditionOperator::Eq,
            vec![json!("new.user@example.com")],
        ));
    let updated = client.update_request(update_req).await?;
    println!("Updated rows: {}", updated.rows.len());

    let delete_req = GatewayDeleteRequest::delete("audit_logs").where_condition(Condition::new(
        "event",
        ConditionOperator::Eq,
        vec![json!("sdk_typed_insert")],
    ));
    let deleted = client.delete_request(delete_req).await?;
    println!("Deleted rows: {}", deleted.rows.len());

    let sql = client
        .sql_request(GatewaySqlRequest::sql("SELECT 1 AS ok"))
        .await?;
    println!("SQL rows: {}", sql.rows.len());

    Ok(())
}
