from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
from django.views.generic.list import ListView
from verify_email.email_handler import send_verification_email

from collect_image.models import FoodImage
from .forms import CustomUserCreationForm
from .models import CustomUser


class SignUpView(CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy("login")  # as opposed to reverse()
    template_name = "registration/signup.html"

    def form_valid(self, form):
        if form.is_valid():
            inactive_user = send_verification_email(  # noqa: F841
                self.request, form
            )
            return super().form_valid(form)


class DashboardView(LoginRequiredMixin, ListView):
    models = CustomUser
    template_name = "dashboard.html"
    # TODO: create some sort of form for the user to change their data

    def get_context_data(self, **kwargs):
        context = super(DashboardView, self).get_context_data(**kwargs)
        context["user_details"] = CustomUser.objects.get(
            pk=self.request.user.pk
        )
        context["user_upload_total"] = FoodImage.objects.filter(
            owner=CustomUser.objects.get(pk=self.request.user.pk)
        ).count()

        return context

    def get_queryset(self):
        return FoodImage.objects.filter(
            owner=CustomUser.objects.get(pk=self.request.user.pk)
        )

from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView
from django.views.generic.list import ListView
from verify_email.email_handler import send_verification_email

from collect_image.models import FoodImage
from .forms import CustomUserCreationForm
from .models import CustomUser


class SignUpView(CreateView):
    form_class = CustomUserCreationForm
    success_url = reverse_lazy("login")  # as opposed to reverse()
    template_name = "registration/signup.html"

    def form_valid(self, form):
        if form.is_valid():
            inactive_user = send_verification_email(  # noqa: F841
                self.request, form
            )
            return super().form_valid(form)


class DashboardView(LoginRequiredMixin, ListView):
    models = CustomUser
    template_name = "dashboard.html"
    # TODO: create some sort of form for the user to change their data

    def get_context_data(self, **kwargs):
        context = super(DashboardView, self).get_context_data(**kwargs)
        context["user_details"] = CustomUser.objects.get(
            pk=self.request.user.pk
        )
        context["user_upload_total"] = FoodImage.objects.filter(
            owner=CustomUser.objects.get(pk=self.request.user.pk)
        ).count()

        return context

    def get_queryset(self):
        return FoodImage.objects.filter(
            owner=CustomUser.objects.get(pk=self.request.user.pk)
        )

# as opposed to reverse() # noqa: F841 # TODO: create some sort of form for the user to change their data