# -*- coding: utf-8 -*-
"""
健康检查端点
用于 Nginx/LB 探活和数据库连接状态探测。
"""

from fastapi import APIRouter, Depends
from sqlalchemy import text
from sqlalchemy.ext.asyncio import AsyncSession

from app.core.database import get_db

router = APIRouter()


@router.get("/health", summary="健康检查", tags=["系统"])
async def health_check(db: AsyncSession = Depends(get_db)):
    """
    探测服务与数据库连接是否存活。
    - 数据库可达 → {"status": "healthy", "database": "connected"}
    - 数据库不可达 → {"status": "degraded", "database": "disconnected", "detail": "..."}
    """
    try:
        await db.execute(text("SELECT 1"))
        return {"status": "healthy", "database": "connected"}
    except Exception as e:
        return {
            "status": "degraded",
            "database": "disconnected",
            "detail": str(e),
        }
