use ratatui::prelude::*;
use ratatui::widgets::Paragraph;

use crate::app::App;
use super::theme;

/// Render the search input bar.
pub fn render(frame: &mut Frame, area: Rect, app: &App) {
    let result_count = app.search_results.len();

    let api_indicator = if app.searching_api {
        Span::styled("  ◌ searching...", Style::default().fg(theme::warm()))
    } else if result_count > 0 {
        Span::styled(format!("  {} found", result_count), theme::dim())
    } else {
        Span::styled("", theme::dim())
    };

    let spans = vec![
        Span::styled(" 🔍 ", theme::neon()),
        Span::styled(&app.search_query, theme::cyan()),
        Span::styled("█", Style::default().fg(theme::highlight())),
        api_indicator,
    ];

    let line = Line::from(spans);

    let search_bar = Paragraph::new(vec![line])
        .style(Style::default().bg(theme::surface_color()));

    frame.render_widget(search_bar, area);
}
