from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.urls import reverse_lazy
from django.views.generic import TemplateView, ListView, CreateView, UpdateView

from booking.models import Booking
from common.forms.company import CompanyCreateForm, CompanyUpdateForm
from common.forms.line import LineCreateForm, LineUpdateForm
from common.models import CompanyModel, LinesModel
from containers.models import Container
from preinfo.models import Preinfo


class EmployeeDashboardView(TemplateView):
    template_name = 'employee-dashboard-content.html'
    extra_context = {
        'title': 'Employee Dashboard',
        'description': 'This is the depot employee dashboard page.',
    }

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        containers = Container.objects.filter(expedited=False).count()
        preinfos = Preinfo.objects.filter(received=False).count()
        bookings = Booking.objects.filter(status='active').count()
        context['containers'] = containers
        context['preinfos'] = preinfos
        context['bookings'] = bookings
        return context


class EmployeeCompanyListView(LoginRequiredMixin, UserPassesTestMixin, ListView):
    model = CompanyModel
    template_name = 'common/../../templates/employee/company-list.html'
    context_object_name = 'objects'
    paginate_by = 3
    # base_template = 'employee-base.html'

    def test_func(self):
        return True # self.request.user.has_employee_perm('can_view_preinfo') or self.request.user.user_type == 'CA'

    # 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(active=True)

        queryset = queryset.order_by('name')

        return queryset

class EmployeeCompanyCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
    model = CompanyModel
    template_name = 'common/../../templates/employee/company-create.html'
    form_class = CompanyCreateForm
    success_url = reverse_lazy('employee_company')

    def test_func(self):
        return True # self.request.user.has_company_perm('can_create_preinfo') or self.request.user.user_type == 'CA'

    # def form_valid(self, form):
    #     form.instance.created_by = self.request.user
    #     form.instance.updated_by = self.request.user
    #     return super().form_valid(form)



class EmployeeCompanyUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = CompanyModel
    template_name = 'common/../../templates/employee/company-update.html'
    form_class = CompanyUpdateForm
    success_url = reverse_lazy('employee_company')

    def test_func(self):
        return True # self.request.user.ha.s_company_perm('can_edit_preinfo') or self.request.user.user_type == 'CA'

    # def form_valid(self, form):
    #     form.instance.updated_by = self.request.user
    #     return super().form_valid(form)


class EmployeeLineListView(LoginRequiredMixin, UserPassesTestMixin, ListView):
    model = LinesModel
    template_name = 'employee/line-list.html'
    context_object_name = 'objects'
    paginate_by = 3
    # base_template = 'employee-base.html'

    def test_func(self):
        return True # self.request.user.has_employee_perm('can_view_preinfo') or self.request.user.user_type == 'CA'

    # 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(active=True)

        queryset = queryset.order_by('name')

        return queryset

class EmployeeLineCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
    model = LinesModel
    template_name = 'employee/company-create.html'
    form_class = LineCreateForm
    success_url = reverse_lazy('employee_line')

    def test_func(self):
        return True # self.request.user.has_company_perm('can_create_preinfo') or self.request.user.user_type == 'CA'

    # def form_valid(self, form):
    #     form.instance.created_by = self.request.user
    #     form.instance.updated_by = self.request.user
    #     return super().form_valid(form)



class EmployeeLineUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = LinesModel
    template_name = 'employee/company-update.html'
    form_class = LineUpdateForm
    success_url = reverse_lazy('employee_line')

    def test_func(self):
        return True # self.request.user.ha.s_company_perm('can_edit_preinfo') or self.request.user.user_type == 'CA'

    # def form_valid(self, form):
    #     form.instance.updated_by = self.request.user
    #     return super().form_valid(form)
