from django import forms
from .models import ClientType, NormalizedInvoice


class ExtractInvoiceUploadForm(forms.Form):
    """Formulaire : société, client, mode de paiement, type client, fichier PDF/image."""
    societe = forms.ModelChoiceField(
        queryset=None,
        required=True,
        label="Société (émettrice)",
        widget=forms.Select(attrs={"class": "form-select"}),
    )
    client_type = forms.ModelChoiceField(
        queryset=None,
        required=True,
        label="Type client",
        widget=forms.Select(attrs={"class": "form-select"}),
    )
    payment_mode = forms.CharField(
        max_length=40,
        required=True,
        label="Mode de paiement",
        widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "Ex: Espèces, Virement, Chèque..."}),
    )
    client_nom = forms.CharField(
        max_length=200,
        required=True,
        label="Client - Nom / Raison sociale",
        widget=forms.TextInput(attrs={"class": "form-control"}),
    )
    client_nif = forms.CharField(
        max_length=30,
        required=False,
        label="Client - NIF",
        widget=forms.TextInput(attrs={"class": "form-control", "placeholder": "NIF"}),
    )
    client_telephone = forms.CharField(
        max_length=30,
        required=False,
        label="Client - Téléphone",
        widget=forms.TextInput(attrs={"class": "form-control"}),
    )
    client_adresse = forms.CharField(
        required=False,
        label="Client - Adresse",
        widget=forms.Textarea(attrs={"class": "form-control", "rows": 2}),
    )
    file = forms.FileField(
        required=True,
        label="Fichier facture (PDF ou image)",
        widget=forms.FileInput(attrs={"class": "form-control", "accept": ".pdf,.png,.jpg,.jpeg,.gif,.bmp,.tiff"}),
        help_text="PDF avec texte sélectionnable ou image (JPG, PNG…) pour OCR.",
    )

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        from normalisee.models import Societe
        self.fields["societe"].queryset = Societe.objects.all().order_by("nom")
        self.fields["client_type"].queryset = ClientType.objects.all()
