import socket
import asyncio

from fastapi import APIRouter, Depends
from starlette.responses import Response
from sqlalchemy.ext.asyncio import AsyncSession

from app.api.deps import get_session

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


@router.get("/", status_code=204)
async def health(session: AsyncSession = Depends(get_session)):
    try:
        await asyncio.wait_for(session.execute("SELECT 1"), timeout=1)
    except (asyncio.TimeoutError, socket.gaierror):
        return Response(status_code=503)
    return Response(status_code=204)

@router.get('/check', status_code=200)
def healthcheck():
    """
    Performs a health check on the application.

    Returns:
        dict: A JSON response indicating the health status.
    """ 
    return {'healthcheck': 'Everything OK!'}
 
