"""Orders app action endpoints."""
from __future__ import annotations

from quart import Blueprint, g, jsonify, request

from shared.infrastructure.actions import ACTION_HEADER


def register() -> Blueprint:
    bp = Blueprint("actions", __name__, url_prefix="/internal/actions")

    @bp.before_request
    async def _require_action_header():
        if not request.headers.get(ACTION_HEADER):
            return jsonify({"error": "forbidden"}), 403
        from shared.infrastructure.internal_auth import validate_internal_request
        if not validate_internal_request():
            return jsonify({"error": "forbidden"}), 403

    _handlers: dict[str, object] = {}

    @bp.post("/<action_name>")
    async def handle_action(action_name: str):
        handler = _handlers.get(action_name)
        if handler is None:
            return jsonify({"error": "unknown action"}), 404
        try:
            result = await handler()
            return jsonify(result)
        except Exception as exc:
            import logging
            logging.getLogger(__name__).exception("Action %s failed", action_name)
            return jsonify({"error": str(exc)}), 500

    # --- create-order ---
    async def _create_order():
        """Create an order from cart data. Called by cart during checkout."""
        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))

        # Build URLs using orders service's own domain
        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
