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.netbox.client import NetboxClient
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_automatic_ticket_from_uni import (
    MattermostNotificationOnAutomaticTicketFromUniActivities,
)


class TestNotifyMattermostOnAutomaticTicket:
    async def test_notify_mattermost_on_automatic_ticket_ticket_obj_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        activities = MattermostNotificationOnAutomaticTicketFromUniActivities(
            RTClient(worker_config.rt),
            NetboxClient(worker_config.netbox),
            MattermostClient(worker_config.mattermost),
            worker_config.rt.base_url,
        )
        mock_transaction_obj.ticket_obj = None
        assert (
            await test_activity_env.run(
                activities.notify_mattermost_on_automatic_ticket,
                mock_transaction_obj,
                "idk",
            )
        ) is None

    async def test_notify_mattermost_on_automatic_ticket_ticket_data_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockFixture,
    ) -> None:
        activities = MattermostNotificationOnAutomaticTicketFromUniActivities(
            RTClient(worker_config.rt),
            NetboxClient(worker_config.netbox),
            MattermostClient(worker_config.mattermost),
            worker_config.rt.base_url,
        )
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.RTClient.get_ticket_data",
        )
        get_ticket_data_mock.return_value = None
        assert (
            await test_activity_env.run(
                activities.notify_mattermost_on_automatic_ticket,
                mock_transaction_obj,
                "idk",
            )
        ) is None

    async def test_notify_mattermost_on_automatic_ticket_attachment_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 = MattermostNotificationOnAutomaticTicketFromUniActivities(
            RTClient(worker_config.rt),
            NetboxClient(worker_config.netbox),
            MattermostClient(worker_config.mattermost),
            worker_config.rt.base_url,
        )
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.RTClient.get_ticket_data",
        )
        send_message_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.MattermostClient.send_message",
        )
        send_message_mock.return_value = None
        mock_ticket_data.attachments[0].subject = "attachment_subject"
        get_ticket_data_mock.return_value = mock_ticket_data
        await test_activity_env.run(
            activities.notify_mattermost_on_automatic_ticket,
            mock_transaction_obj,
            "idk",
        )
        assert mock_transaction_obj.ticket_obj is not None
        send_message_mock.assert_called_once_with(
            f"New ticket in Automatic: attachment_subject, link: {worker_config.rt.base_url}Ticket/Display.html?id={mock_transaction_obj.ticket_obj.id}",
            channel="idk",
            username="rt_bot",
            icon_emoji="sauron",
        )

    async def test_notify_mattermost_on_automatic_ticket_ticket_subject(
        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 = MattermostNotificationOnAutomaticTicketFromUniActivities(
            RTClient(worker_config.rt),
            NetboxClient(worker_config.netbox),
            MattermostClient(worker_config.mattermost),
            worker_config.rt.base_url,
        )
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.RTClient.get_ticket_data",
        )
        send_message_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.MattermostClient.send_message",
        )
        send_message_mock.return_value = None
        mock_ticket_data.attachments = []
        get_ticket_data_mock.return_value = mock_ticket_data
        mock_transaction_obj.ticket_obj.subject = "ticket_subject"
        await test_activity_env.run(
            activities.notify_mattermost_on_automatic_ticket,
            mock_transaction_obj,
            "idk",
        )
        send_message_mock.assert_called_once_with(
            f"New ticket in Automatic: ticket_subject, link: {worker_config.rt.base_url}Ticket/Display.html?id={mock_transaction_obj.ticket_obj.id}",
            channel="idk",
            username="rt_bot",
            icon_emoji="sauron",
        )
