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_status_rejected_on_returned_mail import (
    SetStatusRejectedOnReturnedMailActivities,
)


class TestRejectMail:
    async def test_reject_mail_ticket_obj_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        activities = SetStatusRejectedOnReturnedMailActivities(RTClient(worker_config.rt))
        mock_transaction_obj.ticket_obj = None
        edit_custom_field_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_status_rejected_on_returned_mail.RTClient.edit_custom_field",
        )
        assert not await test_activity_env.run(activities.reject_mail, mock_transaction_obj)
        edit_custom_field_mock.assert_not_called()

    async def test_reject_mail_success(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        test_activity_env: ActivityEnvironment,
        mocker: pytest_mock.MockerFixture,
    ) -> None:
        assert mock_transaction_obj.ticket_obj is not None
        activities = SetStatusRejectedOnReturnedMailActivities(RTClient(worker_config.rt))
        edit_custom_field_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_status_rejected_on_returned_mail.RTClient.edit_custom_field",
        )
        edit_ticket_mock = mocker.patch(
            "csirtmu.beta_temporal.workflows.set_status_rejected_on_returned_mail.RTClient.edit_ticket",
        )
        assert not await test_activity_env.run(activities.reject_mail, mock_transaction_obj)
        edit_custom_field_mock.assert_called_once_with(
            mock_transaction_obj.ticket_obj.id, "Category", "Unable to contact"
        )
        edit_ticket_mock.assert_called_once_with(mock_transaction_obj.ticket_obj.id, Status="rejected")
