from django.views.generic import TemplateView
from django.conf import settings
from _keenthemes.__init__ import KTLayout
from _keenthemes.libs.theme import KTTheme
from django.http import HttpResponse
from django.urls import reverse_lazy
from django.views.generic.edit import FormView
from authentication.forms import SignUpForm
from authentication.models import User
from django.shortcuts import render,HttpResponse,redirect
from django.views import View
from authentication.models import User
from django.contrib import messages
import re
"""
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 postsignup(request):
    if request.method == 'POST':
        email = request.POST.get('email')
        username = request.POST.get('email')  # Assuming email is the username
        role = request.POST.get('role')
        terms_accepted = request.POST.get('terms_accepted')
        password = request.POST.get('password')
        confirm_password = request.POST.get('confirm_password')

        # Field validations

            
        if not email and not password:
                messages.error(request, 'Please enter both email and password.')
                return redirect("/signup")
        
        if not email:
            messages.error(request, 'Please enter an email.')
            return redirect('/signup')
        
        if not username:
            messages.error(request, 'Please enter a username.')
            return redirect('/signup')

     
    
        if not password:
            messages.error(request, 'Please enter a password.')
            return redirect('/signup')

        if not confirm_password:
            messages.error(request, 'Please confirm your password.')
            return redirect('/signup')

        if password != confirm_password:
            messages.error(request, 'Password and Repeat Password do not match.')
            return redirect('/signup')
        
        if not role:
            messages.error(request, 'Please select a role.')
            return redirect('/signup')

        
        if not terms_accepted:
            messages.error(request, 'Please accept the terms and conditions.')
            return redirect('/signup')


        user_exists = User.objects.filter(email=email).exists()

        if user_exists:
            messages.error(request, 'A user with this email already exists.')
            return redirect('/signup')

        user = User(email=email, username=username, role=role, terms_accepted=True)
        user.set_password(password)
        user.save()
        messages.success(request, 'User successfully registered')
        return redirect('/')
    else:
        return render(request, 'signup.html')
    
       
    


class AuthSignupView(TemplateView):
    template_name = 'pages/auth/signup.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-up/general.js')

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

        return context
