"""UI page registration exports."""

from fastapi import FastAPI
from nicegui import ui

from transcription.ui.pages.home_page import register_page as register_home_page
from transcription.ui.pages.documents_page import register_page as register_documents_page
from transcription.ui.pages.jobs_page import register_page as register_jobs_page
from transcription.ui.pages.people_page import register_page as register_people_page
from transcription.ui.pages.sources_page import register_page as register_sources_page
from transcription.ui.resources import read_css
from transcription.ui.theme import apply_archival_theme

_THEME_REGISTERED_STATE_KEY = "transcription_ui_theme_registered"


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

    apply_archival_theme()
    ui.add_css(read_css("theme.css"), 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_home_page()
    register_documents_page()
    register_people_page()
    register_sources_page()
    register_jobs_page()
    ui.run_with(app, mount_path="/ui", show_welcome_message=False, dark=False)
