use ratatui::layout::Rect;
use ratatui::text::{Line, Span};
use ratatui::widgets::Paragraph;
use ratatui::Frame;

use crate::tui::app::AppState;
use crate::tui::theme;

/// Renders the command bar at the bottom of the screen (replaces status bar when active).
/// Shows mode label, `:` prefix with current input, and cursor or running indicator.
pub fn render(f: &mut Frame, area: Rect, state: &AppState) {
    let line = if let Some(ref cmd) = state.command_running {
        // Running state: show spinner
        let spinner = [
            '\u{280b}', '\u{2819}', '\u{2838}', '\u{2834}', '\u{2826}', '\u{2807}',
        ];
        let tick = (std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis()
            / 250) as usize;
        let ch = spinner[tick % spinner.len()];
        Line::from(vec![
            Span::styled(format!(" {ch} "), theme::highlight()),
            Span::styled(" :", theme::dim()),
            Span::styled(cmd.as_str(), theme::dim()),
        ])
    } else {
        // Input state
        Line::from(vec![
            Span::styled("[CMD]", theme::dim()),
            Span::styled(" :", theme::highlight()),
            Span::styled(&state.command_input, theme::normal()),
            Span::styled("\u{2588}", theme::highlight()),
        ])
    };

    let paragraph =
        Paragraph::new(line).style(ratatui::style::Style::default().bg(theme::SURFACE_ALT));
    f.render_widget(paragraph, area);
}
