from http.client import HTTPResponse
from typing import List
from django.shortcuts import redirect
from django.utils import translation
from django.views.generic.base import View
from django.apps import AppConfig
from django.http import Http404, HttpResponse, HttpResponseBadRequest, response
from django.urls import Resolver404
from django.views.generic import TemplateView, ListView
from django.views import View
from django.template import Template, context, loader
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.generic.list import QuerySet
from django.db.models import Q

from .forms import MainSearchForm
from .models import Searchable

from abc import ABC

class SearchableView(View, ABC):
    """
    This abstract view implements some traits of views that should show up
    in the main search
    """
    title: str
    subtitle: str
    desc: str
    date: None = None
    keywords: list[str]


class Index(TemplateView, SearchableView):
    """
    The index page of the gawa app.

    Utilizes a generic view, because I will do so for all views, 
    a regular view function would suffice.
    """

    template_name: str = "start/index.html"

class Professional(TemplateView, SearchableView):
    """
    Professional informations that might interest a professional employer
    """
    # TODO
    template_name: str = "start/legalinfo.html"

class LegalInfo(TemplateView, SearchableView):
    """
    Legal info that the german authorities want.
    """
    # TODO
    template_name: str = "start/legalinfo.html"

class ActivateLanguage(View):
    """
    Set the language to whatever
    """
    language_code = ''
    redirect_to   = ''

    def get(self, request, *args, **kwargs):
        self.redirect_to   = request.META.get('HTTP_REFERER')
        self.language_code = kwargs.get('language_code')
        translation.activate(self.language_code)
        request.session[translation.LANGUAGE_SESSION_KEY] = self.language_code
        return redirect(self.redirect_to)

class MainSearch(ListView):
    """
    Search for anything.
    """

    model = Searchable
    object_list = []    # this is only declaration, the view breaks without it.
    template_name: str = "start/search.html"

    def get_queryset(self) -> QuerySet:
        search = self.request.GET.get("search")
        object_list = Searchable.objects.filter(
            Q(title_de__icontains=search) | Q(title_en__icontains=search) |
            Q(subtitle_de__icontains=search) | Q(subtitle_en__icontains=search) |
            Q(desc_de__icontains=search) | Q(desc_en__icontains=search)
        )
        print(object_list)
        return object_list

    def get(self, request, *args, **kwargs):
        form = MainSearchForm(request.GET)
        if not form.is_valid():
            return render(request, "errors/bad_request.html")
        return super().get(request, *args, **kwargs)
