"""Tests for DatastoresResource."""

from __future__ import annotations

import httpx
import pytest

from appliku import Appliku


@pytest.fixture()
def ds_data() -> dict[str, object]:
    return {
        "id": 1,
        "name": "primary-db",
        "engine": "postgres",
        "version": "15",
        "is_primary": True,
        "status": "running",
    }


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


class TestDatastoresGet:
    def test_get_returns_datastore(
        self, mock_client: tuple[Appliku, list[httpx.Response]], ds_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json=ds_data))
        ds = client.datastores.get("acme", 42, 1)
        assert ds.name == "primary-db"


class TestDatastoresCreate:
    def test_create_returns_datastore(
        self, mock_client: tuple[Appliku, list[httpx.Response]], ds_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(201, json=ds_data))
        ds = client.datastores.create("acme", 42, "primary-db", "postgres", version="15")
        assert ds.id == 1


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


class TestDatastoresLifecycle:
    def test_start(
        self, mock_client: tuple[Appliku, list[httpx.Response]], ds_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        ds_data["status"] = "running"
        responses.append(httpx.Response(200, json=ds_data))
        ds = client.datastores.start("acme", 42, 1)
        assert ds.status == "running"

    def test_stop(
        self, mock_client: tuple[Appliku, list[httpx.Response]], ds_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        ds_data["status"] = "stopped"
        responses.append(httpx.Response(200, json=ds_data))
        ds = client.datastores.stop("acme", 42, 1)
        assert ds.status == "stopped"

    def test_restart(
        self, mock_client: tuple[Appliku, list[httpx.Response]], ds_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        ds_data["status"] = "running"
        responses.append(httpx.Response(200, json=ds_data))
        ds = client.datastores.restart("acme", 42, 1)
        assert ds.status == "running"


class TestDatastoresLogs:
    def test_logs_returns_response(self, mock_client: tuple[Appliku, list[httpx.Response]]) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json={"id": 5, "logs": "db migration completed\n"}))
        result = client.datastores.logs("acme", 42, 1)
        assert result.id == 5
        assert "migration" in result.logs
