use msghandler::{smartpack, Payload, smartpackOptions};

fn main() {
    // Create mixed payload data
    let payloads = vec![
        (
            "message".to_string(),
            Payload::Text("Hello from Rust!".to_string()),
            "text".to_string(),
        ),
        (
            "user_data".to_string(),
            Payload::Dictionary(serde_json::json!({
                "name": "Alice",
                "role": "admin",
                "scores": [95, 88, 92]
            })),
            "dictionary".to_string(),
        ),
        (
            "avatar".to_string(),
            Payload::Binary(vec![0x89, 0x50, 0x4E, 0x47]), // PNG header
            "image".to_string(),
        ),
    ];

    let options = smartpackOptions {
        broker_url: "localhost:4222".to_string(),
        fileserver_url: "http://localhost:8080".to_string(),
        msg_purpose: "chat".to_string(),
        sender_name: "rust-example".to_string(),
        ..Default::default()
    };

    match smartpack("/agent/wine/api/v1/prompt", &payloads, &options) {
        Ok((envelope, json_str)) => {
            println!("=== Envelope Created ===");
            println!("Correlation ID: {}", envelope.correlation_id);
            println!("Message ID: {}", envelope.msg_id);
            println!("Timestamp: {}", envelope.timestamp);
            println!("Subject: {}", envelope.send_to);
            println!("Purpose: {}", envelope.msg_purpose);
            println!("Sender: {}", envelope.sender_name);
            println!("Payloads: {}", envelope.payloads.len());
            println!();

            for payload in &envelope.payloads {
                println!("Payload: {} (type: {}, transport: {}, encoding: {})",
                    payload.dataname,
                    payload.payload_type,
                    payload.transport,
                    payload.encoding);
                println!("  Size: {} bytes", payload.size);
                println!("  Data: {}", if payload.transport == "direct" {
                    &payload.data[..payload.data.len().min(40)]
                } else {
                    &payload.data[..payload.data.len().min(60)]
                });
            }

            println!();
            println!("=== JSON String for Transport Publishing ===");
            println!("{}", json_str);
        }
        Err(e) => {
            eprintln!("Error: {}", e);
        }
    }
}
