import json
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.forms import AuthenticationForm
from django.shortcuts import render, redirect
from rest_framework.exceptions import AuthenticationFailed
from rest_framework.permissions import AllowAny
from rest_framework.response import Response
from rest_framework.serializers import Serializer, CharField
from rest_framework.views import APIView

from passkeys.api.token_backends import get_token_response

def loginView(request):
    if request.method == "POST":
        passkeys_raw = request.POST.get("passkeys", "")
        if passkeys_raw:
            user = authenticate(request)
            if user:
                login(request, user)
                return redirect(request.POST.get("next", "") or "principal")
            form = AuthenticationForm(request)
            form.add_error(None, "Passkey authentication failed. Try again.")
        else:
            form = AuthenticationForm(request, data=request.POST)
            if form.is_valid():
                user = form.get_user()
                login(request, user)
                return redirect(request.POST.get("next", "") or "principal")
    else:
        form = AuthenticationForm(request)
    return render(request, "registration/login.html", {"form": form})

# def create_session(request,username):
#     user=User.objects.get(username=username)
#     user.backend='django.contrib.auth.backends.ModelBackend'
#     login(request, user)
#     return HttpResponseRedirect(reverse('home'))


def logoutView(request):
    logout(request) # pragma: no cover
    return  render(request,"logout.html",{}) # pragma: no cover


class LoginSerializer(Serializer):
    username = CharField()
    password = CharField()


class LoginAPIView(APIView):
    """
    Username + password login API.

    Returns the same token format as passkey authentication
    (JWT, DRF Token, or session depending on project config).
    Use this to get an initial token before registering passkeys.
    """

    permission_classes = [AllowAny]
    serializer_class = LoginSerializer

    def post(self, request):
        serializer = LoginSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        user = authenticate(
            request,
            username=serializer.validated_data['username'],
            password=serializer.validated_data['password'],
        )
        if user is None:
            raise AuthenticationFailed("Invalid username or password")
        token_data = get_token_response(user, request)
        return Response({
            'user_id': user.pk,
            'username': user.get_username(),
            **token_data,
        })