"""Generic webhook notification client."""

from __future__ import annotations

import logging
from typing import Any

import aiohttp

from ..http_base import HttpProviderClient

_LOGGER = logging.getLogger(__name__)


class WebhookClient(HttpProviderClient):
    """Send JSON payloads to a webhook URL.

    The URL is SSRF-validated on every send (defense-in-depth: re-validating
    catches DNS rebinding between calls and a misconfigured target). Headers
    pass through :func:`safe_headers` so a target config can't inject
    framing/hop-by-hop headers like ``Host`` or ``Transfer-Encoding``.
    """

    def __init__(
        self,
        session: aiohttp.ClientSession,
        url: str,
        headers: dict[str, str] | None = None,
    ) -> None:
        super().__init__(session, provider_name="webhook")
        self._url = url
        self._extra_headers = headers or {}

    async def send(self, payload: dict[str, Any]) -> dict[str, Any]:
        return await self.request("POST", self._url, json=payload, headers=self._extra_headers)
