import logging

from django.http import HttpRequest, HttpResponse, JsonResponse

from django_hawk.utils import DjangoHawkAuthenticationFailed, authenticate_request

logger = logging.getLogger(__name__)


def simple_view(request: HttpRequest) -> HttpResponse:
    try:
        authenticate_request(request=request)
    except DjangoHawkAuthenticationFailed as e:
        logger.info("Authentication failed", e)
        return JsonResponse(
            status=401,
            data={
                "detail": e.message,
            },
        )
    return HttpResponse("This is a simple view")


def custom_header_view(request: HttpRequest) -> HttpResponse:
    try:
        authenticate_request(request=request)
    except DjangoHawkAuthenticationFailed as e:
        logger.info("Authentication failed", e)
        return JsonResponse(
            status=401,
            data={
                "detail": e.message,
            },
        )

    response = HttpResponse("This is a view with a custom header")
    response["Server-Authorization"] = "custom-header-value"

    return response
