"""Account pages blueprint.

Moved from federation/bp/auth — newsletters, fragment pages (tickets, bookings).
Mounted at root /. GET page handlers replaced by defpage.
"""
from __future__ import annotations

from quart import (
    Blueprint,
    g,
)
from sqlalchemy import select

from shared.models import UserNewsletter
from shared.infrastructure.fragments import fetch_fragments
from shared.sx.helpers import sx_response, sx_call


def register(url_prefix="/"):
    account_bp = Blueprint("account", __name__, url_prefix=url_prefix)

    @account_bp.before_request
    async def _prepare_page_data():
        """Fetch account_nav fragments for layout."""
        events_nav, cart_nav, artdag_nav = await fetch_fragments([
            ("events", "account-nav-item", {}),
            ("cart", "account-nav-item", {}),
            ("artdag", "nav-item", {}),
        ], required=False)
        g.account_nav = events_nav + cart_nav + artdag_nav

    @account_bp.post("/newsletter/<int:newsletter_id>/toggle/")
    async def toggle_newsletter(newsletter_id: int):
        if not g.get("user"):
            return "", 401

        result = await g.s.execute(
            select(UserNewsletter).where(
                UserNewsletter.user_id == g.user.id,
                UserNewsletter.newsletter_id == newsletter_id,
            )
        )
        un = result.scalar_one_or_none()

        if un:
            un.subscribed = not un.subscribed
        else:
            un = UserNewsletter(
                user_id=g.user.id,
                newsletter_id=newsletter_id,
                subscribed=True,
            )
            g.s.add(un)

        await g.s.flush()

        # Render toggle directly — no sx_components intermediary
        from shared.browser.app.csrf import generate_csrf_token
        from shared.infrastructure.urls import account_url

        nid = un.newsletter_id
        url_fn = getattr(g, "_account_url", None) or account_url
        toggle_url = url_fn(f"/newsletter/{nid}/toggle/")
        csrf = generate_csrf_token()
        bg = "bg-emerald-500" if un.subscribed else "bg-stone-300"
        translate = "translate-x-6" if un.subscribed else "translate-x-1"
        checked = "true" if un.subscribed else "false"

        return sx_response(sx_call(
            "account-newsletter-toggle",
            id=f"nl-{nid}", url=toggle_url,
            hdrs={"X-CSRFToken": csrf},
            target=f"#nl-{nid}",
            cls=f"relative inline-flex h-6 w-11 items-center rounded-full transition-colors focus:outline-none focus:ring-2 focus:ring-emerald-500 focus:ring-offset-2 {bg}",
            checked=checked,
            knob_cls=f"inline-block h-4 w-4 rounded-full bg-white shadow transform transition-transform {translate}",
        ))

    return account_bp
