from django import forms

from servala.core.models import CloudProvider, ControlPlane, ServiceCategory


class ServiceFilterForm(forms.Form):
    category = forms.ModelChoiceField(
        queryset=ServiceCategory.objects.all(), required=False
    )
    cloud_provider = forms.ModelChoiceField(
        queryset=CloudProvider.objects.all(), required=False
    )
    q = forms.CharField(required=False)

    def filter_queryset(self, queryset):
        if category := self.cleaned_data.get("category"):
            queryset = queryset.filter(category=category)
        if cloud_provider := self.cleaned_data.get("cloud_provider"):
            queryset = queryset.filter(
                offerings__control_planes__cloud_provider=cloud_provider
            )
        return queryset


class ControlPlaneSelectForm(forms.Form):
    control_plane = forms.ModelChoiceField(queryset=ControlPlane.objects.none())

    def __init__(self, *args, planes=None, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields["control_plane"].queryset = planes
