from django import forms

from .models import ContactMessage
from .partner_content import (
    ORGANISATION_TYPE_CHOICES,
    PARTNERSHIP_AREA_CHOICES,
    PARTNERSHIP_AREA_LABELS,
)


class PartnerInquiryForm(forms.Form):
    full_name = forms.CharField(
        max_length=200,
        label='Full Name',
        widget=forms.TextInput(attrs={'placeholder': 'Your full name'}),
    )
    organisation_name = forms.CharField(
        max_length=150,
        label='Organisation Name',
        widget=forms.TextInput(attrs={'placeholder': 'Enter your organisation name'}),
    )
    position = forms.CharField(
        max_length=120,
        label='Position / Job Title',
        widget=forms.TextInput(attrs={'placeholder': 'Your role or job title'}),
    )
    email = forms.EmailField(
        label='Email Address',
        widget=forms.EmailInput(attrs={'placeholder': 'you@organisation.com'}),
    )
    phone = forms.CharField(
        max_length=30,
        label='Phone Number',
        widget=forms.TextInput(attrs={'placeholder': '+260 97 000 0000'}),
    )
    organisation_type = forms.ChoiceField(
        label='Organisation Type',
        choices=ORGANISATION_TYPE_CHOICES,
    )
    partnership_areas = forms.MultipleChoiceField(
        label='Partnership Area',
        choices=PARTNERSHIP_AREA_CHOICES,
        widget=forms.CheckboxSelectMultiple,
    )
    message = forms.CharField(
        label='Tell Us About Your Partnership Idea',
        widget=forms.Textarea(attrs={
            'placeholder': (
                'Share your objectives, audience, and the type of partnership you would like to explore…'
            ),
            'rows': 6,
        }),
    )
    website = forms.CharField(
        max_length=255,
        required=False,
        label='Website / Social Media Link (Optional)',
        widget=forms.URLInput(attrs={'placeholder': 'https://'}),
    )

    def clean_organisation_type(self):
        org_type = self.cleaned_data.get('organisation_type')
        if not org_type:
            raise forms.ValidationError('Please select your organisation type.')
        return org_type

    def clean_partnership_areas(self):
        areas = self.cleaned_data.get('partnership_areas') or []
        if not areas:
            raise forms.ValidationError('Please select at least one partnership area.')
        return areas

    def save(self):
        full_name = self.cleaned_data['full_name'].strip()
        parts = full_name.split(None, 1)
        first_name = parts[0]
        last_name = parts[1] if len(parts) > 1 else ''
        area_labels = [
            PARTNERSHIP_AREA_LABELS.get(key, key)
            for key in self.cleaned_data['partnership_areas']
        ]
        return ContactMessage.objects.create(
            first_name=first_name,
            last_name=last_name,
            email=self.cleaned_data['email'],
            phone=self.cleaned_data.get('phone', ''),
            company=self.cleaned_data['organisation_name'],
            position=self.cleaned_data['position'],
            organisation_type=self.cleaned_data['organisation_type'],
            partnership_areas=', '.join(area_labels),
            website=self.cleaned_data.get('website', ''),
            sender_role=ContactMessage.Role.EMPLOYER,
            subject=ContactMessage.Subject.PARTNERSHIP,
            message=self.cleaned_data['message'],
        )


class ContactForm(forms.ModelForm):
    class Meta:
        model = ContactMessage
        fields = [
            'first_name',
            'last_name',
            'email',
            'phone',
            'sender_role',
            'subject',
            'message',
        ]
        widgets = {
            'first_name': forms.TextInput(attrs={'placeholder': 'Chanda', 'required': True}),
            'last_name': forms.TextInput(attrs={'placeholder': 'Mwila', 'required': True}),
            'email': forms.EmailInput(attrs={'placeholder': 'you@email.zm', 'required': True}),
            'phone': forms.TextInput(attrs={'placeholder': '+260 97 000 0000'}),
            'message': forms.Textarea(
                attrs={'placeholder': 'Tell us how we can help you…', 'required': True}
            ),
        }
