from __future__ import annotations

from fastapi import APIRouter, Depends

from services.base.redis import RedisService
from v1.infra.dependencies import get_redis_service
from v1.infra.schemas import InfraHealthResponse, ServiceHealth


router = APIRouter(prefix="/infra", tags=["infra"])


@router.get("/health", response_model=InfraHealthResponse)
async def infra_health(
    redis_service: RedisService = Depends(get_redis_service),
) -> InfraHealthResponse:
    if not redis_service.is_initialized:
        await redis_service.initialize()

    redis_health = await redis_service.health_check()
    status = "healthy" if redis_health["status"] == "healthy" else "unhealthy"

    return InfraHealthResponse(
        status=status,
        services={
            "redis": ServiceHealth(**redis_health),
        },
    )
