from django.shortcuts import get_object_or_404
from main.views import (
    NummiCreateView,
    NummiDeleteView,
    NummiDetailView,
    NummiListView,
    NummiUpdateView,
)
from statement.views import StatementListView
from transaction.views import TransactionListView

from .forms import AccountForm
from .models import Account


class AccountCreateView(NummiCreateView):
    model = Account
    form_class = AccountForm


class AccountUpdateView(NummiUpdateView):
    model = Account
    form_class = AccountForm
    pk_url_kwarg = "account"


class AccountDeleteView(NummiDeleteView):
    model = Account
    pk_url_kwarg = "account"


class AccountDetailView(NummiDetailView):
    model = Account
    pk_url_kwarg = "account"
    context_object_name = "account"


class AccountMixin:
    def get_queryset(self):
        self.account = get_object_or_404(
            Account.objects.filter(user=self.request.user),
            pk=self.kwargs.get("account"),
        )
        return super().get_queryset().filter(account=self.account)

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


class AccountListView(NummiListView):
    model = Account
    context_object_name = "accounts"


class AccountTListView(AccountMixin, TransactionListView):
    pass


class AccountSListView(AccountMixin, StatementListView):
    pass
