/// Example: Task Management
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!("=== Task Management ===\n");

    // Create a task
    let task_creation = TaskCreation {
        task_id: Some("example-task-001".into()),
        display_name: Some("Example Task".into()),
        description: Some("This is an example task".into()),
        specification: None,
        author: None,
        relations: None,
        is_executed_elsewhere: None,
        initial_entities: None,
    };

    println!("1. Creating task...");
    let task_id = match client.tasks.create_task(&task_creation, None).await {
        Ok(task) => {
            println!("✓ Task created!");
            task.version
                .and_then(|v| v.task_id.clone())
                .unwrap_or_else(|| "unknown".into())
        }
        Err(e) => {
            println!("✗ Error: {:?}", e);
            return Ok(());
        }
    };

    println!("  Task ID: {}\n", task_id);

    // Get task
    println!("2. Retrieving task...");
    match client.tasks.get_task(&task_id, None).await {
        Ok(task) => {
            println!("✓ Task retrieved!");
            if let Some(name) = task.display_name {
                println!("  Name: {}", name);
            }
        }
        Err(e) => println!("✗ Error: {:?}", e),
    }

    println!("\n=== Examples completed ===");
    Ok(())
}
