import pytest_mock
from temporalio.testing import ActivityEnvironment

from csirtmu.beta_temporal.config import Config as WorkerConfig
from csirtmu.beta_temporal.external_services.mattermost.client import MattermostClient
from csirtmu.beta_temporal.external_services.rt import structs
from csirtmu.beta_temporal.external_services.rt.client import RTClient
from csirtmu.beta_temporal.workflows.mattermost_notification_on_csirt_web_form import (
    MattermostNotificationOnCSIRTWebFormActivities,
)


class TestSendMattermostNotification:
    async def test_sent_mattermost_notification_ticket_obj_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        activities = MattermostNotificationOnCSIRTWebFormActivities(
            RTClient(worker_config.rt), worker_config.rt.base_url, MattermostClient(worker_config.mattermost)
        )
        mock_transaction_obj.ticket_obj = None
        assert (
            await test_activity_env.run(activities.send_mattermost_notification, mock_transaction_obj, "idk")
        ) is None

    async def test_sent_mattermost_notification_ticket_data_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockFixture,
    ) -> None:
        activities = MattermostNotificationOnCSIRTWebFormActivities(
            RTClient(worker_config.rt), worker_config.rt.base_url, MattermostClient(worker_config.mattermost)
        )
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_csirt_web_form.RTClient.get_ticket_data",
        )
        get_ticket_data_mock.return_value = None
        assert (
            await test_activity_env.run(activities.send_mattermost_notification, mock_transaction_obj, "idk")
        ) is None
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_sent_mattermost_notification_success_message_without_skipped_lines(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockFixture,
    ) -> None:
        assert mock_transaction_obj.ticket_obj is not None
        activities = MattermostNotificationOnCSIRTWebFormActivities(
            RTClient(worker_config.rt), worker_config.rt.base_url, MattermostClient(worker_config.mattermost)
        )
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_csirt_web_form.RTClient.get_ticket_data",
        )
        send_message_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_csirt_web_form.MattermostClient.send_message",
        )
        send_message_mock.return_value = None
        mock_ticket_data.body = "Message from csirt web form"
        get_ticket_data_mock.return_value = mock_ticket_data
        await test_activity_env.run(activities.send_mattermost_notification, mock_transaction_obj, "idk")
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        send_message_mock.assert_called_once_with(
            f"New ticket in Info: {mock_transaction_obj.ticket_obj.subject}, link: "
            f"{worker_config.rt.base_url}Ticket/Display.html?id={mock_transaction_obj.ticket_obj.id}\n"
            "> Message from csirt web form\n",
            channel="idk",
            username="rt_bot",
            icon_emoji="sauron",
        )

    async def test_sent_mattermost_notification_success_message_with_skipped_lines(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockFixture,
    ) -> None:
        assert mock_transaction_obj.ticket_obj is not None
        activities = MattermostNotificationOnCSIRTWebFormActivities(
            RTClient(worker_config.rt), worker_config.rt.base_url, MattermostClient(worker_config.mattermost)
        )
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_csirt_web_form.RTClient.get_ticket_data",
        )
        send_message_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_csirt_web_form.MattermostClient.send_message",
        )
        send_message_mock.return_value = None
        mock_ticket_data.body = "Message from csirt web form\nNa zprávu mužete přímo odpovědět"
        get_ticket_data_mock.return_value = mock_ticket_data
        await test_activity_env.run(activities.send_mattermost_notification, mock_transaction_obj, "idk")
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        send_message_mock.assert_called_once_with(
            f"New ticket in Info: {mock_transaction_obj.ticket_obj.subject}, link: "
            f"{worker_config.rt.base_url}Ticket/Display.html?id={mock_transaction_obj.ticket_obj.id}\n"
            "> Message from csirt web form\n",
            channel="idk",
            username="rt_bot",
            icon_emoji="sauron",
        )
