from django.views.generic import TemplateView
from django.conf import settings
from _keenthemes.__init__ import KTLayout
from _keenthemes.libs.theme import KTTheme
from authentication.forms import SigninForm
from django.contrib.auth import authenticate, login, logout
from django.shortcuts import redirect, render
from django.http import HttpResponse
from django.views import View
from django.contrib import messages
from authentication.models import Role,User


"""
This file is a view controller for multiple pages as a module.
Here you can override the page view layout.
Refer to urls.py file for more pages.
"""

def signin(request):
    if request.user.is_authenticated:
        # user=login(request, user)
        # request.session['email'] = email
        return redirect("/dashboard")
    else:
    #   messages.error(request, 'Invalid Username or Password.')
       return render(request,'pages/auth/signin.html')  
    
def postsignin(request):
    if request.method =='POST':
        email = request.POST.get("email")
        password = request.POST.get("password")

        if not email and not password:
                messages.error(request, 'Please Enter Both Email And Password.')
                return redirect("/")
            
        if not email:
            messages.error(request, 'Please Enter Email.')
            return redirect("/")
        
        if not password:
            messages.error(request, 'Please Enter Password.')
            return redirect("/")
            
        if User.objects.filter(email=email, role__in=[3, 4, 5]).exclude(role__in=[1, 2]).exists():  
             messages.error(request, 'You do not have permission to log in.')
             return redirect("/")     
        
        if User.objects.filter(email=email, role=1 ,is_active=False) or User.objects.filter(email=email, role=2 , is_active=False):
           messages.error(request, 'Your account is inactive.')
           return redirect("/")  
       
       

        user = authenticate(email=email, password=password)


        
        if user is not None and user.is_active:
            if (Role.ADMIN in user.role.values_list('id', flat=True) or
                Role.SALES in user.role.values_list('id', flat=True) or
                user.is_superuser):
                login(request, user)
                request.session['email'] = email
                return redirect("/dashboard")
            else:
                messages.error(request, 'You do not have permission to log in.')
        else:
            messages.error(request, 'Invalid email or password.')
    return redirect("/")  


class AuthSigninView(TemplateView):
    template_name = 'pages/auth/signin.html'

    def get_context_data(self, **kwargs):

        # Call the base implementation first to get a context


        context = super().get_context_data(**kwargs)

        # A function to init the global layout. It is defined in _keenthemes/__init__.py file
        context = KTLayout.init(context)

        KTTheme.addJavascriptFile('js/custom/authentication/sign-in/general.js')

        # Define the layout for this module
        # _templates/layout/auth.html
        context.update({
            'layout': KTTheme.setLayout('auth.html', context),
        })

        return context
    
   