from mattermostdriver.client import Client
from mattermostdriver.endpoints.webhooks import Webhooks

from csirtmu.beta_temporal.config import MattermostConfig


class MattermostClient:
    DEFAULT_CHANNEL_ID: str = "mmrttestchannel"
    DEFAULT_USERNAME: str = "TransactionBot"
    DEFAULT_ICON_EMOJI: str = ":ghost:"

    def __init__(self, config: MattermostConfig) -> None:
        self.config = config
        self.scheme, self.url = self.config.host.split("://")
        client = Client(
            {
                "scheme": self.scheme,
                "url": self.url,
                "port": 443,
                "basepath": "/",
                "verify": True,
                "auth": None,
                "debug": False,
                "request_timeout": None,
            }
        )
        self.webhook = Webhooks(client=client)

    def send_message(
        self, message: str, channel: str | None = None, username: str | None = None, icon_emoji: str | None = None
    ) -> None:
        self.webhook.call_webhook(
            self.config.webhook,
            options={
                "channel": channel or self.DEFAULT_CHANNEL_ID,
                "text": message,
                "username": username or self.DEFAULT_USERNAME,
                "icon_emoji": icon_emoji or self.DEFAULT_ICON_EMOJI,
            },
        )
