//! TUI constants and configuration

use ratatui::style::Color;

// ============================================================================
// Layout Constants
// ============================================================================

/// Layout constraints for main UI sections
pub mod layout {
    pub const TITLE_HEIGHT: u16 = 3;
    pub const SUMMARY_HEIGHT: u16 = 6; // 3 columns with 4 lines each
    pub const FOOTER_HEIGHT: u16 = 3;
    pub const MIN_CHART_HEIGHT: u16 = 8; // Reduced to fit cache stats in summary
    /// Minimum terminal height (lines) before the log panel is shown
    pub const MIN_HEIGHT_FOR_LOGS: u16 = 40;
    /// Number of log lines visible in the log panel
    pub const LOG_WINDOW_HEIGHT: u16 = 10;
}

// ============================================================================
// Chart Configuration
// ============================================================================

/// Chart configuration
pub mod chart {
    pub const HISTORY_POINT_COUNT: usize = 60;
    pub const HISTORY_POINTS: f64 = HISTORY_POINT_COUNT as f64;
    pub const MIN_THROUGHPUT: f64 = 1_048_576.0; // 1 MiB/s

    pub const X_LABEL_0S: &str = "0s";
    pub const X_LABEL_5S: &str = "5s";
    pub const X_LABEL_10S: &str = "10s";
    pub const X_LABEL_15S: &str = "15s";

    pub const Y_LABEL_ZERO: &str = "0";
    pub const TITLE: &str = "Throughput (15s)";
}

// ============================================================================
// Color Palette
// ============================================================================

/// Color palette for backends
pub const BACKEND_COLORS: &[Color] = &[
    Color::Green,
    Color::Cyan,
    Color::Yellow,
    Color::Magenta,
    Color::Red,
    Color::Blue,
];

/// UI text styles
pub mod styles {
    use ratatui::style::Color;

    pub const LABEL: Color = Color::Gray;
    pub const VALUE_PRIMARY: Color = Color::Green;
    pub const VALUE_SECONDARY: Color = Color::Yellow;
    pub const VALUE_INFO: Color = Color::Cyan;
    pub const VALUE_NEUTRAL: Color = Color::Blue;
    pub const BORDER_ACTIVE: Color = Color::Cyan;
    pub const BORDER_NORMAL: Color = Color::White;
}

// ============================================================================
// Text Constants
// ============================================================================

/// UI text constants
pub mod text {
    pub const ARROW_UP: &str = "↑";
    pub const ARROW_DOWN: &str = "↓";
    pub const DEFAULT_CMD_RATE: &str = "0.0";
    pub const DEFAULT_THROUGHPUT: &str = "0 B/s";
}

// ============================================================================
// Throughput Formatting Constants
// ============================================================================

/// Constants for throughput calculations and formatting
pub mod throughput {
    pub const HUNDRED_MIB: f64 = 104_857_600.0;
    pub const TEN_MIB: f64 = 10_485_760.0;
    pub const ONE_MIB: f64 = 1_048_576.0;
    pub const HUNDRED_KIB: f64 = 102_400.0;
    pub const TEN_KIB: f64 = 10_240.0;
    pub const ONE_KIB: f64 = 1_024.0;
}

#[cfg(test)]
#[allow(clippy::float_cmp)] // Constant tests intentionally compare exact float literals defined in the UI module.
mod tests {
    use super::*;

    #[test]
    fn test_layout_constants() {
        assert_eq!(layout::TITLE_HEIGHT, 3);
        assert_eq!(layout::SUMMARY_HEIGHT, 6);
        assert_eq!(layout::FOOTER_HEIGHT, 3);
        assert_eq!(layout::MIN_CHART_HEIGHT, 8);
    }

    #[test]
    fn test_chart_constants() {
        assert_eq!(chart::HISTORY_POINT_COUNT, 60);
        assert_eq!(chart::HISTORY_POINTS, 60.0);
        assert_eq!(chart::MIN_THROUGHPUT, 1_048_576.0);
        assert_eq!(chart::X_LABEL_0S, "0s");
        assert_eq!(chart::X_LABEL_5S, "5s");
        assert_eq!(chart::X_LABEL_10S, "10s");
        assert_eq!(chart::X_LABEL_15S, "15s");
        assert_eq!(chart::Y_LABEL_ZERO, "0");
        assert_eq!(chart::TITLE, "Throughput (15s)");
    }

    #[test]
    fn test_backend_colors() {
        assert_eq!(BACKEND_COLORS.len(), 6);
        assert_eq!(BACKEND_COLORS[0], Color::Green);
        assert_eq!(BACKEND_COLORS[1], Color::Cyan);
        assert_eq!(BACKEND_COLORS[2], Color::Yellow);
        assert_eq!(BACKEND_COLORS[3], Color::Magenta);
        assert_eq!(BACKEND_COLORS[4], Color::Red);
        assert_eq!(BACKEND_COLORS[5], Color::Blue);
    }

    #[test]
    fn test_style_colors() {
        assert_eq!(styles::LABEL, Color::Gray);
        assert_eq!(styles::VALUE_PRIMARY, Color::Green);
        assert_eq!(styles::VALUE_SECONDARY, Color::Yellow);
        assert_eq!(styles::VALUE_INFO, Color::Cyan);
        assert_eq!(styles::VALUE_NEUTRAL, Color::Blue);
        assert_eq!(styles::BORDER_ACTIVE, Color::Cyan);
        assert_eq!(styles::BORDER_NORMAL, Color::White);
    }

    #[test]
    fn test_text_constants() {
        assert_eq!(text::ARROW_UP, "↑");
        assert_eq!(text::ARROW_DOWN, "↓");
        assert_eq!(text::DEFAULT_CMD_RATE, "0.0");
        assert_eq!(text::DEFAULT_THROUGHPUT, "0 B/s");
    }

    #[test]
    fn test_throughput_constants() {
        assert_eq!(throughput::HUNDRED_MIB, 104_857_600.0);
        assert_eq!(throughput::TEN_MIB, 10_485_760.0);
        assert_eq!(throughput::ONE_MIB, 1_048_576.0);
        assert_eq!(throughput::HUNDRED_KIB, 102_400.0);
        assert_eq!(throughput::TEN_KIB, 10_240.0);
        assert_eq!(throughput::ONE_KIB, 1_024.0);
    }

    #[test]
    fn test_throughput_relationships() {
        // Verify mathematical relationships
        assert_eq!(throughput::HUNDRED_MIB, throughput::TEN_MIB * 10.0);
        assert_eq!(throughput::TEN_MIB, throughput::ONE_MIB * 10.0);
        assert_eq!(throughput::ONE_MIB, throughput::ONE_KIB * 1024.0);
        assert_eq!(throughput::HUNDRED_KIB, throughput::TEN_KIB * 10.0);
        assert_eq!(throughput::TEN_KIB, throughput::ONE_KIB * 10.0);
    }
}
