"""Tests for ClustersResource."""

from __future__ import annotations

import httpx
import pytest

from appliku import Appliku


@pytest.fixture()
def cluster_data() -> dict[str, object]:
    return {"id": 1, "name": "production", "apps_count": 5, "domains_count": 3}


class TestClustersList:
    def test_list_returns_clusters(
        self,
        mock_client: tuple[Appliku, list[httpx.Response]],
        cluster_data: dict[str, object],
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json=[cluster_data]))
        result = client.clusters.list("acme")
        assert len(result) == 1
        assert result[0].name == "production"


class TestClustersCreate:
    def test_create_returns_cluster(
        self, mock_client: tuple[Appliku, list[httpx.Response]], cluster_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(201, json=cluster_data))
        cluster = client.clusters.create("acme", "production")
        assert cluster.id == 1


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


class TestClustersServiceLogs:
    def test_service_logs_filters_by_cluster(
        self,
        mock_client: tuple[Appliku, list[httpx.Response]],
    ) -> None:
        client, responses = mock_client
        # App list response — one app in cluster 1, one in cluster 2
        apps = [
            {"id": 10, "name": "app-a", "branch": "main", "cluster_id": 1},
            {"id": 11, "name": "app-b", "branch": "main", "cluster_id": 2},
        ]
        responses.append(httpx.Response(200, json=apps))
        # Service logs for app-a (cluster 1)
        responses.append(httpx.Response(200, json={"id": 50, "logs": "worker log from app-a"}))
        result = client.clusters.service_logs("acme", 1, "worker")
        assert len(result) == 1
        assert result[0][0].name == "app-a"
        assert result[0][1].logs == "worker log from app-a"

    def test_service_logs_empty_cluster(
        self,
        mock_client: tuple[Appliku, list[httpx.Response]],
    ) -> None:
        client, responses = mock_client
        # All apps in a different cluster
        apps = [{"id": 10, "name": "app-x", "branch": "main", "cluster_id": 99}]
        responses.append(httpx.Response(200, json=apps))
        result = client.clusters.service_logs("acme", 1, "web")
        assert result == []
