"""UI page registration exports."""

from pathlib import Path

from fastapi import FastAPI
from nicegui import app as nicegui_app
from nicegui import ui

from transcription.ui.pages.jobs_page import register_page as register_jobs_page
from transcription.ui.pages.upload_page import register_page as register_upload_page

_THEME_REGISTERED_STATE_KEY = "transcription_ui_theme_registered"

_THEME_COLORS: dict[str, str] = {
    "primary": "#6f97e8",
    "secondary": "#92b5f5",
    "accent": "#7fc0de",
    "dark": "#22304a",
    "dark_page": "#1a2538",
    "positive": "#86c8ad",
    "negative": "#d98a9a",
    "info": "#7ebdda",
    "warning": "#e2c083",
}


def _register_global_styles(app: FastAPI) -> None:
    if getattr(app.state, _THEME_REGISTERED_STATE_KEY, False):
        return

    nicegui_app.colors(**_THEME_COLORS)

    css_path = Path(__file__).resolve().parent / "static" / "colors.css"
    if css_path.exists():
        ui.add_css(css_path, shared=True)

    setattr(app.state, _THEME_REGISTERED_STATE_KEY, True)


def register_pages(app: FastAPI) -> None:
    """Register all NiceGUI pages and mount them onto the FastAPI app."""
    _register_global_styles(app)
    register_upload_page()
    register_jobs_page()
    ui.run_with(app, mount_path="/ui", show_welcome_message=False, dark=True)
