from .models import PatientServiceItem, Service, ServiceCategory
from django import forms
from .models import PatientService
from django.contrib.auth.forms import AuthenticationForm
from userauth.models import User
from doctor.models import Doctor, Technician


class LoginForm(AuthenticationForm):
    username = forms.CharField(widget=forms.TextInput(attrs={
        'class': 'form-control',
        'placeholder': 'Username'
    }))
    password = forms.CharField(widget=forms.PasswordInput(attrs={
        'class': 'form-control',
        'placeholder': '********'
    }))

    class Meta:
        model = User
        fields = ['username', 'password']


class ServiceCategoryCreateForm(forms.ModelForm):
    class Meta:
        model = ServiceCategory
        fields = ['name', 'description']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control'}),
            'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }


class ServiceCreateForm(forms.ModelForm):
    status = forms.ChoiceField(choices=Service.STATUS_CHOICES, required=False, widget=forms.Select(attrs={
        'class': 'form-control',
    }))

    class Meta:
        model = Service
        fields = ['name', 'unit_price', 'status', 'description', 'service_category']

        widgets = {
            'name': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Service Name',
            }),
            'unit_price': forms.TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Unit Price',
                'type': 'number',
            }),
            'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'service_category': forms.Select(attrs={'class': 'form-control'}),

        }
        labels = {
            'name': 'Service Name',
            'unit_price': 'Unit price',
        }


class AddPatientServiceForm(forms.ModelForm):
    doctor = forms.ModelChoiceField(
        queryset=Doctor.objects.all(),
        widget=forms.Select(attrs={'class': 'form-select', 'required': True}),
        empty_label="Select Doctor"
    )
    technician = forms.ModelChoiceField(
        queryset=Technician.objects.all(),
        widget=forms.Select(attrs={'class': 'form-select', 'required': True}),
        empty_label="Select Technician"
    )
    services = forms.ModelMultipleChoiceField(
        queryset=Service.objects.all(),
        widget=forms.CheckboxSelectMultiple(attrs={'class': 'form-check-input'}),
        required=True
    )
    discount = forms.DecimalField(
        min_value=0, max_value=100,
        widget=forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'})
    )
    additional_notes = forms.CharField(
        widget=forms.Textarea(attrs={'class': 'form-control', 'placeholder': 'Additional Notes', 'rows': 3}),
        required=False
    )

    class Meta:
        model = PatientService
        fields = ['doctor', 'technician', 'services', 'discount', 'additional_notes']

    def clean_discount(self):
        discount = self.cleaned_data.get('discount')
        if discount is not None and (discount < 0 or discount > 100):
            raise forms.ValidationError("Discount must be between 0 and 100.")
        return discount

    def clean(self):
        cleaned_data = super().clean()
        services = cleaned_data.get('services')

        if not services:
            raise forms.ValidationError("At least one service must be selected.")

        return cleaned_data


class PatientServiceItemForm(forms.ModelForm):
    class Meta:
        model = PatientServiceItem
        fields = ['service', 'number_of_units']
        widgets = {
            'service': forms.Select(attrs={'class': 'form-select'}),
            'number_of_units': forms.NumberInput(attrs={'class': 'form-control', 'min': 1}),
        }

    def clean(self):
        cleaned_data = super().clean()
        service = cleaned_data.get('service')
        number_of_units = cleaned_data.get('number_of_units')

        if number_of_units and number_of_units < 1:
            self.add_error('number_of_units',
                           'Number of units must be at least 1.')

        return cleaned_data
