"""Mobile push notification channel adapter for Grukk.

Sends messages to the King via push notifications and the chat API.
This is the primary channel for time-sensitive communications like
approval escalations and critical loop alerts.
"""

from __future__ import annotations

import logging
from typing import Any

from peon_mcp.grukk.api import GrukkAPIClient

log = logging.getLogger("peon-grukk")


class MobileChannel:
    """Sends messages via the Grukk messages API (and optionally push notifications).

    Parameters
    ----------
    client:
        :class:`GrukkAPIClient` instance for API access.
    """

    def __init__(self, client: GrukkAPIClient) -> None:
        self.client = client

    async def send(
        self,
        user_id: int,
        content: str,
        metadata: dict[str, Any] | None = None,
    ) -> bool:
        """Send a message to a user via the mobile channel.

        Posts the message through the Grukk messages API
        (``POST /api/grukk/messages``) with role ``grukk``.

        Parameters
        ----------
        user_id:
            Target user's ID in the peon system.
        content:
            Message content (plain text or markdown).
        metadata:
            Optional metadata (e.g., priority, approval_id, task_id) for
            rich notification rendering.

        Returns
        -------
        bool
            True if the message was sent successfully.
        """
        result = self.client.send_message(
            content=content,
            user_id=user_id,
            metadata=metadata,
        )
        if result is not None:
            log.debug("Message sent to user %d via mobile channel.", user_id)
            # TODO: If push tokens are registered, send a push notification
            # via Expo push service for real-time alerting.  For now we rely
            # on the client polling the messages API.
            return True

        log.warning("Failed to send message to user %d via mobile channel.", user_id)
        return False
