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_report_uuid import SetReportUUIDActivities


class TestSetUuid:
    async def test_set_uuid_ticket_obj_is_none(
        self,
        worker_config: WorkerConfig,
        test_activity_env: ActivityEnvironment,
        mock_transaction_obj: structs.TransactionStruct,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        activities = SetReportUUIDActivities(RTClient(worker_config.rt))
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_report_uuid.RTClient.get_ticket_data",
        )
        mock_transaction_obj.ticket_obj = None
        assert (await test_activity_env.run(activities.set_report_uuid, mock_transaction_obj)) is None
        get_ticket_data_mock.assert_not_called()

    async def test_set_uuid_ticket_data_is_none(
        self,
        worker_config: WorkerConfig,
        test_activity_env: ActivityEnvironment,
        mock_transaction_obj: structs.TransactionStruct,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        activities = SetReportUUIDActivities(RTClient(worker_config.rt))
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_report_uuid.RTClient.get_ticket_data",
        )
        get_ticket_data_mock.return_value = None
        edit_ticket_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_report_uuid.RTClient.edit_ticket",
        )
        assert (await test_activity_env.run(activities.set_report_uuid, mock_transaction_obj)) is None
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        edit_ticket_mock.assert_not_called()

    async def test_set_uuid_no_report_uuid_header(
        self,
        worker_config: WorkerConfig,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mock_transaction_obj: structs.TransactionStruct,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        activities = SetReportUUIDActivities(RTClient(worker_config.rt))
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_report_uuid.RTClient.get_ticket_data",
        )
        get_ticket_data_mock.return_value = mock_ticket_data
        edit_ticket_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_report_uuid.RTClient.edit_ticket",
        )
        assert (await test_activity_env.run(activities.set_report_uuid, mock_transaction_obj)) is None
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        edit_ticket_mock.assert_not_called()

    async def test_set_uuid_correct_report_uuid_header(
        self,
        worker_config: WorkerConfig,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mock_transaction_obj: structs.TransactionStruct,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        assert mock_transaction_obj.ticket_obj is not None
        activities = SetReportUUIDActivities(RTClient(worker_config.rt))
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_report_uuid.RTClient.get_ticket_data",
        )
        mock_ticket_data.headers.add_header("X-RT-Report-UUID", "test_uuid")
        get_ticket_data_mock.return_value = mock_ticket_data
        edit_ticket_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_report_uuid.RTClient.edit_ticket",
        )
        await test_activity_env.run(activities.set_report_uuid, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        edit_ticket_mock.assert_called_once_with(mock_transaction_obj.ticket_obj.id, CustomFields={"uuid": "test_uuid"})
