from django import forms
from django.contrib.auth.forms import AuthenticationForm
from pathlib import Path


class PortalLoginForm(AuthenticationForm):
    username = forms.EmailField(
        label="Email Address",
        widget=forms.EmailInput(attrs={
            'class': 'form-control',
            'placeholder': 'you@company.com',
            'autocomplete': 'email',
        })
    )
    password = forms.CharField(
        label="Password",
        widget=forms.PasswordInput(attrs={
            'class': 'form-control',
            'placeholder': 'Your password',
            'autocomplete': 'current-password',
        })
    )


class SignDocumentForm(forms.Form):
    signature_name = forms.CharField(
        max_length=200,
        min_length=3,
        widget=forms.TextInput(attrs={
            'class': 'sign-name-input',
            'placeholder': 'Type your full name to sign',
            'id': 'sign-name-input',
        })
    )
    agree = forms.BooleanField(required=True)


class AssetUploadForm(forms.Form):
    file = forms.FileField()

    def clean_file(self):
        file_obj = self.cleaned_data.get('file')
        if not file_obj:
            return file_obj
        max_bytes = 10 * 1024 * 1024
        allowed_extensions = {'.pdf', '.png', '.jpg', '.jpeg', '.webp', '.doc', '.docx', '.xlsx', '.zip'}
        ext = Path(file_obj.name).suffix.lower()
        if ext not in allowed_extensions:
            raise forms.ValidationError(f'Unsupported file type "{ext or "unknown"}".')
        if file_obj.size > max_bytes:
            raise forms.ValidationError('File must be 10MB or smaller.')
        return file_obj
