//! Steam-User Example - Cookie Login
//!
//! Demonstrates logging on to the Steam CM using existing web session cookies.
//!
//! The cookies themselves never reach the CM. Instead, they are exchanged at
//! `https://steamcommunity.com/chat/clientjstoken` for a short-lived web
//! logon token, which (together with the account name and SteamID returned by
//! that endpoint) authenticates the CM logon.
//!
//! # Usage
//! ```bash
//! STEAM_COOKIES="sessionid=...; steamLoginSecure=..." cargo run --example cookie_login
//! ```

use std::env;

use steam_client::SteamClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    tracing_subscriber::fmt().with_max_level(tracing::Level::INFO).init();

    let cookies = env::var("STEAM_COOKIES").map_err(|_| "Set STEAM_COOKIES env var to your steamcommunity.com cookie string (must include steamLoginSecure)")?;

    let mut client = SteamClient::new(Default::default());

    println!("🍪 Minting web logon token from cookies and connecting to CM...");
    let response = client.log_on_with_cookies(&cookies).await?;

    println!("✅ Logged in as {}", response.steam_id.steam3());
    println!("   Result: {:?}", response.eresult);
    if let Some(ref ip) = response.public_ip {
        println!("   Public IP: {}", ip);
    }

    // Drain a few events to show the connection is live.
    for _ in 0..5 {
        match client.poll_event_timeout(std::time::Duration::from_secs(2)).await {
            Ok(Some(event)) => println!("📬 Event: {:?}", event),
            Ok(None) => break,
            Err(_) => break,
        }
    }

    client.log_off().await?;
    println!("👋 Logged off");

    Ok(())
}
