from allauth.account.adapter import DefaultAccountAdapter
from django.contrib.auth.tokens import default_token_generator
from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
from django.conf import settings

from django.utils.http import urlsafe_base64_encode
from django.utils.encoding import force_bytes




class CustomAccountAdapter(DefaultAccountAdapter):
    def get_email_confirmation_url(self, request, emailconfirmation):
        """
        Constructs the email confirmation (activation) url.
        """
        url = f"{settings.FRONTEND_URL}/confirm-email/{emailconfirmation.key}/"
        return url
        
    def send_confirmation_mail(self, request, emailconfirmation, signup):
        """
        Sends the confirmation email to the user.
        """
        current_site = settings.FRONTEND_URL
        activate_url = self.get_email_confirmation_url(request, emailconfirmation)
        print("active url  ", activate_url)
        ctx = {
            "user": emailconfirmation.email_address.user,
            "activate_url": activate_url,
            "current_site": current_site,
            "key": emailconfirmation.key,
        }
        self.send_mail("account/email/email_confirmation", emailconfirmation.email_address.email, ctx)
        

    def get_password_reset_url(self, user):
        """
        Constructs the password reset URL.
        """
        uid = urlsafe_base64_encode(force_bytes(user.pk))  # Encodage en Base64
        token = default_token_generator.make_token(user)
        return f"{settings.FRONTEND_URL}/reset-password/confirm/{uid}/{token}/"
        
    def send_mail(self, template_prefix, email, context):
        """
        Sends an email to the specified email address using the provided template.
        """
        context['site_name'] = 'DeltaX'
        context['domain'] = settings.FRONTEND_URL.replace('http://', '').replace('https://', '')
        
        # Fix: Use self.get_password_reset_url instead of get_password_reset_url
        if 'user' in context and template_prefix == 'account/email/password_reset_key':
            context['password_reset_url'] = self.get_password_reset_url(context['user'])
            
        return super().send_mail(template_prefix, email, context)

class CustomSocialAccountAdapter(DefaultSocialAccountAdapter):
    def pre_social_login(self, request, sociallogin):
        """
        Invoked just after a user successfully authenticates via a social provider,
        but before the login is actually processed.
        """
        print("SOCIAL LOGIN")
        # Activate the user immediately since they're coming from a trusted provider
        if sociallogin.is_existing:
            return
        
        # If the user is new, activate them immediately
        user = sociallogin.user
        user.is_active = True
        
        # If you want to add a specific role for social users
        if hasattr(user, 'role'):
            from users.models import User
            user.role = User.VISITOR