from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
from django.contrib.auth import views as auth_views

from django.contrib.auth.decorators import login_required

from .custom_forms import CustomUserCreationForm , CustomPasswordChangeForm

def signup_view(request):
    hide_navbar = True
    if request.method == 'POST':
        form = CustomUserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('home')  # Redirect to a homepage or dashboard
    else:
        form = CustomUserCreationForm()
    return render(request, 'authapp/registration/signup.html', {'form': form})


from django.contrib.auth import update_session_auth_hash
from django.shortcuts import render, redirect
from django.contrib import messages


@login_required
def change_password(request):
    if request.method == 'POST':
        form = CustomPasswordChangeForm(user=request.user, data=request.POST)
        if form.is_valid():
            user = form.save()
            update_session_auth_hash(request, user)  # Important to keep the user logged in
            messages.success(request, 'Your password was successfully updated!')
            return redirect('home')  # Redirect to a success page
    else:
        form = CustomPasswordChangeForm(user=request.user)
    
    return render(request, 'authapp/registration/change_password.html', {'form': form})

from django.urls import reverse_lazy

class CustomPasswordResetView(auth_views.PasswordResetView):
    template_name = 'authapp/registration/password_reset_form.html'
    html_email_template_name = 'authapp/registration/password_reset_email.html'
    success_url = reverse_lazy('password_reset_done')




class CustomPasswordResetConfirmView(auth_views.PasswordResetConfirmView):
    template_name = 'authapp/registration/password_reset_confirm.html'
    success_url = reverse_lazy('password_reset_complete')

class CustomPasswordResetCompleteView(auth_views.PasswordResetCompleteView):
    template_name = 'authapp/registration/password_reset_complete.html'



















# # yourapp/views.py
# # yourapp/views.py
# from django.shortcuts import render
# from django.http import HttpResponse
# from django.core.mail import EmailMessage
# # from django.conf import settings

# def send_test_email_view_correct(request):
#     # Email parameters
#     subject = 'Test Email with URL'
#     html_message = """
#     <!DOCTYPE html>
#     <html>
#     <head>
#         <title>Test Email</title>
#     </head>
#     <body>
#         <p>Hello,</p>
#         <p>This is a test email from Django using MailTrap.</p>
#         <p>Please visit the following URL for more information:</p>
#         <p><a href="http://example.com">http://example.com</a></p>
#         <p>Thank you!</p>
#     </body>
#     </html>
#     """
#     from_email = 'noreply@yourdomain.com'
#     recipient_list = ['your-email@example.com']

#     try:
#         # Send the HTML email
#         email = EmailMessage(
#             subject,
#             html_message,
#             from_email,
#             recipient_list
#         )
#         email.content_subtype = 'html'  # Important: Set content type to HTML
#         email.send(fail_silently=False)
#         return HttpResponse('Email sent successfully')
#     except Exception as e:
#         # Display the error
#         return HttpResponse(f'Error sending email: {e}')

# import logging
# from django.contrib.auth.views import PasswordResetView

# logger = logging.getLogger(__name__)

    
# # yourapp/views.py
# from django.http import HttpResponse
# from django.core.mail import EmailMessage

# def send_test_email_view(request):
#     subject = 'Test Email with URL'
#     html_message = """
#     <!DOCTYPE html>
#     <html>
#     <head>
#         <title>Test Email</title>
#     </head>
#     <body>
#         <p>Hello,</p>
#         <p>This is a test email frddddddddddddddddddddddddddddddddddddddddddom Django using MailTrap.</p>
#         <p>Please visit the following URL for more information:</p>
#         <p><a href="http://example.com">http://example.com</a></p>
#         <p>Thank you!</p>
#     </body>
#     </html>
#     """
#     from_email = 'noreply@yourdomain.com'
#     recipient_list = ['your-email@example.com']

#     try:
#         email = EmailMessage(
#             subject,
#             html_message,
#             from_email,
#             recipient_list
#         )
#         email.content_subtype = 'html'  # Important: Set content type to HTML
#         email.send(fail_silently=False)
#         return HttpResponse('Email sent successfully')
#     except Exception as e:
#         return HttpResponse(f'Error sending email: {e}')
