"""Reusable app shell primitives for page-level layout."""

from __future__ import annotations

from nicegui import ui

NAV_ITEMS: tuple[tuple[str, str, str], ...] = (
    ("Upload", "/upload", "upload_file"),
    ("Jobs", "/jobs", "work_history"),
)


def _is_active_path(*, current_path: str, item_path: str) -> bool:
    if item_path == "/jobs":
        return current_path == "/jobs" or current_path.startswith("/jobs/")
    return current_path == item_path


def _button_props(*, icon: str, is_active: bool) -> str:
    if is_active:
        return f"icon={icon} no-caps unelevated color=primary text-color=white"
    return f"icon={icon} no-caps outline color=secondary text-color=secondary"


def _button_classes(*, is_active: bool) -> str:
    base = "w-full sm:w-auto min-h-[40px] px-3 rounded-lg text-body2 text-weight-medium"
    if is_active:
        return f"{base}"
    return f"{base}"


def _render_nav_button(*, label: str, path: str, icon: str, current_path: str) -> None:
    is_active = _is_active_path(current_path=current_path, item_path=path)
    button = ui.button(
        label,
        icon=icon,
        on_click=lambda _=None, route=path: ui.navigate.to(route),
    )
    button.props(_button_props(icon=icon, is_active=is_active)).classes(_button_classes(is_active=is_active))


def _normalize_path(current_path: str | None) -> str:
    normalized = (current_path or "").strip()
    if not normalized:
        return "/upload"
    return normalized.rstrip("/") or "/"


def render_navigation_header(*, current_path: str | None = None) -> None:
    """Render a shared app header with links for top-level pages."""
    normalized_path = _normalize_path(current_path)

    with (
        ui.header(elevated=True).props("bordered").classes("bg-dark text-white q-px-sm q-py-xs"),
        ui.row().classes("w-full items-center justify-end q-gutter-xs"),
        ui.element("div").classes("w-full grid grid-cols-2 gap-2 sm:flex sm:justify-start sm:gap-2"),
    ):
        for label, path, icon in NAV_ITEMS:
            _render_nav_button(label=label, path=path, icon=icon, current_path=normalized_path)
