from datetime import date, timedelta
from django.contrib.auth.mixins import LoginRequiredMixin
from django.views.generic import ListView, TemplateView

from .models import Agenda, Document, Membre


class HomeView(TemplateView):
    template_name="index.html"

    def get_context_data(self, **kwargs):
        return {
            **super().get_context_data(**kwargs),
            "items": Agenda.objects.filter(
                date_heure__date__gte=date.today(), prive=False,
            ).order_by("date_heure")[:2],
        }


class PresentationView(TemplateView):
    template_name="presentation.html"

    def get_context_data(self, **kwargs):
        return {
            **super().get_context_data(**kwargs),
            "next_event": Agenda.objects.filter(
                date_heure__date__gte=date.today(), prive=False,
            ).order_by("date_heure").first(),
        }


class AgendaView(ListView):
    model = Agenda
    template_name = "agenda.html"

    def get_queryset(self):
        qs = Agenda.objects.filter(
            date_heure__gt=date.today() - timedelta(days=3),
        ).order_by("date_heure")
        if not self.request.user.is_authenticated:
            qs = qs.filter(prive=False)
        return qs


class MediaView(ListView):
    model = Document
    template_name = "media.html"
    prive = False

    def get_queryset(self):
        return Document.objects.filter(prive=self.prive).order_by("-quand", "titre")

    def get_context_data(self, **kwargs):
        return {
            **super().get_context_data(**kwargs),
            "title": {
                False: "Enregistrements, photos et vidéos de la chorale",
                True: "Documents privés",
            }.get(self.prive)
        }


class EspaceMembresView(LoginRequiredMixin, TemplateView):
    template_name = "membres/index.html"


class ListeMembresView(LoginRequiredMixin, ListView):
    model = Membre
    template_name = "membres/liste.html"
