import pytest_mock
from temporalio.testing import ActivityEnvironment

from csirtmu.beta_temporal.config import Config as WorkerConfig
from csirtmu.beta_temporal.external_services.rt import structs
from csirtmu.beta_temporal.external_services.rt.client import RTClient
from csirtmu.beta_temporal.workflows.handle_generic_autoreplies import HandleGenericAutorepliesActivities


class TestIsAutoresponse:
    async def test_is_autoresponse_no_ticket_data(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.handle_generic_autoreplies.RTClient.get_ticket_data",
        )
        get_ticket_data_mock.return_value = None

        activities = HandleGenericAutorepliesActivities(RTClient(worker_config.rt))
        assert not await test_activity_env.run(activities.is_autoresponse, mock_transaction_obj)

    async def test_is_autoresponse_no_keyword_in_header(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mock_ticket_data: structs.TicketData,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.handle_generic_autoreplies.RTClient.get_ticket_data",
        )
        get_ticket_data_mock.return_value = mock_ticket_data

        activities = HandleGenericAutorepliesActivities(RTClient(worker_config.rt))
        assert not await test_activity_env.run(activities.is_autoresponse, mock_transaction_obj)

    async def test_is_autoresponse_from_header_but_no_subject_header(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mock_ticket_data: structs.TicketData,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.handle_generic_autoreplies.RTClient.get_ticket_data",
        )
        mock_ticket_data.headers.add_header("From", "ec2-abuse")
        get_ticket_data_mock.return_value = mock_ticket_data

        activities = HandleGenericAutorepliesActivities(RTClient(worker_config.rt))
        assert not await test_activity_env.run(activities.is_autoresponse, mock_transaction_obj)

    async def test_is_autoresponse_from_header_keyword_in_body(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mock_ticket_data: structs.TicketData,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.handle_generic_autoreplies.RTClient.get_ticket_data",
        )
        mock_ticket_data.headers.add_header("From", "ec2-abuse")
        mock_ticket_data.body = "This case has been investigated and resolved by the Amazon EC2 Abuse Team"
        get_ticket_data_mock.return_value = mock_ticket_data

        activities = HandleGenericAutorepliesActivities(RTClient(worker_config.rt))
        assert await test_activity_env.run(activities.is_autoresponse, mock_transaction_obj)

    async def test_is_autoresponse_from_header_keyword_in_subject(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mock_ticket_data: structs.TicketData,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.handle_generic_autoreplies.RTClient.get_ticket_data",
        )
        mock_ticket_data.headers.add_header("From", "ec2-abuse")
        mock_ticket_data.headers.add_header("Subject", "Amazon Web Services: You have submitted a new report")
        get_ticket_data_mock.return_value = mock_ticket_data

        activities = HandleGenericAutorepliesActivities(RTClient(worker_config.rt))
        assert await test_activity_env.run(activities.is_autoresponse, mock_transaction_obj)
