from django.shortcuts import render, redirect
from django.views import View
from django.views.generic import ListView

from common.models import CompanyModel
from common.utils.utils import get_preinfo_by_number, get_container_by_number
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):
        queryset = super().get_queryset()

        data_filter = self.request.GET.get('filter')
        if data_filter != 'all':
            queryset = queryset.filter(expedited=False)
        queryset = queryset.order_by('-received_on')

        return queryset


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')


class ContainerSearchView(View):
    template_name = 'barrier/container-search.html'  # Single template for all searches

    def get(self, request):
        search_type = request.GET.get('param')  # container_receive or container_expedition
        return render(request, self.template_name, {'search_type': search_type})

    def post(self, request):
        number = request.POST.get('number')
        search_type = request.POST.get('search_type')

        if search_type == 'container_receive':
            preinfo = get_preinfo_by_number(number)
            if preinfo:
                return redirect('receive_container', pk=preinfo.pk)
        else:  # container_expedition
            container = get_container_by_number(number)
            if container:
                return redirect('expedite_container', pk=container.pk)

        return render(request, self.template_name, {
            'error': 'Not found',
            'search_type': search_type
        })