//! # Application Layout Widgets (R-WIDGET-COMPOSITION)
//!
//! Common widgets and components that follow the R-WIDGET-COMPOSITION pattern:
//! - Pure rendering functions
//! - Immutable state (takes `&self`)
//! - No internal mutation
//! - Composable design

use crate::config::Theme;
use ratatui::prelude::*;
use ratatui::widgets::{Block, Borders, Paragraph};

/// Application header widget
///
/// Renders a title bar at the top of the application.
/// Follows R-WIDGET-COMPOSITION pattern - pure rendering.
#[derive(Debug, Clone)]
pub struct AppHeader {
    /// Header title text
    title: String,
}

impl AppHeader {
    /// Create a new header widget
    #[must_use]
    pub fn new(title: impl Into<String>) -> Self {
        Self {
            title: title.into(),
        }
    }

    /// Render the header widget
    ///
    /// # Arguments
    ///
    /// * `f` - Terminal frame
    /// * `area` - Layout area for the header
    /// * `theme` - Color theme
    pub fn render(&self, f: &mut Frame<'_>, area: Rect, theme: &Theme) {
        let header = Paragraph::new(self.title.clone())
            .style(Style::default().fg(theme.primary).bold())
            .alignment(Alignment::Center)
            .block(
                Block::default()
                    .borders(Borders::BOTTOM)
                    .border_style(Style::default().fg(theme.secondary)),
            );

        f.render_widget(header, area);
    }
}

/// Application footer widget
///
/// Renders a help/status bar at the bottom of the application.
/// Follows R-WIDGET-COMPOSITION pattern - pure rendering.
#[derive(Debug, Clone)]
pub struct AppFooter {
    /// Help/status text to display
    content: String,
}

impl AppFooter {
    /// Create a new footer widget
    #[must_use]
    pub fn new(content: impl Into<String>) -> Self {
        Self {
            content: content.into(),
        }
    }

    /// Render the footer widget
    ///
    /// # Arguments
    ///
    /// * `f` - Terminal frame
    /// * `area` - Layout area for the footer
    /// * `theme` - Color theme
    pub fn render(&self, f: &mut Frame<'_>, area: Rect, theme: &Theme) {
        let footer = Paragraph::new(self.content.clone())
            .style(Style::default().fg(theme.secondary))
            .alignment(Alignment::Center)
            .block(
                Block::default()
                    .borders(Borders::TOP)
                    .border_style(Style::default().fg(theme.secondary)),
            );

        f.render_widget(footer, area);
    }
}

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

    #[test]
    fn test_app_header_creation() {
        let header = AppHeader::new("Test App");
        assert_eq!(header.title, "Test App");
    }

    #[test]
    fn test_app_footer_creation() {
        let footer = AppFooter::new("Press q to quit");
        assert_eq!(footer.content, "Press q to quit");
    }
}
