from django import forms
from django.contrib.auth.forms import AuthenticationForm, SetPasswordForm, PasswordChangeForm
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Submit, Field, Div, HTML


class CustomLoginForm(AuthenticationForm):
    username = forms.CharField(
        label='Email Address',
        widget=forms.TextInput(attrs={'placeholder': 'Enter your email', 'autofocus': True}),
    )
    password = forms.CharField(
        label='Password',
        widget=forms.PasswordInput(attrs={'placeholder': 'Enter your password'}),
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.form_class = 'needs-validation'
        self.helper.layout = Layout(
            Field('username', css_class='form-control form-control-lg'),
            Field('password', css_class='form-control form-control-lg'),
            Div(
                HTML('<a href="{% url \'accounts:password_reset\' %}" class="text-decoration-none small">Forgot password?</a>'),
                css_class='text-end mb-3',
            ),
            Submit('submit', 'Login to Portal', css_class='btn btn-primary w-100 btn-lg'),
        )


class CustomSetPasswordForm(SetPasswordForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Field('new_password1', css_class='form-control'),
            Field('new_password2', css_class='form-control'),
            Submit('submit', 'Set New Password', css_class='btn btn-primary w-100'),
        )


class CustomPasswordChangeForm(PasswordChangeForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        self.helper.layout = Layout(
            Field('old_password', css_class='form-control'),
            Field('new_password1', css_class='form-control'),
            Field('new_password2', css_class='form-control'),
            Submit('submit', 'Update Password', css_class='btn btn-primary mt-2'),
        )
