"""Cart defpage setup — registers layouts, page helpers, and loads .sx pages."""
from __future__ import annotations

from typing import Any


def setup_cart_pages() -> None:
    """Register cart-specific layouts, page helpers, and load page definitions."""
    _register_cart_layouts()
    _register_cart_helpers()
    _load_cart_page_files()


def _load_cart_page_files() -> None:
    import os
    from shared.sx.pages import load_page_dir
    load_page_dir(os.path.dirname(__file__), "cart")


# ---------------------------------------------------------------------------
# Layouts
# ---------------------------------------------------------------------------

def _register_cart_layouts() -> None:
    from shared.sx.layouts import register_custom_layout
    register_custom_layout("cart-page", _cart_page_full, _cart_page_oob)
    register_custom_layout("cart-admin", _cart_admin_full, _cart_admin_oob)


def _cart_page_full(ctx: dict, **kw: Any) -> str:
    from shared.sx.helpers import root_header_sx, sx_call, SxExpr
    from sx.sx_components import _cart_header_sx, _page_cart_header_sx

    page_post = ctx.get("page_post")
    root_hdr = root_header_sx(ctx)
    child = _cart_header_sx(ctx)
    page_hdr = _page_cart_header_sx(ctx, page_post)
    nested = sx_call(
        "header-child-sx",
        inner=SxExpr("(<> " + child + " " + sx_call("header-child-sx", id="cart-header-child", inner=SxExpr(page_hdr)) + ")"),
    )
    return "(<> " + root_hdr + " " + nested + ")"


def _cart_page_oob(ctx: dict, **kw: Any) -> str:
    from shared.sx.helpers import root_header_sx, sx_call, SxExpr
    from sx.sx_components import _cart_header_sx, _page_cart_header_sx

    page_post = ctx.get("page_post")
    child_oob = sx_call("oob-header-sx",
                           parent_id="cart-header-child",
                           row=SxExpr(_page_cart_header_sx(ctx, page_post)))
    cart_hdr_oob = _cart_header_sx(ctx, oob=True)
    root_hdr_oob = root_header_sx(ctx, oob=True)
    return "(<> " + child_oob + " " + cart_hdr_oob + " " + root_hdr_oob + ")"


async def _cart_admin_full(ctx: dict, **kw: Any) -> str:
    from shared.sx.helpers import root_header_sx
    from sx.sx_components import _post_header_sx, _cart_page_admin_header_sx

    page_post = ctx.get("page_post")
    selected = kw.get("selected", "")
    root_hdr = root_header_sx(ctx)
    post_hdr = await _post_header_sx(ctx, page_post)
    admin_hdr = _cart_page_admin_header_sx(ctx, page_post, selected=selected)
    return "(<> " + root_hdr + " " + post_hdr + " " + admin_hdr + ")"


async def _cart_admin_oob(ctx: dict, **kw: Any) -> str:
    from sx.sx_components import _cart_page_admin_header_sx

    page_post = ctx.get("page_post")
    selected = kw.get("selected", "")
    return _cart_page_admin_header_sx(ctx, page_post, oob=True, selected=selected)


# ---------------------------------------------------------------------------
# Page helpers
# ---------------------------------------------------------------------------

def _register_cart_helpers() -> None:
    from shared.sx.pages import register_page_helpers

    register_page_helpers("cart", {
        "overview-content": _h_overview_content,
        "page-cart-content": _h_page_cart_content,
        "cart-admin-content": _h_cart_admin_content,
        "cart-payments-content": _h_cart_payments_content,
    })


def _h_overview_content():
    from quart import g
    page_groups = getattr(g, "overview_page_groups", [])
    from sx.sx_components import _overview_main_panel_sx
    # _overview_main_panel_sx needs ctx for url helpers — use g-based approach
    # The function reads cart_url from ctx, which we can get from template context
    from shared.sx.page import get_template_context
    import asyncio
    # Page helpers are sync — we pre-compute in before_request
    return getattr(g, "overview_content", "")


def _h_page_cart_content():
    from quart import g
    return getattr(g, "page_cart_content", "")


def _h_cart_admin_content():
    from sx.sx_components import _cart_admin_main_panel_sx
    from shared.sx.page import get_template_context
    # Sync helper — _cart_admin_main_panel_sx is sync, but needs ctx
    # We can pre-compute in before_request, or use get_template_context_sync-like pattern
    from quart import g
    return getattr(g, "cart_admin_content", "")


def _h_cart_payments_content():
    from quart import g
    return getattr(g, "cart_payments_content", "")
