"""Slack incoming webhook client."""

from __future__ import annotations

import logging
from typing import Any

import aiohttp

from ..http_base import HttpProviderClient

_LOGGER = logging.getLogger(__name__)


class SlackClient(HttpProviderClient):
    """Sends messages via Slack incoming webhook URLs."""

    def __init__(self, session: aiohttp.ClientSession) -> None:
        super().__init__(session, provider_name="slack")

    async def send(
        self,
        webhook_url: str,
        message: str,
        username: str | None = None,
        icon_emoji: str | None = None,
    ) -> dict[str, Any]:
        """Send a message to a Slack incoming webhook."""
        if not webhook_url:
            return {"success": False, "error": "Missing webhook_url"}

        payload: dict[str, Any] = {"text": message}
        if username:
            payload["username"] = username
        if icon_emoji:
            payload["icon_emoji"] = icon_emoji

        return await self.request("POST", webhook_url, json=payload)
