use std::time::Duration;

use crate::resil::{BreakerConfig, BreakerPolicyConfig};

/// Circuit breaker settings for Redis cache commands.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct RedisBreakerConfig {
    /// Whether Redis commands and connection acquisition are protected.
    pub enabled: bool,
    /// Basic breaker thresholds and reset timeout.
    pub breaker: BreakerConfig,
    /// Rolling-window and adaptive rejection policy.
    pub policy: BreakerPolicyConfig,
}

impl RedisBreakerConfig {
    /// Creates a production-oriented Redis breaker profile.
    pub fn go_zero_defaults() -> Self {
        Self {
            enabled: true,
            breaker: BreakerConfig::default(),
            policy: BreakerPolicyConfig::default(),
        }
    }

    /// Creates a Redis breaker profile optimized for fast local tests.
    pub fn fast_failure(failure_threshold: u32, reset_timeout: Duration) -> Self {
        Self {
            enabled: true,
            breaker: BreakerConfig {
                failure_threshold,
                reset_timeout,
            },
            policy: BreakerPolicyConfig {
                min_request_count: u64::MAX,
                drop_ratio_percent: 0,
                failure_ratio_percent: 100,
                force_pass_interval: reset_timeout,
                ..BreakerPolicyConfig::default()
            },
        }
    }
}
