from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from rest_framework.permissions import AllowAny
from django.db import connection

class HealthCheckView(APIView):
    permission_classes = [AllowAny]

    def get(self, request):
        try:
            with connection.cursor() as cursor:
                cursor.execute("SELECT 1")
                cursor.fetchone()
            return Response({"status": "healthy", "database": "connected"}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({"status": "unhealthy", "error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
