from django import forms
from .models import Truck, Shipment, Bid, Profile, Country, OTPCode, City, TruckType, AppSetting, ContactMessage
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.contrib.auth.models import User

class UserRegistrationForm(UserCreationForm):
    email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={'class': 'form-control'}))
    confirm_email = forms.EmailField(required=True, widget=forms.EmailInput(attrs={'class': 'form-control'}), label=_("Confirm Email"))
    role = forms.ChoiceField(choices=[choice for choice in Profile.ROLE_CHOICES if choice[0] != 'ADMIN'], widget=forms.Select(attrs={'class': 'form-select'}))
    country_code = forms.ChoiceField(choices=[], widget=forms.Select(attrs={'class': 'form-select'}))
    phone_number = forms.CharField(max_length=20, required=True, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '123456789'}))
    subscription_plan = forms.ChoiceField(choices=[('MONTHLY', _('Monthly')), ('ANNUAL', _('Annual'))], required=False, widget=forms.Select(attrs={'class': 'form-select'}), label=_('Subscription Plan'))
    otp_method = forms.ChoiceField(choices=[("whatsapp", _("WhatsApp")), ("email", _("Email"))], initial="whatsapp", widget=forms.RadioSelect, label=_("Receive verification code via"))
    accept_terms = forms.BooleanField(required=True, widget=forms.CheckboxInput(attrs={'class': 'form-check-input'}), label=_('I have read and agree to the Terms and Conditions and Privacy Policy'))

    class Meta(UserCreationForm.Meta):
        model = User
        fields = UserCreationForm.Meta.fields + ('email',)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Dynamic country codes from the database
        countries = Country.objects.all()
        if countries.exists():
            self.fields['country_code'].choices = [(c.code, str(c)) for c in countries]
            default_country = countries.filter(is_default=True).first()
            if default_country:
                self.fields['country_code'].initial = default_country.code
        else:
            # Fallback if no countries are defined yet
            self.fields['country_code'].choices = [('966', 'Saudi Arabia (+966)')]
            self.fields['country_code'].initial = '966'

        app_settings = AppSetting.objects.first()
        if app_settings and app_settings.subscription_enabled:
            # We will update these labels via JS based on selected role to show the correct fee
            self.fields['subscription_plan'].choices = [
                ('MONTHLY', _('Monthly Plan')),
                ('ANNUAL', _('Annual Plan')),
            ]
            self.fields['subscription_plan'].required = True
        else:
            self.fields['subscription_plan'].required = False

        for field in self.fields.values():
            if not isinstance(field.widget, (forms.CheckboxInput, forms.Select)):
                field.widget.attrs.update({'class': 'form-control'})

    def clean_email(self):
        email = self.cleaned_data.get('email')
        if User.objects.filter(email=email).exists():
            raise forms.ValidationError(_("This email is already in use."))
        return email

    def clean_phone_number(self):
        phone_number = self.cleaned_data.get('phone_number')
        country_code = self.cleaned_data.get('country_code')
        # We check uniqueness of phone_number in Profile
        if Profile.objects.filter(phone_number=phone_number).exists():
            raise forms.ValidationError(_("This phone number is already in use."))
        return phone_number

    def clean(self):
        cleaned_data = super().clean()
        email = cleaned_data.get("email")
        confirm_email = cleaned_data.get("confirm_email")

        if email and confirm_email and email != confirm_email:
            self.add_error('confirm_email', _("The two email fields didn't match."))
        
        return cleaned_data

class OTPVerifyForm(forms.Form):
    otp_code = forms.CharField(label=_("OTP Code"), max_length=6, widget=forms.TextInput(attrs={'class': 'form-control', 'placeholder': '123456'}))

class TruckForm(forms.ModelForm):
    class Meta:
        model = Truck
        fields = [
            'truck_type_link', 'model', 'model_ar', 'year', 'plate_no', 
            'load_capacity', 'load_capacity_ar', 'color', 'color_ar', 'registration_expiry_date',
            'truck_picture', 'registration_front', 'registration_back', 'driver_license_front', 'driver_license_back'
        ]
        widgets = {
            'truck_type_link': forms.Select(attrs={'class': 'form-select'}),
            'model': forms.TextInput(attrs={'class': 'form-control'}),
            'model_ar': forms.TextInput(attrs={'class': 'form-control'}),
            'year': forms.NumberInput(attrs={'class': 'form-control'}),
            'plate_no': forms.TextInput(attrs={'class': 'form-control'}),
            'load_capacity': forms.TextInput(attrs={'class': 'form-control'}),
            'load_capacity_ar': forms.TextInput(attrs={'class': 'form-control'}),
            'color': forms.TextInput(attrs={'class': 'form-control'}),
            'color_ar': forms.TextInput(attrs={'class': 'form-control'}),
            'registration_expiry_date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
            'truck_picture': forms.FileInput(attrs={'class': 'form-control'}),
            'registration_front': forms.FileInput(attrs={'class': 'form-control'}),
            'registration_back': forms.FileInput(attrs={'class': 'form-control'}),
            'driver_license_front': forms.FileInput(attrs={'class': 'form-control'}),
            'driver_license_back': forms.FileInput(attrs={'class': 'form-control'}),
        }
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['truck_type_link'].label = _("Truck Type")

class ShipmentForm(forms.ModelForm):
    class Meta:
        model = Shipment
        fields = [
            'description', 'weight', 'required_truck_type_link',
            'origin_country', 'origin_city', 
            'destination_country', 'destination_city', 
            'delivery_date'
        ]
        widgets = {
            'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'weight': forms.TextInput(attrs={'class': 'form-control'}),
            'required_truck_type_link': forms.Select(attrs={'class': 'form-select'}),
            'origin_country': forms.Select(attrs={'class': 'form-select location-selector', 'data-type': 'origin'}),
            'origin_city': forms.Select(attrs={'class': 'form-select'}),
            'destination_country': forms.Select(attrs={'class': 'form-select location-selector', 'data-type': 'destination'}),
            'destination_city': forms.Select(attrs={'class': 'form-select'}),
            'delivery_date': forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}),
        }
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['required_truck_type_link'].label = _("Truck Type")

class BidForm(forms.ModelForm):
    class Meta:
        model = Bid
        fields = ['truck', 'amount', 'comments']
        widgets = {
            'truck': forms.Select(attrs={'class': 'form-select'}),
            'amount': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'}),
            'comments': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
        }
    
    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super().__init__(*args, **kwargs)
        if user:
            # Only allow bidding with approved trucks
            self.fields['truck'].queryset = Truck.objects.filter(owner=user, is_approved=True)
            if not self.fields['truck'].queryset.exists():
                self.fields['truck'].help_text = _("You must have an approved truck to place a bid.")

class ShipperOfferForm(forms.Form):
    description = forms.CharField(label=_('Goods Description'), widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 3}))
    weight = forms.CharField(label=_('Weight/Volume'), widget=forms.TextInput(attrs={'class': 'form-control'}))
    required_truck_type_link = forms.ModelChoiceField(label=_('Required Truck Type'), queryset=TruckType.objects.all(), widget=forms.Select(attrs={'class': 'form-select'}))
    
    origin_country = forms.ModelChoiceField(queryset=Country.objects.all(), widget=forms.Select(attrs={'class': 'form-select location-selector', 'data-type': 'origin'}))
    origin_city = forms.ModelChoiceField(queryset=City.objects.all(), widget=forms.Select(attrs={'class': 'form-select'}))
    destination_country = forms.ModelChoiceField(queryset=Country.objects.all(), widget=forms.Select(attrs={'class': 'form-select location-selector', 'data-type': 'destination'}))
    destination_city = forms.ModelChoiceField(queryset=City.objects.all(), widget=forms.Select(attrs={'class': 'form-select'}))
    
    delivery_date = forms.DateField(label=_('Requested Delivery Date'), widget=forms.DateInput(attrs={'class': 'form-control', 'type': 'date'}))
    amount = forms.DecimalField(label=_('Offer Amount'), max_digits=10, decimal_places=2, widget=forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'}))
    comments = forms.CharField(label=_('Comments'), required=False, widget=forms.Textarea(attrs={'class': 'form-control', 'rows': 2}))

class RenewSubscriptionForm(forms.Form):
    subscription_plan = forms.ChoiceField(
        choices=[('MONTHLY', _('Monthly Plan')), ('ANNUAL', _('Annual Plan'))],
        widget=forms.Select(attrs={'class': 'form-select'}),
        label=_('Subscription Plan')
    )

class AppSettingForm(forms.ModelForm):
    class Meta:
        model = AppSetting
        fields = '__all__'
        widgets = {
            'app_name': forms.TextInput(attrs={'class': 'form-control'}),
            'logo': forms.FileInput(attrs={'class': 'form-control'}),
            'slogan': forms.TextInput(attrs={'class': 'form-control'}),
            'registration_number': forms.TextInput(attrs={'class': 'form-control'}),
            'tax_number': forms.TextInput(attrs={'class': 'form-control'}),
            'contact_phone': forms.TextInput(attrs={'class': 'form-control'}),
            'contact_email': forms.EmailInput(attrs={'class': 'form-control'}),
            'contact_address': forms.Textarea(attrs={'class': 'form-control', 'rows': 2}),
            'terms_of_service': forms.Textarea(attrs={'class': 'form-control', 'rows': 5}),
            'privacy_policy': forms.Textarea(attrs={'class': 'form-control', 'rows': 5}),
            'subscription_enabled': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
            'shipper_monthly_fee': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'}),
            'shipper_annual_fee': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'}),
            'truck_owner_monthly_fee': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'}),
            'truck_owner_annual_fee': forms.NumberInput(attrs={'class': 'form-control', 'step': '0.01'}),
        }

class ContactForm(forms.ModelForm):
    class Meta:
        model = ContactMessage
        fields = ['name', 'email', 'subject', 'message']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Your Name')}),
            'email': forms.EmailInput(attrs={'class': 'form-control', 'placeholder': _('Your Email')}),
            'subject': forms.TextInput(attrs={'class': 'form-control', 'placeholder': _('Subject')}),
            'message': forms.Textarea(attrs={'class': 'form-control', 'rows': 5, 'placeholder': _('Your Message')}),
        }

class CustomLoginForm(AuthenticationForm):
    otp_method = forms.ChoiceField(
        choices=[("whatsapp", _("WhatsApp")), ("email", _("Email"))],
        initial="whatsapp",
        widget=forms.RadioSelect,
        label=_("Receive verification code via")
    )
    
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        for field in self.fields.values():
            if not isinstance(field.widget, (forms.RadioSelect, forms.CheckboxInput)):
                field.widget.attrs.update({"class": "form-control"})

class ProfileForm(forms.ModelForm):
    first_name = forms.CharField(max_length=150, required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
    last_name = forms.CharField(max_length=150, required=False, widget=forms.TextInput(attrs={'class': 'form-control'}))
    email = forms.EmailField(required=False, widget=forms.EmailInput(attrs={'class': 'form-control'}))

    class Meta:
        model = Profile
        fields = ['profile_picture', 'phone_number', 'country_code', 'email_verified', 'phone_verified']
        widgets = {
            'profile_picture': forms.FileInput(attrs={'class': 'form-control'}),
            'phone_number': forms.TextInput(attrs={'class': 'form-control'}),
            'country_code': forms.Select(attrs={'class': 'form-select'}),
            'email_verified': forms.HiddenInput(),
            'phone_verified': forms.HiddenInput(),
        }

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user', None)
        super().__init__(*args, **kwargs)
        
        # Dynamic country codes from the database
        countries = Country.objects.all()
        if countries.exists():
            self.fields['country_code'].widget = forms.Select(
                attrs={'class': 'form-select'},
                choices=[(c.code, str(c)) for c in countries]
            )

        if user:
            self.fields['first_name'].initial = user.first_name
            self.fields['last_name'].initial = user.last_name
            self.fields['email'].initial = user.email

    def clean(self):
        cleaned_data = super().clean()
        email_verified = cleaned_data.get('email_verified')
        phone_verified = cleaned_data.get('phone_verified')
        
        if not email_verified and not phone_verified:
            raise forms.ValidationError(_("At least one contact method (Email or Phone) must be verified."))
        return cleaned_data

    def save(self, commit=True):
        profile = super().save(commit=False)
        user = profile.user
        user.first_name = self.cleaned_data['first_name']
        user.last_name = self.cleaned_data['last_name']
        user.email = self.cleaned_data['email']
        if commit:
            user.save()
            profile.save()
        return profile