import datetime

from django.shortcuts import render, HttpResponse, redirect
from django.conf import settings
# Create your views here.
from django.contrib import messages
from .models import user,appointment
from django.contrib.auth.hashers import check_password ,make_password
from django.contrib.auth import authenticate, login
from django.core.files.images import get_image_dimensions
from .context_processors import my_constants
from django.db import connection
from django.shortcuts import get_object_or_404
from django.core import signing
from django.urls import reverse
import base64
import os
from PIL import Image
import io
from django.template.loader import render_to_string
from visitor_management import settings
from requests.auth import HTTPBasicAuth
from django.utils.html import strip_tags
from django.core.mail import send_mail, EmailMultiAlternatives
def home(request):
    return redirect('gate_keeper')
    # return HttpResponse('hiiiiiii')

def date_time():
    now = datetime.datetime.now()
    date_time =now.strftime("%y-%m-%d %H:%M:%S")
    return date_time
def generate_vms_string():
    now = datetime.datetime.now()
    vms_string = now.strftime("VMS%d%m%y%H%M%S")
    return vms_string
def register(request, encrypted_appointment_id):
    redirect_url = reverse('register', args=[encrypted_appointment_id])
    constants = my_constants(request)
    database_name = constants['database_name']
    from_email = constants['From_Email']
    try:
        appointment_id = signing.loads(encrypted_appointment_id)
        appointment_id = int(appointment_id)
    except Exception as e:
        messages.error(request, 'Invalid appointment ID.')
        return redirect(redirect_url)
    
    appointmentes = get_object_or_404(appointment, id=appointment_id)
    if appointmentes.status == 'check out':
        message = 'Your Appointment is closed.'
        return render(request, 'registration_form/thank_you.html',{'message':message,'appointment_id':appointment_id})
    
    
    all_employee_id = appointmentes.employee_id
    all_visitor_id = appointmentes.visitors_id
    
    # if user.objects.filter(id=all_visitor_id).exists():
    #     visitor = user.objects.get(id=all_visitor_id)
    #     if visitor.first_name:
    #         message = 'You have been already Registered.'
    #         return render(request, 'registration_form/thank_you.html',{'message':message})
    if all_employee_id:
        sql_query = f'''
            SELECT * FROM {database_name}.users WHERE id = %s
        '''
        with connection.cursor() as cursor:
            cursor.execute(sql_query, [all_employee_id])
            user_data = cursor.fetchone()
            if user_data:
                columns = [col[0] for col in cursor.description]  # Get column names
                user_data_dict = dict(zip(columns, user_data))  # Convert to dictionary
                # Process the user_data_dict as needed
            else:
                messages.error(request, 'No user found for the given ID.')
    if all_visitor_id:
        sql_query_visitor = f'''
            SELECT * FROM {database_name}.users WHERE id = %s
        '''
        with connection.cursor() as cursor:
            cursor.execute(sql_query_visitor, [all_visitor_id])
            visitor_data = cursor.fetchone()
            if visitor_data:
                columns = [col[0] for col in cursor.description]  # Get column names
                user_visitor = dict(zip(columns, visitor_data))
            else:
                messages.error(request, 'No visitor found for the given visitor ID.')
    

    if request.method == 'POST':
        
        firstname = request.POST.get('Firstname', '')

        lastname = request.POST.get('Lastname', '')

        email = request.POST.get('Email', '')

        # password = request.POST.get('Password', '')

        # Confirm_Password = request.POST.get('Confirm_Password', '')

        Address = request.POST.get('Address', '')

        mobiles = request.POST.get('mobile', '')

        gender = request.POST.get('gender', '')
        
        image  = request.POST['user_image']
        
        if image:
            base64_data = image.split(",")[1]  
            binary_data = base64.b64decode(base64_data)  
            image = Image.open(io.BytesIO(binary_data)) 

            # Save the image to a directory
            save_directory = 'user/' 
            if not os.path.exists(save_directory):
                os.makedirs(save_directory)
            filename = firstname.replace(' ','_')     
            file_date = generate_vms_string()
            new_file_name =  file_date.replace('VMS','')
            filename = f"{filename}_{new_file_name}.jpg"
            face_image_path = os.path.join(save_directory, filename)

            image.save(face_image_path)
        
        if firstname == ''  or mobiles == '' or lastname == '' or Address == '' or gender == '' or image == '':
            messages.error(request, 'All Filed......', extra_tags='danger')
            return redirect(redirect_url)
        
        if user.objects.filter(id=all_visitor_id).exists():
            visitor = user.objects.get(id=all_visitor_id)
            visitor.first_name = firstname
            visitor.last_name = lastname
            # visitor.email = email
            visitor.mobile = mobiles
            visitor.gender = gender
            visitor.address = Address
            visitor.image = face_image_path
            visitor.updated_at = date_time()
             
            try:
                visitor.save()
                subject_visitor = 'Appointment Confirmation'
                context_visitor = {
                    'text':'Your invitation form successfully Fillup by visitor.'
                }
                
                template_name_visitor = 'registration_form/email_send.html'
                message_visitor = render_to_string(template_name_visitor, context_visitor)
                plain_message_visitor = strip_tags(message_visitor)
                recipient_list_visitor = [user_data_dict['email']]

                mail_visitor = EmailMultiAlternatives(subject_visitor, plain_message_visitor, from_email, recipient_list_visitor)
                mail_visitor.attach_alternative(message_visitor, "text/html")
                mail_visitor.send()
                message = 'Thank you for Registration.'
                return render(request, 'registration_form/thank_you.html',{'thank_message':message,'registered':'false','appointment_id':appointment_id})
            except Exception as e:
                print(e)
                messages.error(request, 'Error while updating visitor.')
                return redirect(redirect_url)
        else:
            messages.error(request, 'Visitor not found for updating.')
        
    else:
        if all_visitor_id:
            visitor = user.objects.get(id=all_visitor_id)
            if visitor.first_name != "":  # Check if visitor is already registered
                message = 'You have been already Registered.'
                return render(request, 'registration_form/thank_you.html', {'thank_message': message, 'registered': 'true','appointment_id':appointment_id})
      
    return render(request,'registration_form/main_file.html',{'user_data_dict':user_data_dict,'user_visitor':user_visitor})



def visitors(request):
    context = {'function_name': 'Visitors', 'ADMIN_STATIC_PATH': settings.ADMIN_STATIC_PATH}
    if request.method == 'POST':
        email = request.POST.get('email', '')
        password = request.POST.get('password', '')
        user_type = request.POST.get('user_type', '')
        if email == '' or password == '':
            messages.error(request, 'All fields are required.')
            return render(request, 'registration/login.html', context)
        abc = user.objects.filter(email=email, is_active=True, type="visitors").first()
        if abc is not None:
            if check_password(password, abc.password):
                user_name = abc.first_name
                vistior = {'user_email': email, 'user_password': password,'user_name':user_name}
                request.session['visitors'] = vistior
                # request.session['user_type'] = user_type
                # return render(request, 'dashboard/visitors_dashboard/dashboard.html', {'username': user_name})
                return redirect(f'visitors_dashboard')
            else:
                messages.error(request, 'Invalid Email or Password.')
        else:
            messages.error(request, 'Invalid Login.')

        return render(request, 'registration/login.html', context)

    return render(request, 'registration/login.html', context)

# def visitors(request):
#     context = {'ADMIN_STATIC_PATH': settings.ADMIN_STATIC_PATH}
#     if request.method == 'POST':
#         email = request.POST.get('email', '')
#         password = request.POST.get('password', '')
#         user_type = request.POST.get('user_type', '')

#         if email == '' or password == '':
#             messages.error(request, 'All fields are required.')
#             context['function_name'] = 'Login'
#             return render(request, 'registration/login.html', context)
        
#         user_instance = user.objects.filter(email=email, is_active=True, type=user_type).first()

#         if user_instance is not None:
#             if check_password(password, user_instance.password):
#                 user_name = user_instance.first_name
                
#                 # Store user information in session
#                 user_data = {
#                     'user_email': email,
#                     'user_name': user_name,
#                     'user_type': user_type,
#                 }
                
                
#                 # Redirect to the appropriate dashboard based on user type
#                 if user_type == 'admin':
#                     context['function_name'] = 'Admin Dashboard'
#                     return redirect('admin_dashboard')
#                 elif user_type == 'gate_keeper':
#                     request.session['gate_keeper'] = user_data
#                     context['gate_keeper'] = 'gate_keeper'
#                     return redirect('gate_keeper_dashboard')
#                 elif user_type == 'employee':
#                     context['function_name'] = 'Employee Dashboard'
#                     return redirect('employee_dashboard')
#                 elif user_type == 'visitor':
#                     context['function_name'] = 'Visitor Dashboard'
#                     return redirect('visitor_dashboard')
#                 else:
#                     messages.error(request, 'Invalid user type.')
#                     context['function_name'] = 'Login'
#                     return render(request, 'registration/login.html', context)
#             else:
#                 messages.error(request, 'Invalid login password.')
#                 context['function_name'] = 'Login'
#         else:
#             messages.error(request, 'Invalid login.')
#             context['function_name'] = 'Login'

#         return render(request, 'registration/login.html', context)

#     # Default context for GET request
#     context['function_name'] = 'Login'
#     return render(request, 'registration/login.html', context)



def visitors_logout(request):
    if request.session.get('visitors'):
        del request.session['visitors']
    return redirect('visitors')
# admin_layout.html
def dashboard_view(request, user_type):
    username = request.session.get('visitors')
    if not username or request.session.get('user_type') != user_type:
        return redirect('visitors')

    context = {'username': username}

    return render(request, f'dashboard/{user_type}_dashboard/dashboard.html', context)


# def visitors_dashboard(request):
#     # return dashboard_view(request, 'visitors')
#     username = request.session.get('visitors')

#     # user_type = request.session.get('user_type')
#     # print('user_type:-', user_type)

#     if not username :
#         return redirect('visitors')
#         # return redirect('gate_keeper')
#     all_appointment = appointment.objects.all().count()
#     username['all_appointment'] = all_appointment
#     context = {'username': username}
#     email = username['user_email']
#     user_name = username['user_name']
#     useres = user.objects.filter(email=email, first_name=user_name).first()
#     user_image = useres.image

    
#     return render(request, 'dashboard/visitors_dashboard/dashboard.html', context)

    # return render(request,'dashboard/visitors_dashboard/dashboard.html',{'username':username})

def visitors_dashboard(request):
    # Call my_constants to get domain and user information
    constants = my_constants(request)
    
    username = request.session.get('visitors')
    if not username:
        return redirect('visitors')
    user_data = constants.get('user_data', {})
    user_id = user_data.get('id')
    all_appointment = appointment.objects.filter(visitors_id=user_id).count()
    username['all_appointment'] = all_appointment


    now = datetime.datetime.now()
    date = now.strftime("%Y-%m-%d")
    time = now.strftime("%I:%M %p").lstrip('0')  # 12-hour format

    # SQL date and time filter
    date_filter = f"""
                AND(appointment.date > '{date}' OR (appointment.date = '{date}' AND appointment.time > '{time}'))

            """
    user_id_filter = f"AND appointment.visitors_id = {user_id}"
    # First SQL query to get the count of appointments
    sql_query_count = f"""
                SELECT 
                    COUNT(*) AS appointment_count
                FROM 
                    vms.appointment
                INNER JOIN 
                    vms.users AS visitors ON visitors.id = appointment.visitors_id
                INNER JOIN 
                    vms.users AS employees ON employees.id = appointment.employee_id
                WHERE 1=1
                {date_filter}{user_id_filter};
            """

    with connection.cursor() as cursor:
        cursor.execute(sql_query_count)
        database_all_data = cursor.fetchall()

    Upcoming_appointment = database_all_data[0][0] if database_all_data else 0

    context = {

        'username': username,
        'DOMAIN_NAME': constants['DOMAIN_NAME'],
        'DOMAIN_ICON': constants['DOMAIN_ICON'],
        'user_data': constants['user_data'],
        'all_appointment': all_appointment,
        'Upcoming_appointment':Upcoming_appointment
    }
    return render(request, 'dashboard/visitors_dashboard/dashboard.html', context)