//! Sidebar: narrow icon rail on the left.
//!
//! Contains icon buttons for settings and project management.
//! Matches the existing Tauri app's GlobalTabBar (2.75rem icon rail).

use gpui::*;

use crate::state::AppState;
use crate::theme;

// ── Sidebar View ────────────────────────────────────────────────────

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

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

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

        let settings_bg = if settings_active {
            theme::blue_wash()
        } else {
            theme::MANTLE
        };
        let settings_fg = if settings_active {
            theme::BLUE
        } else {
            theme::SUBTEXT0
        };

        div()
            .id("sidebar")
            .w(px(48.0))
            .h_full()
            .flex()
            .flex_col()
            .items_center()
            .gap(px(8.0))
            .py(px(12.0))
            .bg(theme::MANTLE)
            .border_r_1()
            .border_color(theme::SURFACE0)
            // Settings button (gear icon)
            .child(
                div()
                    .id("sidebar-settings")
                    .w(px(40.0))
                    .h(px(40.0))
                    .flex()
                    .items_center()
                    .justify_center()
                    .rounded(px(8.0))
                    .bg(settings_bg)
                    .text_color(settings_fg)
                    .text_size(px(16.0))
                    .cursor_pointer()
                    .hover(|s| s.bg(theme::hover_bg()))
                    .child("\u{2699}".to_string())
                    .on_click({
                        let state = self.app_state.clone();
                        move |_event: &ClickEvent, _window: &mut Window, cx: &mut App| {
                            state.update(cx, |s, cx| {
                                s.toggle_settings();
                                cx.notify();
                            });
                        }
                    }),
            )
            // Spacer
            .child(div().flex_1())
            // Project count badge
            .child(
                div()
                    .w(px(32.0))
                    .h(px(32.0))
                    .flex()
                    .items_center()
                    .justify_center()
                    .rounded(px(6.0))
                    .bg(theme::SURFACE0)
                    .text_color(theme::SUBTEXT0)
                    .text_size(px(11.0))
                    .child({
                        let count = state.projects.len();
                        format!("{count}")
                    }),
            )
    }
}
