from django.http import JsonResponse
from django.shortcuts import render
from django.views.decorators.http import require_POST

from .health import run_health_checks


def health_dashboard(request):
    report = run_health_checks(include_details=bool(request.user.is_authenticated and request.user.is_staff))
    if request.GET.get("format") == "json" or "application/json" in request.headers.get("Accept", ""):
        return JsonResponse(report, status=503 if report["status"] == "error" else 200)
    return render(request, "health/dashboard.html", {"report": report})


@require_POST
def health_run(request):
    report = run_health_checks(include_details=bool(request.user.is_authenticated and request.user.is_staff))
    return JsonResponse(report, status=503 if report["status"] == "error" else 200)
