//! StatusBar: bottom bar showing agent states, cost, token count.
//!
//! Equivalent to the Tauri app's Mission Control bar.

use gpui::*;

use crate::state::{AgentStatus, AppState};
use crate::theme;

// ── Status Pill ─────────────────────────────────────────────────────

/// Small colored pill with a count label.
fn status_pill(label: &str, count: usize, color: Rgba) -> Div {
    div()
        .flex()
        .flex_row()
        .items_center()
        .gap(px(4.0))
        .px(px(8.0))
        .py(px(2.0))
        .rounded(px(4.0))
        .bg(theme::with_alpha(color, 0.12))
        .child(
            div()
                .w(px(6.0))
                .h(px(6.0))
                .rounded(px(3.0))
                .bg(color),
        )
        .child(
            div()
                .text_size(px(11.0))
                .text_color(color)
                .child(format!("{count} {label}")),
        )
}

// ── StatusBar View ──────────────────────────────────────────────────

pub struct StatusBar {
    app_state: Entity<AppState>,
}

impl StatusBar {
    pub fn new(app_state: Entity<AppState>) -> Self {
        Self { app_state }
    }
}

impl Render for StatusBar {
    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
        let state = self.app_state.read(cx);

        let running = state
            .projects
            .iter()
            .filter(|p| p.agent.status == AgentStatus::Running)
            .count();
        let idle = state
            .projects
            .iter()
            .filter(|p| p.agent.status == AgentStatus::Idle)
            .count();
        let done = state
            .projects
            .iter()
            .filter(|p| p.agent.status == AgentStatus::Done)
            .count();
        let errors = state
            .projects
            .iter()
            .filter(|p| p.agent.status == AgentStatus::Error)
            .count();
        let total_cost = state.total_cost();
        let total_tokens = state.total_tokens();
        let project_count = state.projects.len();

        let mut bar = div()
            .id("status-bar")
            .w_full()
            .h(px(28.0))
            .flex()
            .flex_row()
            .items_center()
            .px(px(12.0))
            .gap(px(12.0))
            .bg(theme::CRUST)
            .border_t_1()
            .border_color(theme::SURFACE0)
            // Agent status pills
            .child(status_pill("running", running, theme::GREEN))
            .child(status_pill("idle", idle, theme::OVERLAY0));

        if done > 0 {
            bar = bar.child(status_pill("done", done, theme::BLUE));
        }
        if errors > 0 {
            bar = bar.child(status_pill("error", errors, theme::RED));
        }

        bar
            // Spacer
            .child(div().flex_1())
            // Cost
            .child(
                div()
                    .flex()
                    .flex_row()
                    .items_center()
                    .gap(px(4.0))
                    .text_size(px(11.0))
                    .text_color(theme::SUBTEXT0)
                    .child(format!("${:.4}", total_cost)),
            )
            // Separator
            .child(
                div()
                    .w(px(1.0))
                    .h(px(14.0))
                    .bg(theme::SURFACE0),
            )
            // Tokens
            .child(
                div()
                    .text_size(px(11.0))
                    .text_color(theme::SUBTEXT0)
                    .child(format!("{}tok", format_number(total_tokens))),
            )
            // Separator
            .child(
                div()
                    .w(px(1.0))
                    .h(px(14.0))
                    .bg(theme::SURFACE0),
            )
            // Project count
            .child(
                div()
                    .text_size(px(11.0))
                    .text_color(theme::SUBTEXT0)
                    .child(format!("{project_count} projects")),
            )
    }
}

/// Format a number with K/M suffixes.
fn format_number(n: u64) -> String {
    if n >= 1_000_000 {
        format!("{:.1}M", n as f64 / 1_000_000.0)
    } else if n >= 1_000 {
        format!("{:.1}K", n as f64 / 1_000.0)
    } else {
        format!("{n}")
    }
}
