"""Tests for CronJobsResource."""

from __future__ import annotations

import httpx
import pytest

from appliku import Appliku


@pytest.fixture()
def cron_data() -> dict[str, object]:
    return {
        "id": 1,
        "name": "cleanup",
        "schedule": "0 2 * * *",
        "command": "python cleanup.py",
        "is_disabled": False,
    }


class TestCronJobsList:
    def test_list_returns_paginated(
        self,
        mock_client: tuple[Appliku, list[httpx.Response]],
        cron_data: dict[str, object],
        paginated_response: object,
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json=paginated_response([cron_data])))  # type: ignore[operator]
        result = client.cron_jobs.list("acme", 42)
        assert result.count == 1
        assert result.results[0].name == "cleanup"


class TestCronJobsCreate:
    def test_create_returns_cron_job(
        self, mock_client: tuple[Appliku, list[httpx.Response]], cron_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(201, json=cron_data))
        cron = client.cron_jobs.create("acme", 42, "cleanup", "0 2 * * *", "python cleanup.py")
        assert cron.name == "cleanup"


class TestCronJobsUpdate:
    def test_update_returns_cron_job(
        self, mock_client: tuple[Appliku, list[httpx.Response]]
    ) -> None:
        client, responses = mock_client
        updated = {
            "id": 1,
            "name": "cleanup",
            "schedule": "0 3 * * *",
            "command": "python cleanup.py",
            "is_disabled": True,
        }
        responses.append(httpx.Response(200, json=updated))
        cron = client.cron_jobs.update("acme", 42, 1, schedule="0 3 * * *", is_disabled=True)
        assert cron.is_disabled is True


class TestCronJobsDelete:
    def test_delete_succeeds(self, mock_client: tuple[Appliku, list[httpx.Response]]) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(204))
        client.cron_jobs.delete("acme", 42, 1)
