from .models import Question, Answer
from .forms import QuestionForm, AnswerForm, AnswerAcceptanceForm
from django.shortcuts import render
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.detail import DetailView
from django.views.generic.dates import DayArchiveView
from django.views.generic.base import RedirectView
from django.views.generic.base import TemplateView
from django.http import HttpResponseBadRequest, HttpResponseRedirect
from django.utils import timezone
from django.urls.base import reverse
from .service.elasticsearch import search_for_questions


class QuestionCreateView(LoginRequiredMixin, CreateView):
    form_class = QuestionForm
    template_name = "quena/ask.html"

    def get_initial(self):
        return {
            "user": self.request.user.id,
        }
    
    def form_valid(self, form):
        action = self.request.POST.get("action")
        if action == "SAVE":
            return super().form_valid(form)
        elif action == "PREVIEW":
            preview = Question(
                question = form.cleaned_data["question"],
                title = form.cleaned_data["title"],
            )
            ctx = self.get_context_data(preview=preview)
            return self.render_to_response(context=ctx)
        return HttpResponseBadRequest


class QuestionDetailView(DetailView):
    model = Question

    ACCEPT_FORM = AnswerAcceptanceForm(initial={"accepted": True})
    REJECT_FORM = AnswerAcceptanceForm(initial={"accepted": False})

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        ctx.update({
            "answer_form": AnswerForm(initial={
                "user": self.request.user.id,
                "question": self.object.id,
            })
        })

        if self.object.can_accept_answers(self.request.user):
            ctx.update({
                "accept_form": self.ACCEPT_FORM,
                "reject_form": self.REJECT_FORM,
            })
        return ctx


class AnswerCreateView(LoginRequiredMixin, CreateView):
    form_class = AnswerForm
    template_name = "quena/create_answer.html"

    def get_initial(self):
        return {
            "question": self.get_question().id,
            "user": self.request.user.id,
        }
    
    def get_context_data(self, **kwargs):
        return super().get_context_data(question=self.get_question(), **kwargs)
    
    def get_success_url(self):
        return self.object.question.get_absolute_url()
    
    def form_valid(self, form):
        action = self.request.POST.get("action")
        if action == 'SAVE':
            return super().form_valid(form)
        elif action == "PREVIEW":
            ctx = self.get_context_data(preview=form.cleaned_data["answer"])
            return self.render_to_response(context=ctx)
        return HttpResponseBadRequest()
    
    def get_question(self):
        return Question.objects.get(pk=self.kwargs["pk"])


class UpdateAnswerAcceptanceView(LoginRequiredMixin, UpdateView):
    form_class = AnswerAcceptanceForm
    queryset = Answer.objects.all()

    def get_success_url(self):
        return self.object.question.get_absolute_url()
    
    def form_valid(self, form):
        action = self.request.POST.get("action")
        if action == "REJECT":
            self.object.accepted = False
            self.object.save()
            return HttpResponseRedirect(redirect_to=self.object.question.get_absolute_url())
        elif action == "ACCEPT":
            self.object.accepted = True
            self.object.save()
            return HttpResponseRedirect(redirect_to=self.object.question.get_absolute_url())


class DailyQuestionListView(DayArchiveView):
    queryset = Question.objects.all()
    date_field = "created"
    month_format = "%m"
    allow_empty = True


class TodayQuestionListView(RedirectView):
    def get_redirect_url(self, *args, **kwargs):
        today = timezone.now()
        return reverse("quena:daily_questions",
            kwargs={
                "day": today.day,
                "month": today.month,
                "year": today.year,
            }
        )


class SearchView(TemplateView):
    template_name = "quena/search.html"

    def get_context_data(self, **kwargs):
        query = self.request.GET.get("question", None)
        ctx = super().get_context_data(query=query, **kwargs)
        if query:
            results = search_for_questions(query)
            ctx["hits"] = results
        return ctx