from django import forms
from .models import Journee, Repas, CompositionRepas, Aliment

class JourneeForm(forms.ModelForm):
    class Meta:
        model = Journee
        fields = ['type_journee', 'notes']
        widgets = {
            'type_journee': forms.Select(attrs={'class': 'form-select'}),
            'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }
        labels = {
            'type_journee': 'Type de journée',
            'notes': 'Notes',
        }

class RepasForm(forms.ModelForm):
    class Meta:
        model = Repas
        fields = ['type_repas', 'heure', 'notes']
        widgets = {
            'type_repas': forms.Select(attrs={'class': 'form-select'}),
            'heure': forms.TimeInput(attrs={'class': 'form-control', 'type': 'time'}),
            'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 2}),
        }
        labels = {
            'type_repas': 'Type de repas',
            'heure': 'Heure',
            'notes': 'Notes',
        }

class CompositionRepasForm(forms.ModelForm):
    class Meta:
        model = CompositionRepas
        fields = ['aliment', 'nombre_portions', 'notes']
        widgets = {
            'aliment': forms.Select(attrs={'class': 'form-select'}),
            'nombre_portions': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.5', 'min': '0.5'}),
            'notes': forms.TextInput(attrs={'class': 'form-control'}),
        }
        labels = {
            'aliment': 'Aliment',
            'nombre_portions': 'Nombre de portions',
            'notes': 'Notes (préparation, remarques)',
        }
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Organiser les aliments par catégorie dans le select
        self.fields['aliment'].queryset = Aliment.objects.all().order_by('categorie', 'nom')
