from django.views.generic import ListView

from common.models import CompanyModel
from containers.models import Container


class ContainersListView(ListView):
    template_name = 'employee/containers-list.html'
    model = Container
    context_object_name = 'objects'
    paginate_by = 30  # Number of containers per page
    base_template = 'employee-base.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['base_template'] = self.base_template
        return context

    def get_queryset(self):
        return Container.objects.filter(expedited=False).order_by('-received_on')


class ReportContainersUnpaidListView(ListView):
    template_name = 'employee/payment-list.html'
    model = Container
    context_object_name = 'objects'
    paginate_by = 30  # Number of payments per page
    base_template = 'employee-base.html'

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['base_template'] = self.base_template
        context['companies'] = CompanyModel.objects.all().order_by('name')
        return context

    def get_queryset(self):
        queryset = self.model.objects.filter(expedited=True)

        # Get date from request parameters
        date = self.request.GET.get('date')
        if date:
            queryset = queryset.filter(expedited_on__date__lte=date)

        # Get company from request parameters
        company = self.request.GET.get('company')
        if company:
            queryset = queryset.filter(line__company_id=company)

        # Add payment filter to show only unpaid containers
        queryset = queryset.filter(payment_containers__isnull=True)

        return queryset.order_by('-expedited_on')