"""Convenience router factory that wires up standard card stack routes (Tier 2 API)."""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/routes/router.ipynb.

# %% auto #0
__all__ = ['init_card_stack_router']

# %% ../../nbs/routes/router.ipynb #r1000003
from typing import Any, Callable, List, Optional, Tuple

from fasthtml.common import APIRouter

from ..core.config import CardStackConfig
from ..core.html_ids import CardStackHtmlIds
from ..core.models import CardStackState, CardStackUrls
from cjm_fasthtml_card_stack.routes.handlers import (
    card_stack_navigate,
    card_stack_navigate_to_index,
    card_stack_update_viewport,
    card_stack_save_width,
    card_stack_save_scale,
)

# %% ../../nbs/routes/router.ipynb #r1000005
def init_card_stack_router(
    config: CardStackConfig,  # Card stack configuration
    state_getter: Callable[[], CardStackState],  # Function to get current state
    state_setter: Callable[[CardStackState], None],  # Function to save state
    get_items: Callable[[], List[Any]],  # Function to get current items list
    render_card: Callable,  # Card renderer callback: (item, CardRenderContext) -> FT
    route_prefix: str = "/card-stack",  # Route prefix for all card stack routes
    progress_label: str = "Item",  # Label for progress indicator
) -> Tuple[APIRouter, CardStackUrls]:  # (router, urls) tuple
    """Initialize an APIRouter with all standard card stack routes."""
    router = APIRouter(prefix=route_prefix)
    ids = CardStackHtmlIds(prefix=config.prefix)

    # -----------------------------------------------------------------
    # Navigation Routes
    # -----------------------------------------------------------------

    def _nav(direction: str) -> Any:
        """Shared navigation handler."""
        state = state_getter()
        items = get_items()
        result = card_stack_navigate(
            direction=direction, card_items=items, state=state,
            config=config, ids=ids, urls=urls,
            render_card=render_card, progress_label=progress_label,
        )
        state_setter(state)
        return result

    @router
    def nav_up() -> Any:
        """Navigate to previous item."""
        return _nav("up")

    @router
    def nav_down() -> Any:
        """Navigate to next item."""
        return _nav("down")

    @router
    def nav_first() -> Any:
        """Navigate to first item."""
        return _nav("first")

    @router
    def nav_last() -> Any:
        """Navigate to last item."""
        return _nav("last")

    @router
    def nav_page_up() -> Any:
        """Navigate up by page."""
        return _nav("page_up")

    @router
    def nav_page_down() -> Any:
        """Navigate down by page."""
        return _nav("page_down")

    @router
    def nav_to_index(target_index: int) -> Any:
        """Navigate to a specific item index (click-to-focus)."""
        state = state_getter()
        items = get_items()
        result = card_stack_navigate_to_index(
            target_index=target_index, card_items=items, state=state,
            config=config, ids=ids, urls=urls,
            render_card=render_card, progress_label=progress_label,
        )
        state_setter(state)
        return result

    # -----------------------------------------------------------------
    # Viewport Route
    # -----------------------------------------------------------------

    @router
    def update_viewport(visible_count: int) -> Any:
        """Update viewport with new card count (OOB section swaps)."""
        state = state_getter()
        items = get_items()
        result = card_stack_update_viewport(
            visible_count=visible_count, card_items=items, state=state,
            config=config, ids=ids, urls=urls, render_card=render_card,
        )
        state_setter(state)
        return result

    # -----------------------------------------------------------------
    # Preference Persistence Routes
    # -----------------------------------------------------------------

    @router
    def save_width(card_width: int) -> Any:
        """Save card stack width to server state."""
        state = state_getter()
        card_stack_save_width(state, card_width, config)
        state_setter(state)
        return ""

    @router
    def save_scale(card_scale: int) -> Any:
        """Save card stack scale to server state."""
        state = state_getter()
        card_stack_save_scale(state, card_scale, config)
        state_setter(state)
        return ""

    # -----------------------------------------------------------------
    # Build URL bundle from registered routes
    # -----------------------------------------------------------------

    urls = CardStackUrls(
        nav_up=nav_up.to(),
        nav_down=nav_down.to(),
        nav_first=nav_first.to(),
        nav_last=nav_last.to(),
        nav_page_up=nav_page_up.to(),
        nav_page_down=nav_page_down.to(),
        nav_to_index=nav_to_index.to(),
        update_viewport=update_viewport.to(),
        save_width=save_width.to(),
        save_scale=save_scale.to(),
    )

    return router, urls
