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 TestIsMessageFromCsirtWebForm:
    async def test_is_message_from_csirt_web_form_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 not await test_activity_env.run(activities.is_message_from_csirt_web_form, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_is_message_from_csirt_web_form_wrong_subject(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        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",
        )
        mock_ticket_data.attachments[0].subject = "Random subject"
        get_ticket_data_mock.return_value = mock_ticket_data
        assert not await test_activity_env.run(activities.is_message_from_csirt_web_form, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_is_message_from_csirt_web_form_correct_subject(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        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",
        )
        mock_ticket_data.attachments[0].subject = "Zprava z formulare webu csirt.muni.cz"
        get_ticket_data_mock.return_value = mock_ticket_data
        assert await test_activity_env.run(activities.is_message_from_csirt_web_form, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
