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

from typing import Any


def setup_account_pages() -> None:
    """Register account-specific layouts, page helpers, and load page definitions."""
    _register_account_layouts()
    _register_account_helpers()
    _load_account_page_files()


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


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

def _register_account_layouts() -> None:
    from shared.sx.layouts import register_custom_layout
    register_custom_layout("account", _account_full, _account_oob, _account_mobile)


def _account_full(ctx: dict, **kw: Any) -> str:
    from shared.sx.helpers import root_header_sx, header_child_sx
    from sx.sx_components import _auth_header_sx

    root_hdr = root_header_sx(ctx)
    hdr_child = header_child_sx(_auth_header_sx(ctx))
    return "(<> " + root_hdr + " " + hdr_child + ")"


def _account_oob(ctx: dict, **kw: Any) -> str:
    from shared.sx.helpers import root_header_sx
    from sx.sx_components import _auth_header_sx

    return "(<> " + _auth_header_sx(ctx, oob=True) + " " + root_header_sx(ctx, oob=True) + ")"


def _account_mobile(ctx: dict, **kw: Any) -> str:
    from shared.sx.helpers import mobile_menu_sx, mobile_root_nav_sx, sx_call, SxExpr
    from sx.sx_components import _auth_nav_mobile_sx
    ctx = _inject_account_nav(ctx)
    auth_section = sx_call("mobile-menu-section",
        label="account", href="/", level=1, colour="sky",
        items=SxExpr(_auth_nav_mobile_sx(ctx)))
    return mobile_menu_sx(auth_section, mobile_root_nav_sx(ctx))


def _inject_account_nav(ctx: dict) -> dict:
    """Ensure account_nav is in ctx from g.account_nav."""
    if "account_nav" not in ctx:
        from quart import g
        ctx = dict(ctx)
        ctx["account_nav"] = getattr(g, "account_nav", "")
    return ctx


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

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

    register_page_helpers("account", {
        "account-content": _h_account_content,
        "newsletters-content": _h_newsletters_content,
        "fragment-content": _h_fragment_content,
    })


def _h_account_content():
    from sx.sx_components import _account_main_panel_sx
    return _account_main_panel_sx({})


def _h_newsletters_content():
    from quart import g
    d = getattr(g, "newsletters_data", None)
    if not d:
        from shared.sx.helpers import sx_call
        return sx_call("account-newsletter-empty")
    from shared.sx.page import get_template_context_sync
    from sx.sx_components import _newsletters_panel_sx
    # Build a minimal ctx with account_url
    ctx = {"account_url": getattr(g, "_account_url", None)}
    if ctx["account_url"] is None:
        from shared.infrastructure.urls import account_url
        ctx["account_url"] = account_url
    return _newsletters_panel_sx(ctx, d)


def _h_fragment_content():
    from quart import g
    frag = getattr(g, "fragment_page_data", None)
    if not frag:
        return ""
    from sx.sx_components import _fragment_content
    return _fragment_content(frag)
