"""Health check endpoints for Chrysopedia API."""

import logging

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

from database import get_session
from schemas import HealthResponse

logger = logging.getLogger("chrysopedia.health")

router = APIRouter(tags=["health"])


@router.get("/health", response_model=HealthResponse)
async def health_check(db: AsyncSession = Depends(get_session)) -> HealthResponse:
    """Root health check — verifies API is running and DB is reachable."""
    db_status = "unknown"
    try:
        result = await db.execute(text("SELECT 1"))
        result.scalar()
        db_status = "connected"
    except Exception:
        logger.warning("Database health check failed", exc_info=True)
        db_status = "unreachable"

    return HealthResponse(
        status="ok",
        service="chrysopedia-api",
        version="0.1.0",
        database=db_status,
    )
