"""
Tests for the main FastAPI application.
"""

import pytest
from fastapi import FastAPI
from fastapi.testclient import TestClient

from app.main import app


class TestMainApp:
    """Test the main FastAPI application."""

    def test_app_creation(self):
        """Test that the FastAPI app is created correctly."""
        assert isinstance(app, FastAPI)
        assert app.title == "AI Feedback Processing Service"
        assert app.version == "2.0.0"

    def test_app_has_cors_middleware(self):
        """Test that CORS middleware is configured."""
        # Check if CORS middleware is configured
        assert len(app.user_middleware) > 0

    def test_app_includes_api_router(self):
        """Test that the API router is included."""
        # Check that the app has routes
        assert len(app.routes) > 0

    def test_health_endpoint_exists(self):
        """Test that the health endpoint exists."""
        routes = [route.path for route in app.routes]
        # Check that we have health endpoints
        health_routes = [r for r in routes if "health" in r]
        assert len(health_routes) > 0


class TestHealthEndpoint:
    """Test the health endpoint."""

    @pytest.mark.asyncio
    async def test_health_endpoint_response(self, client: TestClient):
        """Test the health endpoint returns a response."""
        response = client.get("/health")
        assert response.status_code == 200
        assert "status" in response.json()

    @pytest.mark.asyncio
    async def test_health_endpoint_structure(self, client: TestClient):
        """Test the health endpoint response structure."""
        response = client.get("/health")
        data = response.json()

        # Check basic structure
        assert "status" in data
        assert "timestamp" in data
        assert "checks" in data

        # Check checks structure
        checks = data["checks"]
        assert "database" in checks
        assert "aws" in checks
        assert "queue" in checks


class TestAppLifespan:
    """Test the application lifespan management."""

    @pytest.mark.asyncio
    async def test_lifespan_startup(self):
        """Test application startup."""
        # This is a basic test - in a real scenario you'd mock the database
        # and background services to avoid actual connections
        assert app is not None

    def test_app_has_lifespan(self):
        """Test that the app has a lifespan manager."""
        assert hasattr(app, "router")
        # The lifespan is set during app creation, so we can't easily test it
        # without mocking the entire startup process
