//! Mirrors `sdk-ts/scripts/db-select-one.ts`: connect to a sqlite database
//! by id and run `SELECT 1`.
//!
//! Picks the database id from the `HEYO_DB_ID` env var. Skips if missing.
//!
//! Run with:
//!   HEYO_API_KEY=... HEYO_DB_ID=db-... cargo test --test db_select_one -- --ignored --nocapture

mod common;

use heyo_sdk::{
    ConnectionScope, ConnectionTokenOptions, Database, ExecOptions, SqlValue,
};

#[tokio::test]
#[ignore]
async fn db_select_one() {
    common::load_dotenv();
    let Some(opts) = common::client_options() else {
        eprintln!("[db] skipping — HEYO_API_KEY not set");
        return;
    };
    let Ok(db_id) = std::env::var("HEYO_DB_ID") else {
        eprintln!("[db] skipping — HEYO_DB_ID not set");
        return;
    };

    println!("[db] base={} db={}", common::base_url(), db_id);

    let db = Database::get(&db_id, opts.clone())
        .await
        .expect("Database::get");

    // Path 1: direct exec via SDK
    let result = db
        .exec("SELECT 1", Vec::<SqlValue>::new(), ExecOptions::default())
        .await
        .expect("exec");
    println!("[sdk] columns: {:?}", result.columns);
    println!("[sdk] rows:    {:?}", result.rows);
    assert_eq!(result.rows.len(), 1);

    // Path 2: mint a connection token (don't run it through libsql here;
    // the goal is to prove the connect endpoint returns a usable token).
    let conn = db
        .connect_token(ConnectionTokenOptions {
            ttl_seconds: Some(300),
            scopes: Some(vec![ConnectionScope::Read]),
        })
        .await
        .expect("connect_token");
    println!(
        "[libsql] url={} (token len={})",
        conn.url,
        conn.auth_token.len()
    );
    assert!(!conn.auth_token.is_empty(), "auth_token should be present");
    assert!(conn.url.starts_with("http"), "url should be http(s)://…");
}
