from django.contrib.auth import logout
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import User
from django.contrib.auth.views import LogoutView
from django.http import JsonResponse
from django.shortcuts import render, redirect
from django.urls import reverse_lazy
from django.views import generic
from django.views.generic import CreateView
from .forms import RegisterUserForm, SearchForm
from .models import Application
from .models import Product
from .models import*
# Create your views here.
def index(request):
    return render(request, 'index.html')
def profil(request):
    return render(request, 'profil.html')

def login(request):
    return render(request, 'registration/login.html')

def registration(request):
    return render(request, 'registration.html')

# def logout(request):
#     return render(request, 'logout.html')

class RegisterView(CreateView):
    template_name = 'registration.html'
    form_class = RegisterUserForm
    success_url = reverse_lazy('login')
def validate_username(request):
    username = request.GET.get('username', None)
    response = {
        'is_taken': User.objects.filter(username__iexact=username).exists()
    }
    return JsonResponse(response)
class BBLogoutView(LogoutView):
    # template_name = 'logout.html'
    success_url = reverse_lazy('index')

class ApplicationListView(generic.ListView):
    model = Application
    template_name = 'index.html'
    context_object_name = 'applications'

class ApplicationsByUserListView(LoginRequiredMixin, generic.ListView):
    model = Application
    template_name = 'profil.html'
    context_object_name = 'applications'

    def get_queryset(self):
        return Application.objects.filter(user=self.request.user)




def search_view(request):
    query = request.GET.get('query', '')
    results = Product.objects.filter(name__icontains=query)
    form = SearchForm({'query': query})

    context = {'results': results, 'form': form}
    return render(request, 'search.html', context)


class ApplicationDelete(ApplicationListView):
    model = Application
    context_object_name = 'application'
    template_name = 'deleting.html'
    success_url = reverse_lazy('request')

    def delete_application(self, pk):
        application = Application.objects.filter(user=self.request.user, pk=pk)

        if application:
            application.delete()
        return redirect('request')



    def logout_view(request):
        logout(request)
        return redirect('base.html')