//! API types for ComfyUI SDK

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

/// Queue status response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueueStatus {
    pub queue_running: Vec<QueueItem>,
    pub queue_pending: Vec<QueueItem>,
}

/// Queue item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueueItem {
    pub prompt_id: String,
    pub number: u32,
    pub outputs: HashMap<String, serde_json::Value>,
}

/// Prompt request
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptRequest {
    pub prompt: HashMap<String, serde_json::Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extra_data: Option<HashMap<String, serde_json::Value>>,
}

/// Prompt response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PromptResponse {
    pub prompt_id: String,
    pub number: u32,
    pub node_errors: HashMap<String, serde_json::Value>,
}

/// History item
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryItem {
    pub prompt: Vec<serde_json::Value>,
    pub outputs: HashMap<String, HashMap<String, Vec<HistoryOutput>>>,
    pub status: HistoryStatus,
}

/// History output
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryOutput {
    pub filename: String,
    pub subfolder: String,
    #[serde(rename = "type")]
    pub output_type: String,
}

/// History status
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HistoryStatus {
    pub status_str: String,
    pub completed: bool,
    pub messages: Vec<Vec<serde_json::Value>>,
}

/// System stats
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemStats {
    pub system: SystemInfo,
    pub devices: Vec<DeviceInfo>,
}

/// System information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SystemInfo {
    pub os: String,
    pub python_version: String,
    pub embedded_python: bool,
}

/// Device information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeviceInfo {
    pub name: String,
    pub device_type: String,
    pub vram_total: u64,
    pub vram_free: u64,
    pub torch_vram_total: u64,
    pub torch_vram_free: u64,
}

/// Object info response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectInfo {
    #[serde(flatten)]
    pub nodes: HashMap<String, NodeInfo>,
}

/// Node information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeInfo {
    pub input: NodeInputInfo,
    pub output: Vec<String>,
    pub output_is_list: Vec<bool>,
    pub output_name: Vec<String>,
    pub name: String,
    pub display_name: String,
    pub description: String,
    pub category: String,
    pub python_module: String,
}

/// Node input information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NodeInputInfo {
    pub required: HashMap<String, Vec<serde_json::Value>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub optional: Option<HashMap<String, Vec<serde_json::Value>>>,
}

/// Upload image response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UploadImageResponse {
    pub name: String,
    pub subfolder: String,
    #[serde(rename = "type")]
    pub image_type: String,
}

/// View metadata response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ViewMetadata {
    #[serde(flatten)]
    pub metadata: HashMap<String, serde_json::Value>,
}
