from django import forms
from .models import Company, JobStatus, RequiredFolder, Job, JobFile, Client

class CompanyForm(forms.ModelForm):
    class Meta:
        model = Company
        fields = ['name', 'is_uprn_required']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'e.g. Acme Repairs Ltd'}),
            'is_uprn_required': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
        }

class JobStatusForm(forms.ModelForm):
    class Meta:
        model = JobStatus
        fields = ['name', 'is_starting_status']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Status Name'}),
            'is_starting_status': forms.CheckboxInput(attrs={'class': 'form-check-input'}),
        }

class RequiredFolderForm(forms.ModelForm):
    class Meta:
        model = RequiredFolder
        fields = ['name']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Folder Name (e.g. Photos)'}),
        }

class ClientForm(forms.ModelForm):
    class Meta:
        model = Client
        fields = ['name', 'client_job_ref_prefix']
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Client Name'}),
            'client_job_ref_prefix': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'Client Job Ref Prefix (Optional)'}),
        }

class JobForm(forms.ModelForm):
    class Meta:
        model = Job
        fields = ['job_ref', 'client', 'uprn', 'address_line_1', 'address_line_2', 'address_line_3', 'postcode', 'description', 'action', 'notes', 'status']
        widgets = {
            'job_ref': forms.TextInput(attrs={'class': 'form-control'}),
            'client': forms.Select(attrs={'class': 'form-control'}),
            'uprn': forms.TextInput(attrs={'class': 'form-control'}),
            'address_line_1': forms.TextInput(attrs={'class': 'form-control'}),
            'address_line_2': forms.TextInput(attrs={'class': 'form-control'}),
            'address_line_3': forms.TextInput(attrs={'class': 'form-control'}),
            'postcode': forms.TextInput(attrs={'class': 'form-control'}),
            'description': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'action': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'notes': forms.Textarea(attrs={'class': 'form-control', 'rows': 3}),
            'status': forms.Select(attrs={'class': 'form-control'}),
        }

    def __init__(self, *args, **kwargs):
        company = kwargs.pop('company', None)
        super().__init__(*args, **kwargs)
        if company:
            self.fields['status'].queryset = JobStatus.objects.filter(company=company)
            self.fields['client'].queryset = Client.objects.filter(company=company)
            
            # Set initial status to starting status if creating new job
            if not self.instance.pk:
                starting_status = JobStatus.objects.filter(company=company, is_starting_status=True).first()
                if starting_status:
                    self.fields['status'].initial = starting_status
            
            if company.is_uprn_required:
                self.fields['uprn'].required = True
            else:
                self.fields['uprn'].required = False

    def clean_uprn(self):
        uprn = self.cleaned_data.get('uprn')
        if not uprn:
            return None
        return uprn

class JobFileForm(forms.ModelForm):
    class Meta:
        model = JobFile
        fields = ['file']
        widgets = {
            'file': forms.ClearableFileInput(attrs={'class': 'form-control'}),
        }

class ImportJobsForm(forms.Form):
    file = forms.FileField(label="Excel/CSV File", widget=forms.ClearableFileInput(attrs={'class': 'form-control'}))


class InviteUserForm(forms.Form):
    email = forms.EmailField(label="User Email", widget=forms.EmailInput(attrs={'class': 'form-control', 'placeholder': 'email@example.com'}))