import pytest_mock
from temporalio.testing import ActivityEnvironment

from csirtmu.beta_temporal.config import Config as WorkerConfig
from csirtmu.beta_temporal.external_services.netbox.client import NetboxClient
from csirtmu.beta_temporal.external_services.rt import structs
from csirtmu.beta_temporal.external_services.rt.client import RTClient
from csirtmu.beta_temporal.workflows.close_automatic_tickets import CloseAutomaticTicketActivities


class TestIsAutomaticTicketForClosing:
    async def test_is_automatic_ticket_for_closing_ticket_obj_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        mock_transaction_obj.ticket_obj = None
        activities = CloseAutomaticTicketActivities(RTClient(worker_config.rt), NetboxClient(worker_config.netbox))
        assert not (await test_activity_env.run(activities.is_automatic_ticket_for_closing, mock_transaction_obj))

    async def test_is_automatic_ticket_for_closing_subject_is_placeholder(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        assert mock_transaction_obj.ticket_obj is not None
        mock_transaction_obj.ticket_obj.subject = "Automatic ticket placeholder"
        activities = CloseAutomaticTicketActivities(RTClient(worker_config.rt), NetboxClient(worker_config.netbox))
        assert not (await test_activity_env.run(activities.is_automatic_ticket_for_closing, mock_transaction_obj))

    async def test_is_automatic_ticket_for_closing_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.close_automatic_tickets.RTClient.get_ticket_data"
        )
        get_ticket_data_mock.return_value = None
        activities = CloseAutomaticTicketActivities(RTClient(worker_config.rt), NetboxClient(worker_config.netbox))
        assert not (await test_activity_env.run(activities.is_automatic_ticket_for_closing, mock_transaction_obj))

    async def test_is_automatic_ticket_for_closing_is_from_muni(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.close_automatic_tickets.RTClient.get_ticket_data"
        )
        mock_ticket_data.headers.add_header("From", "test@muni.cz")
        get_ticket_data_mock.return_value = mock_ticket_data
        activities = CloseAutomaticTicketActivities(RTClient(worker_config.rt), NetboxClient(worker_config.netbox))
        assert not (await test_activity_env.run(activities.is_automatic_ticket_for_closing, mock_transaction_obj))

    async def test_is_automatic_ticket_for_closing_without_gw_custom_field(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.close_automatic_tickets.RTClient.get_ticket_data"
        )
        get_ticket_data_mock.return_value = mock_ticket_data
        activities = CloseAutomaticTicketActivities(RTClient(worker_config.rt), NetboxClient(worker_config.netbox))
        assert await test_activity_env.run(activities.is_automatic_ticket_for_closing, mock_transaction_obj)

    async def test_is_automatic_ticket_for_closing_with_gw_custom_field_false(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.close_automatic_tickets.RTClient.get_ticket_data"
        )
        get_ticket_data_mock.return_value = mock_ticket_data
        assert mock_transaction_obj.ticket_obj is not None
        mock_transaction_obj.ticket_obj.custom_fields.append(
            structs.CustomFieldStruct(id="", type="", url="", name="Gateway", _values=["0"])
        )
        activities = CloseAutomaticTicketActivities(RTClient(worker_config.rt), NetboxClient(worker_config.netbox))
        assert await test_activity_env.run(activities.is_automatic_ticket_for_closing, mock_transaction_obj)

    async def test_is_automatic_ticket_for_closing_with_gw_custom_field_true(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mock_ticket_data: structs.TicketData,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        get_ticket_data_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.close_automatic_tickets.RTClient.get_ticket_data"
        )
        get_ticket_data_mock.return_value = mock_ticket_data
        assert mock_transaction_obj.ticket_obj is not None
        mock_transaction_obj.ticket_obj.custom_fields.append(
            structs.CustomFieldStruct(id="", type="", url="", name="Gateway", _values=["1"])
        )
        activities = CloseAutomaticTicketActivities(RTClient(worker_config.rt), NetboxClient(worker_config.netbox))
        assert not await test_activity_env.run(activities.is_automatic_ticket_for_closing, mock_transaction_obj)
