"""
Health check endpoints.
"""

from typing import Any

from fastapi import APIRouter, HTTPException

from app.core.logging import get_logger
from app.services.health import HealthService

logger = get_logger("health_endpoints")

router = APIRouter()


@router.get("/health")
async def health_check() -> dict[str, Any]:
    """Comprehensive health check."""
    try:
        health_service = HealthService()
        return await health_service.check_all()
    except Exception as e:
        logger.error(f"Health check failed: {e}")
        raise HTTPException(
            status_code=500, detail=f"Health check failed: {e!s}"
        ) from e


@router.get("/ready")
async def readiness_check() -> dict[str, Any]:
    """Readiness check for the service."""
    try:
        health_service = HealthService()
        is_ready = await health_service.is_ready()

        if is_ready:
            return {"status": "ready", "message": "Service is ready to handle requests"}
        else:
            return {"status": "not_ready", "message": "Service is not ready"}
    except Exception as e:
        logger.error(f"Readiness check failed: {e}")
        return {"status": "error", "message": f"Readiness check failed: {e!s}"}


@router.get("/live")
async def liveness_check() -> dict[str, str]:
    """Liveness check for the service."""
    return {"status": "alive", "message": "Service is running"}
