"""Platform gateway entrypoint for wp_invoices_mail_app."""

from __future__ import annotations


def handle_request(context, action):
    """Dispatch platform actions without changing the legacy worker flow."""
    try:
        tenant_id = ((context or {}).get("tenant") or {}).get("tenant_id")
        payload = (context or {}).get("payload", {}) or {}
        _ = payload  # explicit extraction for future platform actions

        if action == "test":
            return {
                "status": "ok",
                "app": "wp_invoices_mail_app",
                "message": "Mail app reachable",
                "tenant": tenant_id,
            }

        if action == "process_mailbox":
            from apps.wp_invoices_mail_app.cli import run_once

            run_once()
            return {
                "status": "ok",
                "app": "wp_invoices_mail_app",
                "message": "Mailbox processed via CLI",
                "tenant": tenant_id,
            }

        return {
            "status": "error",
            "app": "wp_invoices_mail_app",
            "message": f"Unsupported action: {action}",
        }, 404
    except Exception as e:
        return {
            "status": "error",
            "app": "wp_invoices_mail_app",
            "message": str(e),
        }, 500
