from django.contrib.auth.forms import UserCreationForm, UserChangeForm, PasswordResetForm
from django import forms
from .models import CustomUser
import re  # Import the re module for regular expressions
from .utils.licence_validator import LicenseValidator
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.contrib.auth.tokens import default_token_generator
from django.contrib.sites.shortcuts import get_current_site
from django.utils.encoding import force_bytes

class CustomUserCreationForm(UserCreationForm):
    usable_password = None

    class Meta:
        model = CustomUser
        error_messages = {
            'email': {
                'unique': "Cette adresse email est déjà utilisée.",
            },
            'username': {
                'unique': "Ce nom d'utilisateur est déjà pris.",
            },
            'licence_id': {
                'unique': "Cette licence est déjà utilisée par un autre compte.",
            },

        }

        fields = UserCreationForm.Meta.fields + ('umpire_code', 'clubs', 'phone', 'first_name', 'last_name', 'licence_id', 'country')

class SimpleCustomUserCreationForm(UserCreationForm):
    usable_password = None

    class Meta:
        model = CustomUser
        fields = UserCreationForm.Meta.fields + ('email', 'phone', 'first_name', 'last_name', 'licence_id', 'country')
        error_messages = {
            'email': {
                'unique': "Cette adresse email est déjà utilisée.",
            },
            'username': {
                'unique': "Ce nom d'utilisateur est déjà pris.",
            },
            'licence_id': {
                'unique': "Cette licence est déjà utilisée par un autre compte.",
            },

        }
        labels = {
            'username': 'Nom d’utilisateur',
            'email': 'E-mail',
            'phone': 'Numéro de téléphone',
            'first_name': 'Prénom',
            'last_name': 'Nom de famille',
            'licence_id': 'Numéro de licence',
            'country': 'Pays',
            'password1': 'Mot de passe',
            'password2': 'Confirmer le mot de passe',
        }

    def clean_username(self):
        username = self.cleaned_data.get('username')
        if username:
            username = username.lower()
            if CustomUser.objects.filter(username__iexact=username).exists() | CustomUser.objects.filter(email__iexact=username).exists():
                raise forms.ValidationError("Cet identifiant est déjà utilisé. Veuillez en choisir un autre :)")
        return username


class CustomUserChangeForm(UserChangeForm):

    class Meta:
        model = CustomUser
        error_messages = {
            'email': {
                'unique': "Cette adresse email est déjà utilisée.",
            },
            'username': {
                'unique': "Ce nom d'utilisateur est déjà pris.",
            },
            'licence_id': {
                'unique': "Cette licence est déjà utilisée par un autre compte.",
            },

        }
        fields = UserCreationForm.Meta.fields + ('umpire_code', 'clubs', 'phone', 'first_name', 'last_name', 'licence_id', 'country')

class SimpleForm(forms.Form):
    # A single field for user input (e.g., a text field)
    name = forms.CharField(label='Enter your name!', max_length=100)

class TournamentRegistrationForm(forms.Form):
    #first_name = forms.CharField(label='Prénom', max_length=50)
    #last_name = forms.CharField(label='Nom', max_length=50)
    email = forms.EmailField(label='E-mail', widget=forms.EmailInput(attrs={'readonly': 'readonly'}))
    mobile_number = forms.CharField(
        label='Téléphone',
        max_length=15,
        required=True
    )

    def clean_mobile_number(self):
        mobile_number = self.cleaned_data.get('mobile_number')
        if mobile_number:
            # Basic regex for mobile numbers, matching common formats
            # Remove spaces from the number first
            mobile_number = mobile_number.replace(' ', '')
            if not re.match(r"^\+?\d{10,15}$", mobile_number):
                raise forms.ValidationError("Entrer un numéro de téléphone valide.")
        return mobile_number

class AddPlayerForm(forms.Form):
    licence_id = forms.CharField(label='Numéro de licence (avec la lettre)', max_length=20, required=False)
    first_name = forms.CharField(label='Prénom', min_length=2, max_length=50, required=False)
    last_name = forms.CharField(label='Nom', min_length=2, max_length=50, required=False)
    first_tournament = False
    user_without_licence = False

    def names_is_valid(self):
        first_name = self.cleaned_data.get('first_name')
        last_name = self.cleaned_data.get('last_name')
        return len(first_name) >= 2 and len(last_name) >= 2

    def clean_licence_id(self):
        licence_id = self.cleaned_data.get('licence_id')

        # Convert to uppercase
        licence_id = licence_id.upper()

        # Update the cleaned_data with the modified licence_id
        self.cleaned_data['licence_id'] = licence_id

        # Optionally, print the cleaned license ID for debugging
        print(f"Cleaned Licence ID (inside clean_licence_id): {licence_id}")

        return licence_id

    def clean_last_name(self):
        last_name = self.cleaned_data.get('last_name')

        # Convert to uppercase
        last_name = last_name.upper()

        # Update the cleaned_data with the modified licence_id
        self.cleaned_data['last_name'] = last_name
        return last_name

    def clean_first_name(self):
        first_name = self.cleaned_data.get('first_name')

        # Convert to capitalize
        first_name = first_name.capitalize()

        # Update the cleaned_data with the modified licence_id
        self.cleaned_data['first_name'] = first_name
        return first_name

class CustomPasswordResetForm(PasswordResetForm):
    def save(self, *args, **kwargs):
        """
        Override the save method to send a custom email.
        """
        email = self.cleaned_data["email"]
        users = self.get_users(email)

        for user in users:
            # Generate the token for password reset
            token = default_token_generator.make_token(user)
            uid = urlsafe_base64_encode(force_bytes(user.pk))

            # Prepare the context for the email template
            context = {
                "email": user.email,
                "domain": get_current_site(self.request).domain,
                "site_name": "Padel Club",
                "uid": uid,
                "token": token,
                "protocol": "http",  # Use 'https' in production
            }

            # Render the email content from the template
            subject = "Réinitialisation du mot de passe"
            message = render_to_string("registration/password_reset_email.html", context)

            # Send the email
            send_mail(subject, message, None, [user.email])

class ProfileUpdateForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Remove autofocus from the 'username' field
        self.fields['username'].widget.attrs.pop("autofocus", None)

    class Meta:
        model = CustomUser
        fields = ['first_name', 'last_name', 'licence_id', 'username', 'email', 'phone']
        labels = {
            'username': 'Nom d’utilisateur',
            'email': 'E-mail',
            'phone': 'Numéro de téléphone',
            'first_name': 'Prénom',
            'last_name': 'Nom de famille',
            'licence_id': 'Numéro de licence',
        }
        error_messages = {
            'email': {
                'unique': "Cette adresse email est déjà utilisée.",
            },
            'username': {
                'unique': "Ce nom d'utilisateur est déjà pris.",
            },
            'licence_id': {
                'unique': "Cette licence est déjà utilisée par un autre compte.",
            },
        }

from django.contrib.auth.forms import PasswordChangeForm

class CustomPasswordChangeForm(PasswordChangeForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Remove autofocus from all fields in the PasswordChangeForm
        for field in self.fields.values():
            field.widget.attrs.pop("autofocus", None)

from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import authenticate  # Add this import
from django import forms
import logging

logger = logging.getLogger(__name__)

class EmailOrUsernameAuthenticationForm(AuthenticationForm):
    username = forms.CharField(label='Username or Email')

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        print(f"Login attempt with username/email: {username}")  # Debug print
        logger.info(f"Login attempt with username/email: {username}")

        if username and password:
            self.user_cache = authenticate(
                self.request,
                username=username,
                password=password
            )

            print(f"Authentication result: {self.user_cache}")  # Debug print
            logger.info(f"Authentication result: {self.user_cache}")

            if self.user_cache is None:
                print("Authentication failed")  # Debug print
                logger.warning("Authentication failed")
                raise forms.ValidationError(
                    "Identifiant/E-mail ou mot de passe incorrect. Les champs sont sensibles à la casse.",
                    code='invalid_login'
                )
            else:
                print(f"Authentication successful for user: {self.user_cache}")  # Debug print
                logger.info(f"Authentication successful for user: {self.user_cache}")
                self.confirm_login_allowed(self.user_cache)

        return self.cleaned_data
