use crate::models::query_result::QueryResult;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "role", rename_all = "snake_case")]
pub enum ChatMessage {
    User { id: String, text: String, created_at: i64 },
    Assistant { id: String, text: String, created_at: i64 },
    ToolCall { id: String, tool: String, input_json: String, created_at: i64 },
    ToolResult {
        id: String,
        tool: String,
        is_error: bool,
        text: Option<String>,
        result: Option<QueryResult>,
        created_at: i64,
    },
}

/// Approximate model-context budget usage for the current chat thread.
/// Measured in characters of the serialized history that we send to the LLM.
/// Token estimate ≈ used_chars / 3 for mixed Cyrillic/ASCII content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContextUsage {
    pub used_chars: u64,
    pub budget_chars: u64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatTurnResult {
    pub messages: Vec<ChatMessage>,
    pub usage: ContextUsage,
}
