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.set_custom_fields_on_create import SetCustomFieldsOnCreateActivities


class TestSetCustomFields:
    async def test_set_custom_fields_ticket_obj_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockFixture,
    ) -> None:
        activities = SetCustomFieldsOnCreateActivities(RTClient(worker_config.rt))
        mock_transaction_obj.ticket_obj = None
        assert (await test_activity_env.run(activities.set_custom_fields, mock_transaction_obj)) is None

    async def test_set_custom_fields_ticket_data_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockFixture,
    ) -> None:
        activities = SetCustomFieldsOnCreateActivities(RTClient(worker_config.rt))
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_custom_fields_on_create.RTClient.get_ticket_data"
        )
        get_ticket_data_mock.return_value = None
        assert (await test_activity_env.run(activities.set_custom_fields, mock_transaction_obj)) is None
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_set_custom_fields_success(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockFixture,
    ) -> None:
        activities = SetCustomFieldsOnCreateActivities(RTClient(worker_config.rt))
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_custom_fields_on_create.RTClient.get_ticket_data"
        )
        edit_ticket_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_custom_fields_on_create.RTClient.edit_ticket"
        )
        mock_ticket_data.headers.add_header("From", "from_header")
        mock_ticket_data.headers.add_header("Reply-To", "reply_to_header")
        mock_ticket_data.headers.add_header("X-Muni-Envelope-From", "x_env_from_header")
        mock_ticket_data.headers.add_header("X-Muni-O365-Test", "x_o365_header")
        mock_ticket_data.headers.add_header("X-Muni-Envelope-To", "x_env_to_header")
        mock_ticket_data.headers.add_header("To", "to_header")
        mock_ticket_data.headers.add_header("X-Get-Message-Sender-Via", "get_message_header")
        mock_ticket_data.headers.add_header("X-Authenticated-Sender", "auth_header")
        mock_ticket_data.headers.add_header("Disposition-Notification-To", "dispo_header")
        get_ticket_data_mock.return_value = mock_ticket_data
        edit_ticket_mock.return_value = None
        assert (await test_activity_env.run(activities.set_custom_fields, mock_transaction_obj)) is None
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        assert mock_transaction_obj.ticket_obj is not None
        edit_ticket_mock.assert_called_once_with(
            mock_transaction_obj.ticket_obj.id,
            CustomFields={
                "From": "from_header",
                "Reply-To": "reply_to_header",
                "X-Muni-Envelope-To": "x_env_to_header",
                "X-Muni-O365-Test": "x_o365_header",
                "X-Muni-Envelope-From": "x_env_from_header",
                "To": "to_header",
                "X-Get-Message-Sender-Via": "get_message_header",
                "X-Authenticated-Sender": "auth_header",
                "Disposition-Notification-To": "dispo_header",
            },
        )
