"""Tests for MigrationsResource."""

from __future__ import annotations

import httpx
import pytest

from appliku import Appliku


@pytest.fixture()
def migration_data() -> dict[str, object]:
    return {
        "id": 1,
        "command": "python manage.py migrate",
        "status": "completed",
        "created_at": "2024-01-01T00:00:00Z",
        "completed_at": "2024-01-01T00:01:00Z",
    }


class TestMigrationsList:
    def test_list_returns_paginated(
        self,
        mock_client: tuple[Appliku, list[httpx.Response]],
        migration_data: dict[str, object],
        paginated_response: object,
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json=paginated_response([migration_data])))  # type: ignore[operator]
        result = client.migrations.list("acme")
        assert result.count == 1
        assert result.results[0].command == "python manage.py migrate"


class TestMigrationsGet:
    def test_get_returns_migration(
        self, mock_client: tuple[Appliku, list[httpx.Response]], migration_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json=migration_data))
        migration = client.migrations.get("acme", 1)
        assert migration.status == "completed"


class TestMigrationsRun:
    def test_run_returns_migration(self, mock_client: tuple[Appliku, list[httpx.Response]]) -> None:
        client, responses = mock_client
        new_migration = {
            "id": 2,
            "command": "python manage.py migrate",
            "status": "running",
            "created_at": "2024-01-01T00:02:00Z",
        }
        responses.append(httpx.Response(201, json=new_migration))
        migration = client.migrations.run("acme", 42, "python manage.py migrate")
        assert migration.status == "running"


class TestMigrationsLogs:
    def test_logs_returns_entries(self, mock_client: tuple[Appliku, list[httpx.Response]]) -> None:
        client, responses = mock_client
        logs = [
            {"id": 1, "message": "Running migrations...", "timestamp": "2024-01-01T00:00:00Z"},
            {"id": 2, "message": "Done.", "timestamp": "2024-01-01T00:01:00Z"},
        ]
        responses.append(httpx.Response(200, json=logs))
        entries = client.migrations.logs("acme", 1)
        assert len(entries) == 2
        assert entries[0].message == "Running migrations..."
