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.link_with_ticket_on_link_with_header import (
    LinkWithTicketOnLinkWithHeaderActivities,
)


class TestLinkWithTicket:
    async def test_link_with_ticket_ticket_obj_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        activities = LinkWithTicketOnLinkWithHeaderActivities(RTClient(worker_config.rt))
        mock_transaction_obj.ticket_obj = None
        assert (await test_activity_env.run(activities.link_with_ticket, mock_transaction_obj)) is None

    async def test_link_with_ticket_ticket_data_is_none(
        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.link_with_ticket_on_link_with_header.RTClient.get_ticket_data",
        )
        get_ticket_data_mock.return_value = None
        activities = LinkWithTicketOnLinkWithHeaderActivities(RTClient(worker_config.rt))
        assert (await test_activity_env.run(activities.link_with_ticket, mock_transaction_obj)) is None
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)

    async def test_link_with_ticket_success(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        assert mock_transaction_obj.ticket_obj is not None
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.link_with_ticket_on_link_with_header.RTClient.get_ticket_data",
        )
        mock_ticket_data.headers.add_header("X-RT-Link-With", "1")
        get_ticket_data_mock.return_value = mock_ticket_data
        edit_link_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.link_with_ticket_on_link_with_header.RTClient.edit_link",
        )
        edit_link_mock.return_value = None
        activities = LinkWithTicketOnLinkWithHeaderActivities(RTClient(worker_config.rt))
        await test_activity_env.run(activities.link_with_ticket, mock_transaction_obj)
        get_ticket_data_mock.assert_called_once_with(mock_transaction_obj)
        edit_link_mock.assert_called_once_with(mock_transaction_obj.ticket_obj.id, "RefersTo", "1")
