from account.models import Account
from django.shortcuts import get_object_or_404
from django.urls import reverse_lazy
from main.views import (
    NummiCreateView,
    NummiDeleteView,
    NummiDetailView,
    NummiListView,
    NummiUpdateView,
)
from transaction.views import TransactionListView

from .forms import StatementForm
from .models import Statement


class StatementCreateView(NummiCreateView):
    model = Statement
    form_class = StatementForm

    def get_initial(self):
        _queryset = Account.objects.filter(user=self.request.user)
        if "account" in self.kwargs:
            self.account = get_object_or_404(_queryset, pk=self.kwargs["account"])
        else:
            self.account = _queryset.first()
        return {"account": self.account}

    def get_form_kwargs(self):
        if "account" in self.kwargs:
            return super().get_form_kwargs() | {"disable_account": True}
        return super().get_form_kwargs()

    def get_context_data(self, **kwargs):
        if "account" in self.kwargs:
            return super().get_context_data(**kwargs) | {"account": self.account}
        return super().get_context_data(**kwargs)


class StatementUpdateView(NummiUpdateView):
    model = Statement
    form_class = StatementForm
    pk_url_kwarg = "statement"

    def get_context_data(self, **kwargs):
        data = super().get_context_data(**kwargs)
        statement = data["form"].instance

        _transactions = statement.transaction_set.all()

        return data | {
            "account": statement.account,
            "new_transaction_url": reverse_lazy(
                "new_transaction", kwargs={"statement": statement.pk}
            ),
            "transactions": _transactions,
        }


class StatementDeleteView(NummiDeleteView):
    template_name = "statement/confirm_delete.html"
    model = Statement
    pk_url_kwarg = "statement"


class StatementDetailView(NummiDetailView):
    model = Statement
    pk_url_kwarg = "statement"
    context_object_name = "statement"

    def get_context_data(self, **kwargs):
        data = super().get_context_data(**kwargs)
        statement = data.get("statement")

        return data | {
            "account": statement.account,
            "new_transaction_url": reverse_lazy(
                "new_transaction", kwargs={"statement": statement.pk}
            ),
            "transactions": statement.transaction_set.all(),
        }


class StatementListView(NummiListView):
    model = Statement
    context_object_name = "statements"
    paginate_by = 32


class StatementMixin:
    def get_queryset(self):
        self.statement = get_object_or_404(
            Statement.objects.filter(user=self.request.user),
            pk=self.kwargs.get("statement"),
        )
        return super().get_queryset().filter(statement=self.statement)

    def get_context_data(self, **kwargs):
        return super().get_context_data(**kwargs) | {"statement": self.statement}


class StatementTListView(StatementMixin, TransactionListView):
    pass
