"""Cart app action endpoints.

adopt-cart-for-user is defined in ``cart/actions.sx``.
clear-cart-for-order remains as a Python fallback (complex object construction).
"""
from __future__ import annotations

from quart import Blueprint, g, request

from shared.infrastructure.query_blueprint import create_action_blueprint


def register() -> Blueprint:
    bp, _handlers = create_action_blueprint("cart")

    async def _clear_cart_for_order():
        from bp.cart.services.clear_cart_for_order import clear_cart_for_order

        data = await request.get_json()
        user_id = data.get("user_id")
        session_id = data.get("session_id")
        page_post_id = data.get("page_post_id")

        order = type("_Order", (), {
            "user_id": user_id,
            "session_id": session_id,
        })()

        await clear_cart_for_order(g.s, order, page_post_id=page_post_id)
        return {"ok": True}

    _handlers["clear-cart-for-order"] = _clear_cart_for_order

    return bp
