from datetime import date

from django.db.models import Sum
from django.utils import timezone
from django.views.generic import TemplateView

from gaehsnitz.models import Donation, Payment

festival_start_date, festival_end_date = date(2022, 8, 25), date(2022, 8, 28)


class GaehsnitzTemplateView(TemplateView):
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        delta_til_start = festival_start_date - date.today()
        days_til_start = max(delta_til_start.days, 0)
        context.update(
            {
                "days_til_festival_start": days_til_start,
            }
        )
        return context


class NewsView(GaehsnitzTemplateView):
    template_name = "gaehsnitz/news.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        year = timezone.now().year
        donations = Donation.objects.filter(date__year=year)
        payments = Payment.objects.filter(date__year=year)

        total_donations = donations.aggregate(sum=Sum("amount"))["sum"] or 0
        total_payments = payments.aggregate(sum=Sum("amount"))["sum"] or 0
        total_balance = total_donations - total_payments

        band_sum, displayed_payments = 0, []
        for pay in payments.order_by("date"):
            if pay.purpose.startswith("Band"):
                band_sum += pay.amount
            else:
                displayed_payments.append(pay)

        displayed_payments.append(Payment(purpose="Bands", amount=band_sum))

        context.update(
            {
                "total_donations": total_donations,
                "total_payments": total_payments,
                "total_balance": total_balance,
                "payments": displayed_payments,
            }
        )
        return context


class Archive2022View(GaehsnitzTemplateView):
    template_name = "gaehsnitz/archive-2022.html"


class Archive2024View(GaehsnitzTemplateView):
    template_name = "gaehsnitz/archive-2024.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)

        donations = Donation.objects.filter(date__year=2024)
        payments = Payment.objects.filter(date__year=2024)

        total_donations = donations.aggregate(sum=Sum("amount"))["sum"] or 0
        total_payments = payments.aggregate(sum=Sum("amount"))["sum"] or 0
        total_balance = total_donations - total_payments

        band_sum, displayed_payments = 0, []
        for pay in payments.order_by("date"):
            if pay.purpose.startswith("Band"):
                band_sum += pay.amount
            else:
                displayed_payments.append(pay)

        displayed_payments.append(Payment(purpose="Bands", amount=band_sum))

        context.update(
            {
                "total_donations": total_donations,
                "total_payments": total_payments,
                "total_balance": total_balance,
                "payments": displayed_payments,
            }
        )
        return context
