"""Tests for DeploymentsResource."""

from __future__ import annotations

import httpx
import pytest

from appliku import Appliku


@pytest.fixture()
def deployment_data() -> dict[str, object]:
    return {
        "id": 10,
        "app_id": 5,
        "app_name": "my-app",
        "status": "completed",
        "branch": "main",
        "git_commit": "abc123",
        "is_active": True,
        "is_aborted": False,
    }


@pytest.fixture()
def log_entries() -> list[dict[str, object]]:
    return [
        {"id": 1, "message": "Building...", "timestamp": "2024-01-01T00:00:00Z"},
        {"id": 2, "message": "Deploying...", "timestamp": "2024-01-01T00:01:00Z"},
    ]


class TestDeploymentsList:
    def test_list_returns_paginated(
        self,
        mock_client: tuple[Appliku, list[httpx.Response]],
        deployment_data: dict[str, object],
        paginated_response: object,
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json=paginated_response([deployment_data])))  # type: ignore[operator]
        result = client.deployments.list("acme", 5)
        assert result.count == 1
        assert result.results[0].status == "completed"


class TestDeploymentsGet:
    def test_get_returns_deployment(
        self, mock_client: tuple[Appliku, list[httpx.Response]], deployment_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json=deployment_data))
        dep = client.deployments.get("acme", 5, 10)
        assert dep.id == 10


class TestDeploymentsLatest:
    def test_latest_returns_deployment(
        self, mock_client: tuple[Appliku, list[httpx.Response]], deployment_data: dict[str, object]
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json=deployment_data))
        dep = client.deployments.latest("acme", 5)
        assert dep.status == "completed"


class TestDeploymentsLogs:
    def test_logs_returns_entries(
        self,
        mock_client: tuple[Appliku, list[httpx.Response]],
        log_entries: list[dict[str, object]],
    ) -> None:
        client, responses = mock_client
        responses.append(httpx.Response(200, json=log_entries))
        entries = client.deployments.logs("acme", 5, 10)
        assert len(entries) == 2
        assert entries[0].message == "Building..."

    def test_stream_logs_yields_entries(
        self,
        mock_client: tuple[Appliku, list[httpx.Response]],
        log_entries: list[dict[str, object]],
    ) -> None:
        client, responses = mock_client
        # First poll: logs
        responses.append(httpx.Response(200, json=log_entries))
        # Then deployment status check: completed
        responses.append(
            httpx.Response(
                200,
                json={
                    "id": 10,
                    "app_id": 5,
                    "app_name": "my-app",
                    "status": "completed",
                    "is_active": False,
                    "is_aborted": False,
                },
            )
        )
        # Final log poll (empty)
        responses.append(httpx.Response(200, json=[]))

        entries = list(client.deployments.stream_logs("acme", 5, 10, poll_interval=0))
        assert len(entries) == 2
        assert entries[1].message == "Deploying..."
