//! Aggregated statistics for API requests in a session.

use chrono::{DateTime, Utc};

/// Aggregated statistics for API requests in a session
#[derive(Debug, Clone, Default)]
pub struct ApiStats {
    /// Session ID
    pub session_id: String,
    /// Total API requests
    pub request_count: i64,
    /// Total input tokens
    pub tokens_input: i64,
    /// Total output tokens
    pub tokens_output: i64,
    /// Total cache read tokens
    pub tokens_cache_read: i64,
    /// Total cache write tokens
    pub tokens_cache_write: i64,
    /// Total cost in USD
    pub cost_usd: f64,
    /// First request timestamp
    pub first_request_ts: Option<DateTime<Utc>>,
    /// Last request timestamp
    pub last_request_ts: Option<DateTime<Utc>>,
    /// Context size of most recent request (input + cache_read + cache_write)
    pub context_latest: i64,
    /// Maximum context size across all requests
    pub context_peak: i64,
}

impl ApiStats {
    /// Total tokens across all categories
    pub fn total_tokens(&self) -> i64 {
        self.tokens_input + self.tokens_output + self.tokens_cache_read + self.tokens_cache_write
    }

    /// Duration from first to last request
    pub fn duration(&self) -> Option<chrono::Duration> {
        match (self.first_request_ts, self.last_request_ts) {
            (Some(first), Some(last)) => Some(last - first),
            _ => None,
        }
    }
}
