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

from .forms import CategoryForm
from .models import Category


class CategoryCreateView(NummiCreateView):
    model = Category
    form_class = CategoryForm


class CategoryUpdateView(NummiUpdateView):
    model = Category
    form_class = CategoryForm
    pk_url_kwarg = "category"


class CategoryDetailView(NummiDetailView):
    model = Category
    pk_url_kwarg = "category"
    context_object_name = "category"


class CategoryDeleteView(NummiDeleteView):
    model = Category
    pk_url_kwarg = "category"


class CategoryMixin:
    def get_queryset(self):
        self.category = get_object_or_404(
            Category.objects.filter(user=self.request.user),
            pk=self.kwargs.get("category"),
        )
        return super().get_queryset().filter(category=self.category)

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


class CategoryTListView(CategoryMixin, TransactionListView):
    pass
