"""Orders app action endpoints.

create-order remains as a Python fallback (complex multi-step SumUp checkout).
"""
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("orders")

    async def _create_order():
        from shared.browser.app.payments.sumup import create_checkout as sumup_create_checkout
        from shared.infrastructure.urls import orders_url
        from services.checkout import (
            create_order,
            resolve_page_config_from_post_id,
            build_sumup_description,
            build_sumup_reference,
            build_webhook_url,
        )

        data = await request.get_json(force=True)

        cart_items = data.get("cart_items", [])
        calendar_entries = data.get("calendar_entries", [])
        tickets = data.get("tickets", [])
        user_id = data.get("user_id")
        session_id = data.get("session_id")
        product_total = data.get("product_total", 0)
        calendar_total = data.get("calendar_total", 0)
        ticket_total = data.get("ticket_total", 0)
        page_post_id = data.get("page_post_id")

        order = await create_order(
            g.s, cart_items, calendar_entries,
            user_id, session_id,
            product_total, calendar_total,
            ticket_total=ticket_total,
            page_post_id=page_post_id,
        )

        page_config = None
        if page_post_id:
            page_config = await resolve_page_config_from_post_id(page_post_id)

        if page_config:
            order.page_config_id = page_config.id

        order.sumup_reference = build_sumup_reference(order.id, page_config=page_config)
        description = build_sumup_description(cart_items, order.id, ticket_count=len(tickets))

        redirect_url = orders_url(f"/checkout/return/{order.id}/")
        webhook_base_url = orders_url(f"/checkout/webhook/{order.id}/")
        webhook_url = build_webhook_url(webhook_base_url)

        checkout_data = await sumup_create_checkout(
            order,
            redirect_url=redirect_url,
            webhook_url=webhook_url,
            description=description,
            page_config=page_config,
        )
        order.sumup_checkout_id = checkout_data.get("id")
        order.sumup_status = checkout_data.get("status")
        order.description = checkout_data.get("description")
        hosted_cfg = checkout_data.get("hosted_checkout") or {}
        hosted_url = hosted_cfg.get("hosted_checkout_url") or checkout_data.get("hosted_checkout_url")
        order.sumup_hosted_url = hosted_url
        await g.s.flush()

        return {
            "order_id": order.id,
            "sumup_hosted_url": hosted_url,
            "page_config_id": order.page_config_id,
            "sumup_reference": order.sumup_reference,
            "description": order.description,
        }

    _handlers["create-order"] = _create_order

    return bp
