import pytest
import pytest_mock
from freezegun import freeze_time
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_starts_on_create import SetStartsOnCreateActivities


class TestSetStartsOnCreate:
    async def test_set_starts_on_create_ticket_obj_is_none(
        self,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mocker: pytest_mock.MockerFixture,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        activities = SetStartsOnCreateActivities(RTClient(worker_config.rt))
        mock_transaction_obj.ticket_obj = None
        edit_ticket_mock = mocker.patch("csirtmu.beta_temporal.workflows.set_starts_on_create.RTClient.edit_ticket")
        assert await test_activity_env.run(activities.set_starts_on_create, mock_transaction_obj) is None
        edit_ticket_mock.assert_not_called()

    @pytest.mark.parametrize("weekday", range(7))
    async def test_set_starts_on_create_weekdays(
        self,
        weekday: int,
        worker_config: WorkerConfig,
        mock_transaction_obj: structs.TransactionStruct,
        mocker: pytest_mock.MockerFixture,
        test_activity_env: ActivityEnvironment,
    ) -> None:
        assert mock_transaction_obj.ticket_obj is not None
        activities = SetStartsOnCreateActivities(RTClient(worker_config.rt))
        edit_ticket_mock = mocker.patch("csirtmu.beta_temporal.workflows.set_starts_on_create.RTClient.edit_ticket")
        # 2025-02-22 is saturday
        starts = "2025-02-24 00:00:00" if weekday < 2 else f"2025-02-{22 + weekday} 09:21:15"
        with freeze_time(f"2025-02-{22 + weekday} 09:21:15"):
            await test_activity_env.run(activities.set_starts_on_create, mock_transaction_obj)
        edit_ticket_mock.assert_called_once_with(mock_transaction_obj.ticket_obj.id, Starts=starts)
