/// Example: Entity Event Polling
use lattice::prelude::*;
use std::env;

#[tokio::main]
async fn main() -> Result<(), ApiError> {
    let client = ApiClientBuilder::new(
        &env::var("LATTICE_BASE_URL").unwrap_or("https://api.lattice.example.com".into())
    )
    .api_key(&env::var("LATTICE_API_KEY").unwrap_or("your-key".into()))
    .build()?;

    println!("=== Entity Event Polling ===\n");

    let mut session_token = String::new();

    for attempt in 1..=2 {
        println!("[{}] Polling...", attempt);

        let request = EntityEventRequest {
            session_token: session_token.clone(),
            batch_size: Some(10),
        };

        match client.entities.long_poll_entity_events(&request, None).await {
            Ok(response) => {
                if let Some(token) = response.session_token {
                    session_token = token;
                    println!("✓ Session active");
                }

                if let Some(events) = response.entity_events {
                    println!("  Events: {}", events.len());
                } else {
                    println!("  No events");
                }
            }
            Err(ApiError::RequestTimeoutError { .. }) => {
                println!("  ⏱  Timeout");
            }
            Err(e) => println!("✗ Error: {:?}", e),
        }

        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
    }

    println!("\n=== Completed ===");
    Ok(())
}
