use crate::adaptive_rate::LATENCY_WINDOW_SIZE;
use std::time::{Duration, Instant};

/// Per-host state tracked by the adaptive rate limiter.
#[derive(Debug)]
pub(crate) struct HostState {
    /// Recent response times for spike detection.
    pub(crate) recent_latencies: Vec<Duration>,
    /// EWMA (Exponentially Weighted Moving Average) of response times.
    pub(crate) ewma_latency: Option<Duration>,
    /// When the host is next available for requests.
    pub(crate) next_ready_at: Instant,
    /// Current backoff exponent for exponential backoff.
    pub(crate) backoff_exponent: u32,
    /// Count of recent rate limit responses (429).
    pub(crate) rate_limit_count: usize,
    /// Count of recent error responses (403, 503).
    pub(crate) error_count: usize,
    /// Total requests recorded in current window.
    pub(crate) total_requests: usize,
    /// Whether the host is currently considered rate-limited.
    pub(crate) is_throttled: bool,
    /// Timestamp of the last successful (non-rate-limited) response.
    pub(crate) last_success_at: Option<Instant>,
}

impl Default for HostState {
    fn default() -> Self {
        Self {
            recent_latencies: Vec::with_capacity(LATENCY_WINDOW_SIZE),
            ewma_latency: None,
            next_ready_at: Instant::now(),
            backoff_exponent: 0,
            rate_limit_count: 0,
            error_count: 0,
            total_requests: 0,
            is_throttled: false,
            last_success_at: None,
        }
    }
}
