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.netbox.structs import NetboxSubnetInfoDTO
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 TestIsAutomaticTicketFromUni:
    async def test_is_automatic_ticket_from_uni_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 not await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_is_automatic_ticket_from_uni_no_senders_no_ips(
        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",
        )
        get_ticket_data_mock.return_value = mock_ticket_data
        assert not await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_is_automatic_ticket_from_uni_no_senders_empty_ip(
        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",
        )
        mock_ticket_data.headers.add_header("X-RT-Incident-IP", "")
        get_ticket_data_mock.return_value = mock_ticket_data
        assert not await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_is_automatic_ticket_from_uni_random_senders_empty_ip(
        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",
        )
        mock_ticket_data.headers.add_header("X-RT-Incident-IP", "")
        mock_ticket_data.headers.add_header("From", "idk123")
        get_ticket_data_mock.return_value = mock_ticket_data
        assert not await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_is_automatic_ticket_from_uni_tool_gateway_sender_empty_ip(
        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",
        )
        mock_ticket_data.headers.add_header("X-RT-Incident-IP", "")
        mock_ticket_data.headers.add_header("From", "ToolGateway")
        get_ticket_data_mock.return_value = mock_ticket_data
        assert not await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_is_automatic_ticket_from_uni_random_sender_and_netbox_returns_none(
        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",
        )
        get_subnet_info_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.NetboxClient.get_subnet_info_on_ip",
        )
        mock_ticket_data.headers.add_header("X-RT-Incident-IP", "1.1.1.1")
        mock_ticket_data.headers.add_header("From", "idk123")
        get_ticket_data_mock.return_value = mock_ticket_data
        get_subnet_info_mock.return_value = None
        assert not await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        get_subnet_info_mock.assert_called_once_with("1.1.1.1")

    async def test_is_automatic_ticket_from_uni_random_sender_and_netbox_returns_empty_lists(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        mock_subnet_info: NetboxSubnetInfoDTO,
        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_subnet_info_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.NetboxClient.get_subnet_info_on_ip",
        )
        mock_ticket_data.headers.add_header("X-RT-Incident-IP", "1.1.1.1")
        mock_ticket_data.headers.add_header("From", "idk123")
        get_ticket_data_mock.return_value = mock_ticket_data
        mock_subnet_info.prefixes = []
        mock_subnet_info.ip_addresses = []
        get_subnet_info_mock.return_value = mock_subnet_info
        assert not await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        get_subnet_info_mock.assert_called_once_with("1.1.1.1")

    async def test_is_automatic_ticket_from_uni_random_sender_and_uni_ip(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        mock_subnet_info: NetboxSubnetInfoDTO,
        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_subnet_info_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.NetboxClient.get_subnet_info_on_ip",
        )
        mock_ticket_data.headers.add_header("X-RT-Incident-IP", "1.1.1.1")
        mock_ticket_data.headers.add_header("From", "idk123")
        get_ticket_data_mock.return_value = mock_ticket_data
        get_subnet_info_mock.return_value = mock_subnet_info
        assert not await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        get_subnet_info_mock.assert_called_once_with("1.1.1.1")

    async def test_is_automatic_ticket_from_uni_random_sender_and_friendly_ip(
        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",
        )
        get_subnet_info_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.NetboxClient.get_subnet_info_on_ip",
        )
        mock_ticket_data.headers.add_header("X-RT-Incident-IP", "78.128.210.1")
        mock_ticket_data.headers.add_header("From", "idk123")
        get_ticket_data_mock.return_value = mock_ticket_data
        get_subnet_info_mock.return_value = None
        assert not await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        get_subnet_info_mock.assert_called_once_with("78.128.210.1")

    async def test_is_automatic_ticket_from_uni_tool_gateway_sender_and_uni_ip(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        mock_subnet_info: NetboxSubnetInfoDTO,
        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_subnet_info_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.NetboxClient.get_subnet_info_on_ip",
        )
        mock_ticket_data.headers.add_header("X-RT-Incident-IP", "1.1.1.1")
        mock_ticket_data.headers.add_header("From", "ToolGateway")
        get_ticket_data_mock.return_value = mock_ticket_data
        get_subnet_info_mock.return_value = mock_subnet_info
        assert await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        get_subnet_info_mock.assert_called_once_with("1.1.1.1")

    async def test_is_automatic_ticket_from_uni_tool_gateway_sender_and_friendly_ip(
        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",
        )
        get_subnet_info_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.mattermost_notification_on_automatic_ticket_from_uni.NetboxClient.get_subnet_info_on_ip",
        )
        mock_ticket_data.headers.add_header("X-RT-Incident-IP", "78.128.210.1")
        mock_ticket_data.headers.add_header("From", "ToolGateway")
        get_ticket_data_mock.return_value = mock_ticket_data
        get_subnet_info_mock.return_value = None
        assert await test_activity_env.run(activities.is_automatic_ticket_from_uni, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        get_subnet_info_mock.assert_called_once_with("78.128.210.1")
