from __future__ import annotations

from django import forms


class MultiFileInput(forms.ClearableFileInput):
    allow_multiple_selected = True


class MultiFileField(forms.Field):
    widget = MultiFileInput

    def __init__(self, *args, **kwargs):
        kwargs.setdefault("required", False)
        super().__init__(*args, **kwargs)

    def to_python(self, data):
        # Accept a list of UploadedFile or an empty list/None
        return data

    def validate(self, value):
        # Only enforce presence if required=True
        if self.required and not value:
            raise forms.ValidationError("This field is required.")


class PickupRequestForm(forms.Form):

    name = forms.CharField(max_length=255)
    email = forms.EmailField(required=False)
    phone = forms.CharField(max_length=64, required=False)
    address = forms.CharField(widget=forms.Textarea)
    preferred_at = forms.DateTimeField(required=False, widget=forms.DateTimeInput(attrs={"type": "datetime-local"}))
    materials = forms.CharField(label="Materials/Notes", widget=forms.Textarea, required=False)
    photos = MultiFileField(widget=MultiFileInput, help_text="Optional: upload photos of scrap")


class ContactForm(forms.Form):
    name = forms.CharField(max_length=255)
    email = forms.EmailField()
    phone = forms.CharField(max_length=64, required=False)
    subject = forms.CharField(max_length=255, required=False)
    message = forms.CharField(widget=forms.Textarea)
