from email.utils import formataddr
from django.utils.translation import gettext_lazy as _
from django.conf import settings
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework.throttling import AnonRateThrottle
from ..drf.views import Endpoint
from ..settings import saas_settings
from ..models import UserEmail
from ..security import check_security_rules
from ..serializers.password import (
    PasswordForgetSerializer,
    PasswordResetSerializer,
)
from .._notification import send_mail


class PasswordForgotEndpoint(Endpoint):
    email_template_id = "reset_password"
    email_subject = _("Password Reset Request")

    permission_classes = []
    throttle_classes = [AnonRateThrottle]
    serializer_class = PasswordForgetSerializer

    def post(self, request: Request):
        """Send a forgot password reset email code."""
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        obj: UserEmail = serializer.save()

        # check bad request rules
        check_security_rules(saas_settings.RESET_PASSWORD_SECURITY_RULES, request)

        code = serializer.save_password_code(obj)
        name = obj.user.get_full_name() or obj.user.get_username()
        send_mail(
            self.__class__,
            self.email_subject,
            self.email_template_id,
            recipients=[formataddr((name, obj.email))],
            code=code,
            user=obj.user,
        )
        return Response(status=204)


class PasswordResetEndpoint(Endpoint):
    permission_classes = []
    throttle_classes = [AnonRateThrottle]
    serializer_class = PasswordResetSerializer

    def post(self, request: Request):
        """Reset password of a user with the given code."""
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response({"next": settings.LOGIN_URL})
