"""Upload page registration and handlers."""

from __future__ import annotations

from fastapi import Request
from nicegui import ui

from transcription.app_state import resolve_session_factory
from transcription.db import get_session
from transcription.services.store import create_upload_job
from transcription.ui.components.app_shell import render_navigation_header
from transcription.ui.components.upload import render_upload_widget
from transcription.worker import resolve_worker_notifier


def register_page() -> None:
    """Register the upload page route."""

    @ui.page("/upload", title="Upload Document")
    def upload_page(request: Request) -> None:
        render_navigation_header(current_path="/upload")
        session_factory = resolve_session_factory(request.app.state)

        async def submit_upload(filename: str, file_bytes: bytes):
            async with get_session(session_factory=session_factory) as session:
                return await create_upload_job(
                    filename=filename,
                    file_bytes=file_bytes,
                    session=session,
                )

        notify_worker = resolve_worker_notifier(request.app.state)
        render_upload_widget(submitter=submit_upload, notifier=notify_worker)
