"""Factory function for creating paginated media browser views"""

# AUTOGENERATED! DO NOT EDIT! File to edit: ../../nbs/media/pagination.ipynb.

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

# %% ../../nbs/media/pagination.ipynb 3
from typing import List, Any, Optional, Callable
from fasthtml.common import *

from cjm_fasthtml_interactions.patterns.pagination import Pagination
from cjm_fasthtml_daisyui.components.actions.button import btn, btn_colors
from cjm_fasthtml_daisyui.utilities.semantic_colors import text_dui
from cjm_fasthtml_tailwind.utilities.spacing import p
from cjm_fasthtml_tailwind.utilities.typography import text_align
from cjm_fasthtml_tailwind.core.base import combine_classes

from .models import MediaFile
from .components import grid_view_content, list_view_content, empty_media_content

from .scanner import MediaScanner
from .mounter import MediaMounter

# %% ../../nbs/media/pagination.ipynb 5
def create_media_pagination(
    pagination_id: str,  # Unique identifier for this pagination instance
    scanner: MediaScanner,  # MediaScanner instance for loading files
    mounter: MediaMounter,  # MediaMounter instance for URL generation
    items_per_page: int = 30,  # Number of items per page
    content_id: Optional[str] = None,  # HTML ID for content area
    preview_route_func = None,  # Function to generate preview route URL
    modal_id: str = "sf-media-preview"  # ID for the preview modal
) -> Pagination:  # Configured Pagination instance
    """Create a Pagination instance for media browsing."""

    def load_media_files(request) -> List[MediaFile]:
        """Load media files from scanner with optional filtering."""
        files = scanner.scan()

        # Filter by media type if specified
        media_type = request.query_params.get("media_type")
        if media_type and media_type != "all":
            files = [f for f in files if f.media_type == media_type]

        return files

    def render_media_items(items: List[MediaFile], page: int, request) -> Any:
        """Render media items for the current page."""
        view_mode = request.query_params.get("view", "grid")
        media_type = request.query_params.get("media_type")
        start_idx = (page - 1) * items_per_page

        if items:
            if view_mode == "grid":
                return grid_view_content(
                    media_files=items,
                    mounter=mounter,
                    start_idx=start_idx,
                    media_type=media_type,
                    preview_route_func=preview_route_func,
                    modal_id=modal_id
                )
            else:
                return list_view_content(
                    media_files=items,
                    mounter=mounter,
                    start_idx=start_idx,
                    media_type=media_type,
                    preview_route_func=preview_route_func,
                    modal_id=modal_id
                )
        else:
            return empty_media_content(
                message="No media files found. Configure directories in settings.",
            )

    return Pagination(
        pagination_id=pagination_id,
        data_loader=load_media_files,
        render_items=render_media_items,
        items_per_page=items_per_page,
        content_id=content_id,
        preserve_params=["view", "media_type"],
        show_endpoints=True,
        push_url=False  # Don't push URL for workflow-internal browsing
    )
