//! Theme management for syntaxis-tui
//!
//! This module provides theme support for the syntaxis-tui application,
//! leveraging the tools_tui_shared theme system and extending it with application-specific styling.
//!
//! # Theme Selection
//!
//! The application supports multiple color themes:
//! - `default` - Professional cyan theme (recommended)
//! - `dark` - Dark mode theme
//! - `light` - Light mode with inverted colors
//! - `minimal` - High contrast white theme
//! - `professional` - Corporate blue theme
//!
//! # Usage
//!
//! ```no_run
//! use syntaxis_tui::theme::AppTheme;
//!
//! let theme = AppTheme::default();
//! // Use theme colors in rendering
//! ```

use ratatui::style::{Color, Modifier, Style};
use tools_tui_shared::config::Theme;

/// Application-specific theme configuration
///
/// Wraps the tools_tui_shared Theme and provides application-specific styling for syntaxis-tui.
#[derive(Debug, Clone, Copy)]
pub struct AppTheme {
    /// Base theme from tools_tui_shared
    pub base: Theme,
    /// Whether to use bold text for headers
    #[allow(dead_code)]
    pub bold_headers: bool,
    /// Whether to use italic text for metadata
    #[allow(dead_code)]
    pub italic_metadata: bool,
}

#[allow(dead_code)]
impl AppTheme {
    /// Create a new theme with default settings
    pub fn new(base: Theme) -> Self {
        Self {
            base,
            bold_headers: true,
            italic_metadata: false,
        }
    }

    /// Create theme with default (cyan) colors
    pub fn default_theme() -> Self {
        Self::new(Theme::default())
    }

    /// Create theme with dark mode colors
    pub fn dark() -> Self {
        Self::new(Theme::dark())
    }

    /// Create theme with light mode colors
    pub fn light() -> Self {
        Self::new(Theme::light())
    }

    /// Create theme with minimal (high contrast) colors
    pub fn minimal() -> Self {
        Self::new(Theme::minimal())
    }

    /// Create theme with professional (blue) colors
    pub fn professional() -> Self {
        Self::new(Theme::professional())
    }

    // ========================================================================
    // HEADER STYLING
    // ========================================================================

    /// Get style for application header
    pub fn header_style(&self) -> Style {
        let color = self.base.title_color();
        let mut style = Style::default().fg(color);
        if self.bold_headers {
            style = style.add_modifier(Modifier::BOLD);
        }
        style
    }

    /// Get style for section headers (screen titles)
    pub fn section_header_style(&self) -> Style {
        let color = self.base.primary;
        let mut style = Style::default().fg(color);
        if self.bold_headers {
            style = style.add_modifier(Modifier::BOLD);
        }
        style
    }

    // ========================================================================
    // LIST/SELECTION STYLING
    // ========================================================================

    /// Get style for list items (normal)
    pub fn list_item_style(&self) -> Style {
        Style::default().fg(self.base.text_color())
    }

    /// Get style for selected list item
    pub fn selected_item_style(&self) -> Style {
        Style::default()
            .bg(self.base.selection_color())
            .fg(self.base.background())
            .add_modifier(Modifier::BOLD)
    }

    /// Get style for hovered/highlighted item
    pub fn hovered_item_style(&self) -> Style {
        Style::default()
            .fg(self.base.primary)
            .add_modifier(Modifier::BOLD)
    }

    // ========================================================================
    // STATUS INDICATORS
    // ========================================================================

    /// Get style for success status
    pub fn success_style(&self) -> Style {
        Style::default().fg(self.base.success_color())
    }

    /// Get style for warning status
    pub fn warning_style(&self) -> Style {
        Style::default().fg(self.base.warning_color())
    }

    /// Get style for error status
    pub fn error_style(&self) -> Style {
        Style::default()
            .fg(self.base.error_color())
            .add_modifier(Modifier::BOLD)
    }

    /// Get style for loading status
    pub fn loading_style(&self) -> Style {
        Style::default()
            .fg(self.base.primary)
            .add_modifier(Modifier::DIM)
    }

    // ========================================================================
    // METADATA STYLING
    // ========================================================================

    /// Get style for metadata text (secondary information)
    pub fn metadata_style(&self) -> Style {
        let mut style = Style::default().fg(self.base.text_muted_color());
        if self.italic_metadata {
            style = style.add_modifier(Modifier::ITALIC);
        }
        style
    }

    /// Get style for subtle/dimmed text
    pub fn dim_style(&self) -> Style {
        Style::default()
            .fg(self.base.text_muted_color())
            .add_modifier(Modifier::DIM)
    }

    // ========================================================================
    // BORDER AND BLOCK STYLING
    // ========================================================================

    /// Get color for borders
    pub fn border_color(&self) -> Color {
        self.base.border_color()
    }

    /// Get style for borders
    pub fn border_style(&self) -> Style {
        Style::default().fg(self.border_color())
    }

    /// Get color for background elements
    pub fn background_color(&self) -> Color {
        self.base.background()
    }

    // ========================================================================
    // PHASE-SPECIFIC COLORS
    // ========================================================================

    /// Get color for planning phase
    pub fn planning_color(&self) -> Color {
        self.base.primary
    }

    /// Get color for development phase
    pub fn development_color(&self) -> Color {
        self.base.success_color()
    }

    /// Get color for testing phase
    pub fn testing_color(&self) -> Color {
        self.base.warning_color()
    }

    /// Get color for completed phase
    pub fn completed_color(&self) -> Color {
        self.base.success_color()
    }

    /// Get color for archived phase
    pub fn archived_color(&self) -> Color {
        self.base.text_muted_color()
    }

    // ========================================================================
    // HELPER METHODS
    // ========================================================================

    /// Check if theme is dark
    pub fn is_dark(&self) -> bool {
        self.base.is_dark()
    }

    /// Enable bold headers
    pub fn with_bold_headers(mut self) -> Self {
        self.bold_headers = true;
        self
    }

    /// Disable bold headers
    pub fn without_bold_headers(mut self) -> Self {
        self.bold_headers = false;
        self
    }

    /// Enable italic metadata
    pub fn with_italic_metadata(mut self) -> Self {
        self.italic_metadata = true;
        self
    }

    /// Disable italic metadata
    pub fn without_italic_metadata(mut self) -> Self {
        self.italic_metadata = false;
        self
    }
}

impl Default for AppTheme {
    fn default() -> Self {
        Self::default_theme()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_theme() {
        let theme = AppTheme::default();
        assert!(theme.bold_headers);
        assert!(!theme.italic_metadata);
        assert!(!theme.is_dark() || theme.is_dark()); // Just verify method works
    }

    #[test]
    fn test_theme_variants() {
        let _default = AppTheme::default_theme();
        let _dark = AppTheme::dark();
        let _light = AppTheme::light();
        let _minimal = AppTheme::minimal();
        let _professional = AppTheme::professional();
    }

    #[test]
    fn test_theme_style_methods() {
        let theme = AppTheme::default();

        // Just verify all methods return styles without panicking
        let _ = theme.header_style();
        let _ = theme.section_header_style();
        let _ = theme.list_item_style();
        let _ = theme.selected_item_style();
        let _ = theme.success_style();
        let _ = theme.error_style();
        let _ = theme.metadata_style();
        let _ = theme.border_style();
    }

    #[test]
    fn test_theme_builder_methods() {
        let theme = AppTheme::default()
            .with_bold_headers()
            .with_italic_metadata();

        assert!(theme.bold_headers);
        assert!(theme.italic_metadata);

        let theme = theme.without_bold_headers().without_italic_metadata();
        assert!(!theme.bold_headers);
        assert!(!theme.italic_metadata);
    }

    #[test]
    fn test_phase_colors() {
        let theme = AppTheme::default();

        // Just verify phase color methods work
        let _ = theme.planning_color();
        let _ = theme.development_color();
        let _ = theme.testing_color();
        let _ = theme.completed_color();
        let _ = theme.archived_color();
    }
}
