from django.contrib.messages.views import SuccessMessageMixin
from django.http import JsonResponse
from django.urls import path, include
from .views import registration,user_login,user_logout, verify_otp, resend_otp, profile
from django.contrib.auth import views as auth_views


class CustomPasswordChangeView(SuccessMessageMixin, auth_views.PasswordChangeView):
 template_name = "main.html"
 success_message = "Your password has been successfully changed!"
 success_url = "/user/change-password/done"

 def form_valid(self, form):
  """Handles successful password change via AJAX."""
  response = super().form_valid(form)
  return JsonResponse({"success": True, "message": self.success_message})


 def form_invalid(self, form):
  """Handles errors when password change fails via AJAX."""
  return JsonResponse({"success": False, "errors": form.errors}, status=400)

urlpatterns = [
 path('registration', registration,name="register"),
 path('login', user_login,name="login"),
 path('logout', user_logout,name="logout"),
 path('mail/verify', verify_otp,name="verify_otp"),
 path('mail/re-send', resend_otp,name="resend_otp"),

 path("password-reset/", auth_views.PasswordResetView.as_view(
  template_name="password_reset.html", email_template_name="password_reset_email.html",html_email_template_name="password_reset_email.html"), name="password_reset"),
 path("password-reset/done/", auth_views.PasswordResetDoneView.as_view(
  template_name="password_reset_done.html"), name="password_reset_done"),
 path("password-reset-confirm/<uidb64>/<token>/", auth_views.PasswordResetConfirmView.as_view(
  template_name="password_reset_confirm.html"), name="password_reset_confirm"),
 path("password-reset-complete/", auth_views.PasswordResetCompleteView.as_view(
  template_name="password_reset_complete.html"), name="password_reset_complete"),

 path("change-password", CustomPasswordChangeView.as_view(), name="change_password"),
 path("change-password/done", auth_views.PasswordChangeDoneView.as_view(template_name="change_password_done.html"),
      name="password_change_done"),
 path('profile',profile, name='profile'),
]
