# bp/cart/overview_routes.py — Cart overview (list of page carts)
# GET / handled by defpage.

from __future__ import annotations

from quart import Blueprint, g, request

from .services import get_cart_grouped_by_page


def register(url_prefix: str) -> Blueprint:
    bp = Blueprint("cart_overview", __name__, url_prefix=url_prefix)

    @bp.before_request
    async def _prepare_page_data():
        """Load overview data for defpage route."""
        endpoint = request.endpoint or ""
        if not endpoint.endswith("defpage_cart_overview"):
            return
        from shared.sx.page import get_template_context
        from sx.sx_components import _overview_main_panel_sx
        page_groups = await get_cart_grouped_by_page(g.s)
        ctx = await get_template_context()
        g.overview_content = _overview_main_panel_sx(page_groups, ctx)

    return bp
