"""Account app action endpoints.

Exposes write operations at ``/internal/actions/<action_name>`` for
cross-app callers (blog webhooks) via the internal action client.
"""
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

    # --- ghost-sync-member ---
    async def _ghost_sync_member():
        """Sync a single Ghost member into db_account."""
        data = await request.get_json()
        ghost_id = data.get("ghost_id")
        if not ghost_id:
            return {"error": "ghost_id required"}, 400

        from services.ghost_membership import sync_single_member
        await sync_single_member(g.s, ghost_id)
        return {"ok": True}

    _handlers["ghost-sync-member"] = _ghost_sync_member

    # --- ghost-push-member ---
    async def _ghost_push_member():
        """Push a local user's membership data to Ghost."""
        data = await request.get_json()
        user_id = data.get("user_id")
        if not user_id:
            return {"error": "user_id required"}, 400

        from services.ghost_membership import sync_member_to_ghost
        result_id = await sync_member_to_ghost(g.s, int(user_id))
        return {"ok": True, "ghost_id": result_id}

    _handlers["ghost-push-member"] = _ghost_push_member

    return bp
