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

from DepoT.mixins.LineFiltweFormMixin import LineFilterFormMixin
from common.forms.company import CompanyCreateForm, CompanyUpdateForm
from common.forms.line import LineCreateForm, LineUpdateForm
from common.models import CompanyModel, LinesModel
from common.utils.utils import filter_queryset_by_user


class ClientDashboardView(TemplateView):
    template_name = 'client-dashboard-content.html'
    extra_context = {
        'title': 'Client Dashboard',
        'description': 'This is the client dashboard page.',
    }

    def get(self, request, *args, **kwargs):
        return render(request, self.template_name, self.extra_context)



class ClientCompanyListView(LoginRequiredMixin, UserPassesTestMixin, ListView):
    model = CompanyModel
    template_name = 'common/../../templates/employee/company-list.html'
    context_object_name = 'objects'
    paginate_by = 3
    # base_template = 'client-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()
        user = self.request.user
        result = filter_queryset_by_user(queryset, user)
        return result



class ClientCompanyCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
    model = CompanyModel
    template_name = 'common/../../templates/employee/company-create.html'
    form_class = CompanyCreateForm
    success_url = reverse_lazy('client-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 ClientCompanyUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = CompanyModel
    template_name = 'common/../../templates/employee/company-update.html'
    form_class = CompanyUpdateForm
    success_url = reverse_lazy('client-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 ClientLineListView(LoginRequiredMixin, UserPassesTestMixin, ListView):
    model = LinesModel
    template_name = 'client/line-list.html'
    context_object_name = 'objects'
    paginate_by = 3
    # base_template = 'client-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()
        user = self.request.user
        result = queryset.filter(company=user.company)
        return result


class ClientLineCreateView(LoginRequiredMixin, UserPassesTestMixin, CreateView):
    model = LinesModel
    template_name = 'client/line-create.html'
    form_class = LineCreateForm

    success_url = reverse_lazy('client-line')

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

    def get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.fields['company'].queryset = CompanyModel.objects.filter(id=self.request.user.company.id)
        form.fields['company'].initial = self.request.user.company.id
        return form

    def form_valid(self, form):
        # todo more validation
        form.instance.company = self.request.user.company
        return super().form_valid(form)

class ClientLineUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
    model = LinesModel
    template_name = 'client/line-update.html'
    form_class = LineUpdateForm
    success_url = reverse_lazy('client-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 get_form(self, form_class=None):
        form = super().get_form(form_class)
        form.fields['company'].queryset = CompanyModel.objects.filter(id=self.request.user.company.id)
        form.fields['company'].initial = self.request.user.company.id
        return form

    def form_valid(self, form):
        # todo more validation
        form.instance.company = self.request.user.company
        return super().form_valid(form)
