from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.views import LoginView
from django.shortcuts import render
from django.urls import reverse_lazy
from django.views import generic
from django.views.generic import TemplateView, CreateView

from .forms import RegisterUserForm
from .models import CustUser, Product


class IndexView(generic.ListView):
    model = Product
    template_name = "main/index.html"
    context_object_name = "products"

    def get_queryset(self):
        return Product.objects.order_by('-date')[:5]


class BBLoginView(LoginView):
    template_name = "main/login.html"


@login_required
def logout_view(request):
    logout(request)
    return render(request, 'main/index.html')


class RegisterUserView(CreateView):
    model = CustUser
    template_name = "main/register.html"
    form_class = RegisterUserForm
    success_url = reverse_lazy("main:login")


class ProductListView(generic.ListView):
    model = Product
    template_name = "main/products.html"
    context_object_name = "products"


class ProductDetailView(generic.DetailView):
    model = Product
    template_name = "main/product_detail.html"


class ProfileListView(generic.ListView):
    model = CustUser
    template_name = "main/profile.html"
