text: from django.db.models import query
  from django.forms.forms import Form
  from django.shortcuts import render, redirect, reverse
  from django.core.mail import send_mail
  from django.contrib.auth.forms import UserCreationForm
  from django.http import HttpResponse
  from django.contrib.auth.mixins import LoginRequiredMixin
  from django.views.generic.edit import FormView
  from .models import Lead, Agent, Category
  from .forms import LeadModelForm, LeadForm, CustomUserCreationForm, AssignAgentForm, LeadCategoryUpdateForm
  from django.views.generic import TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView
  from agents.mixins import OrganizerAndLoginRequiredMixin
  
  class SignupView(CreateView):
      template_name = "registration/signup.html"
      form_class = CustomUserCreationForm
  
      def get_success_url(self):
          return reverse("login")
  
  class LandingPageView(TemplateView):
      template_name = "landing.html"
  
  def landing_page(request):
      return render(request, "landing.html")
  
  
  class LeadListView(LoginRequiredMixin, ListView):
      template_name = "leads/lead_list.html"
      context_object_name = "leads"
  
      def get_queryset(self):
          user = self.request.user
  
          #filter for organization
          if user.is_organizer:
              queryset = Lead.objects.filter(organization=user.userprofile, agent__isnull=False)
          else:
              queryset = Lead.objects.filter(organization=user.agent.organization, agent__isnull=False)
              #filter for agent
              queryset = queryset.filter(agent__user=user)
          return queryset
  
      def get_context_data(self, **kwargs):
          context = super(LeadListView, self).get_context_data(**kwargs)
          user = self.request.user
          if user.is_organizer:
              queryset = Lead.objects.filter(
                  organization=user.userprofile, 
                  agent__isnull=True
                  )
              context.update({
                  "unassigned_leads": queryset
              })
          return context
  
  
  def lead_list(request):
      leads = Lead.objects.all()
      context = {
          "leads": leads
          }
      return render(request, "leads/lead_list.html", context)
  
  class LeadDetailView(LoginRequiredMixin, DetailView):
      template_name = "leads/lead_detail.html"
      def get_queryset(self):
          user = self.request.user
  
          #filter for organization
          if user.is_organizer:
              queryset = Lead.objects.filter(organization=user.userprofile)
          else:
              queryset = Lead.objects.filter(organization=user.agent.organization)
              #filter for agent
              queryset = queryset.filter(agent__user=user)
          return queryset
      context_object_name = "lead"
  
  def lead_detail(request, pk):
      lead = Lead.objects.get(id=pk)
      context = {
          "lead": lead
      }
      return render(request, "leads/lead_detail.html", context)
  
  class LeadCreateView(OrganizerAndLoginRequiredMixin, CreateView):
      template_name = "leads/lead_create.html"
      form_class = LeadModelForm
  
      def get_success_url(self):
          return reverse("leads:lead-list")
  
      def form_valid(self, form):
          lead = form.save(commit=False)
          lead.organization = self.request.user.userprofile
          lead.save()
          #todo send email
          send_mail(
              subject = "A lead has been created",
              message = "Go to the site to see the new lead",
              from_email = "test@test.com",
              recipient_list= ["test2@test.com"]
          )
          return super(LeadCreateView, self).form_valid(form)
  
  def lead_create(request):
      form = LeadModelForm()
      if request.method == "POST":
          form = LeadModelForm(request.POST)
          if form.is_valid():
              form.save()
              return redirect("/leads")
      context = {
          "form": form
      }
      return render(request, "leads/lead_create.html", context)
  
  
  class LeadUpdateView(OrganizerAndLoginRequiredMixin, UpdateView):
      def get_queryset(self):
          user = self.request.user
          return Lead.objects.filter(organization=user.userprofile)
          
      template_name = "leads/lead_update.html"
      form_class = LeadModelForm
  
      def get_success_url(self):
          return reverse("leads:lead-list")
  
  def lead_update(request, pk):
      lead = Lead.objects.get(id=pk)
      form = LeadModelForm(instance=lead)
      if request.method == "POST":
          form = LeadModelForm(request.POST, instance=lead)
          if form.is_valid():
              form.save()
              return redirect("/leads")
      context = {
              "lead": lead,
              "form": form
          }
      return render(request, "leads/lead_update.html", context)
  
  class LeadDeleteView(OrganizerAndLoginRequiredMixin, DeleteView):
      def get_queryset(self):
          user = self.request.user
          return Lead.objects.filter(organization=user.userprofile)
      template_name = "leads/lead_delete.html"
      
      def get_success_url(self):
          return reverse("leads:lead-list")
  
  def lead_delete(request, pk):
      lead = Lead.objects.get(id=pk)
      lead.delete()
      return redirect("/leads")
  
  
  class AssignAgentView(OrganizerAndLoginRequiredMixin, FormView):
      template_name = "leads/assign_agent.html"
      form_class = AssignAgentForm
  
      def get_form_kwargs(self, **kwargs):
          kwargs = super(AssignAgentView, self).get_form_kwargs(**kwargs)
  
          kwargs.update ({
              "request": self.request
          })
          return kwargs
  
      def get_success_url(self):
          return reverse("leads:lead-list")
  
      def form_valid(self, form):
          agent = form.cleaned_data["agent"]
          lead = Lead.objects.get(id=self.kwargs["pk"])
          lead.agent = agent
          lead.save()
          return super(AssignAgentView, self).form_valid(form)
  
  
  class CategoryListView(LoginRequiredMixin, ListView):
      template_name = "leads/category_list.html"
      context_object_name = "category_list"
  
      def get_context_data(self, **kwargs):
          context = super(CategoryListView, self).get_context_data(**kwargs)
          user = self.request.user
          if user.is_organizer:
              queryset = Lead.objects.filter(organization=user.userprofile)
          else:
              queryset = Lead.objects.filter(organization=user.agent.organization)
  
          context.update({
              "unassigned_lead_count": queryset.filter(category__isnull=True).count()
          })
  
          return context
  
  
      def get_queryset(self):
          user = self.request.user
  
          #filter for organization
          if user.is_organizer:
              queryset = Category.objects.filter(organization=user.userprofile)
          else:
              queryset = Category.objects.filter(organization=user.agent.organization)
          return queryset
  
  class CategoryDetailView(LoginRequiredMixin, DetailView):
      template_name ="leads/category_detail.html"
      context_object_name = "category"
  
      
  
      # def get_context_data(self, **kwargs):
      #     context = super(CategoryDetailView, self).get_context_data(**kwargs)
          
      #     leads = self.get_object().leads.all()
          
  
      #     context.update({
      #         "leads": leads
      #     })
  
          # return context
  
      def get_queryset(self):
          user = self.request.user
  
          #filter for organization
          if user.is_organizer:
              queryset = Category.objects.filter(organization=user.userprofile)
          else:
              queryset = Category.objects.filter(organization=user.agent.organization)
          return queryset
  
  class LeadCategoryUpdateView(LoginRequiredMixin, UpdateView):
      def get_queryset(self):
          user = self.request.user
          if user.is_organizer:
              queryset = Lead.objects.filter(organization=user.userprofile)
          else:
              queryset = Lead.objects.filter(organization=user.agent.organization)
              queryset = queryset.filter(agent__user=user)
          return queryset
          
      template_name = "leads/lead_category_update.html"
      form_class = LeadCategoryUpdateForm
  
      def get_success_url(self):
          return reverse("leads:lead-detail", kwargs={"pk": self.get_object().id})