from django.contrib.auth.tokens import default_token_generator
from templated_mail.mail import BaseEmailMessage
from djoser import utils
from djoser.conf import settings
import os

# From djoser 

class ActivationEmail(BaseEmailMessage):
    template_name = "emails/activation.html"

    def get_context_data(self):
        # ActivationEmail can be deleted
        context = super().get_context_data()
        user = context.get("user")
        context["uid"] = utils.encode_uid(user.pk)
        context["token"] = default_token_generator.make_token(user)
        context["url"] = settings.ACTIVATION_URL.format(**context)
        context["next_url"] = os.environ.get("FRONTEND_URL")
        return context


class ConfirmationEmail(BaseEmailMessage):
    template_name = "emails/confirmation.html"


class PasswordResetEmail(BaseEmailMessage):
    template_name = "emails/password_reset.html"

    def get_context_data(self):
        # PasswordResetEmail can be deleted
        context = super().get_context_data()
        if hasattr(self, "usr"):
            user = self.usr
        else:
            user = context.get("user")
        context["uid"] = utils.encode_uid(user.pk)
        context["token"] = default_token_generator.make_token(user)
        context["url"] = settings.PASSWORD_RESET_CONFIRM_URL.format(**context)
        context["next_url"] = os.environ.get("FRONTEND_URL")
        return context


class PasswordChangedConfirmationEmail(BaseEmailMessage):
    template_name = "emails/password_changed_confirmation.html"


#class UsernameChangedConfirmationEmail(BaseEmailMessage):
#    template_name = "email/username_changed_confirmation.html"
#
#
class UsernameResetEmail(BaseEmailMessage):
    template_name = "email/username_reset.html"

    def get_context_data(self):
        context = super().get_context_data()

        user = context.get("user")
        context["uid"] = utils.encode_uid(user.pk)
        context["token"] = default_token_generator.make_token(user)
        context["url"] = settings.USERNAME_RESET_CONFIRM_URL.format(**context)
        context["next_url"] = os.environ.get("FRONTEND_URL")
        return context