import logging
from fastapi import APIRouter
import pendulum

from app.config import get_settings

logger = logging.getLogger(f'{__name__}.health')

router = APIRouter()
settings = get_settings()


@router.get("/health")
async def health_check():
    """Health check endpoint"""
    return {
        "status": "healthy",
        "timestamp": pendulum.now('UTC').to_iso8601_string(),
        "environment": settings.app_env,
        "version": "1.0.0"
    }


@router.get("/health/db")
async def database_health():
    """Database health check"""
    from app.database.session import engine
    from sqlalchemy import text

    try:
        async with engine.connect() as conn:
            await conn.execute(text("SELECT 1"))

        return {
            "status": "healthy",
            "database": "connected",
            "timestamp": pendulum.now('UTC').to_iso8601_string()
        }
    except Exception as e:
        logger.error(f"Database health check failed: {e}")
        return {
            "status": "unhealthy",
            "database": "disconnected",
            "error": str(e),
            "timestamp": pendulum.now('UTC').to_iso8601_string()
        }
