"""
Tests for health check endpoints.
"""

from unittest.mock import AsyncMock

import pytest
from fastapi.testclient import TestClient

from app.services.health import HealthService


class TestHealthEndpoints:
    """Test health check endpoints."""

    def test_health_check_success(self, client: TestClient):
        """Test successful health check."""
        response = client.get("/api/v1/health/health")

        assert response.status_code == 200
        data = response.json()
        assert "status" in data
        assert "timestamp" in data
        assert "checks" in data

    def test_health_check_failure(self, client: TestClient):
        """Test health check failure."""
        # This test would require mocking the database connection
        # For now, we'll test the endpoint exists
        response = client.get("/api/v1/health/health")
        assert response.status_code in [200, 500]  # Either success or failure

    def test_readiness_check_ready(self, client: TestClient):
        """Test readiness check when service is ready."""
        response = client.get("/api/v1/health/ready")

        assert response.status_code == 200
        data = response.json()
        assert "status" in data
        assert "message" in data

    def test_readiness_check_not_ready(self, client: TestClient):
        """Test readiness check when service is not ready."""
        response = client.get("/api/v1/health/ready")

        assert response.status_code == 200
        data = response.json()
        assert "status" in data
        assert "message" in data

    def test_readiness_check_error(self, client: TestClient):
        """Test readiness check when service encounters an error."""
        response = client.get("/api/v1/health/ready")

        assert response.status_code == 200
        data = response.json()
        assert "status" in data
        assert "message" in data

    def test_liveness_check(self, client: TestClient):
        """Test liveness check endpoint."""
        response = client.get("/api/v1/health/live")

        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "alive"
        assert "Service is running" in data["message"]


class TestHealthService:
    """Test the HealthService class."""

    @pytest.mark.asyncio
    async def test_check_all_success(self, mock_health_service):
        """Test successful health check all."""
        result = await mock_health_service.check_all()

        assert result["status"] == "healthy"
        assert "timestamp" in result
        assert "checks" in result

        checks = result["checks"]
        assert "database" in checks
        assert "aws" in checks
        assert "queue" in checks

    @pytest.mark.asyncio
    async def test_is_ready_success(self, mock_health_service):
        """Test successful readiness check."""
        result = await mock_health_service.is_ready()
        assert result is True

    @pytest.mark.asyncio
    async def test_is_ready_failure(self):
        """Test readiness check failure."""
        mock_service = AsyncMock()
        mock_service.is_ready.return_value = False

        result = await mock_service.is_ready()
        assert result is False

    def test_health_service_creation(self):
        """Test HealthService can be instantiated."""
        service = HealthService()
        assert service is not None
        assert hasattr(service, "check_all")
        assert hasattr(service, "is_ready")
