"""
Django-filter Integration for Rich Choices

This module provides django-filter compatible filter classes that integrate
with the RichChoice registry system.
"""

from typing import Any

from django_filters import ChoiceFilter, MultipleChoiceFilter

from .registry import registry


class RichChoiceFilter(ChoiceFilter):
    """
    Django-filter ChoiceFilter that uses the RichChoice registry.
    
    This is the REQUIRED replacement for ChoiceFilter with inline choices.
    
    Usage:
        class MyFilterSet(django_filters.FilterSet):
            status = RichChoiceFilter(
                choice_group="ticket_statuses",
                domain="support",
            )
    """
    
    def __init__(
        self,
        choice_group: str,
        domain: str = "core",
        allow_deprecated: bool = False,
        **kwargs
    ):
        """
        Initialize the filter.
        
        Args:
            choice_group: Name of the choice group in the registry
            domain: Domain namespace for the choice group
            allow_deprecated: Whether to include deprecated choices
            **kwargs: Additional arguments passed to ChoiceFilter
        """
        self.choice_group = choice_group
        self.domain = domain
        self.allow_deprecated = allow_deprecated
        
        # Get choices from registry
        if allow_deprecated:
            choices_list = registry.get_choices(choice_group, domain)
        else:
            choices_list = registry.get_active_choices(choice_group, domain)
        
        choices = [(c.value, c.label) for c in choices_list]
        
        super().__init__(choices=choices, **kwargs)


class RichMultipleChoiceFilter(MultipleChoiceFilter):
    """
    Django-filter MultipleChoiceFilter that uses the RichChoice registry.
    
    This is the REQUIRED replacement for MultipleChoiceFilter with inline choices.
    
    Usage:
        class MyFilterSet(django_filters.FilterSet):
            statuses = RichMultipleChoiceFilter(
                choice_group="ticket_statuses",
                domain="support",
                field_name="status",
            )
    """
    
    def __init__(
        self,
        choice_group: str,
        domain: str = "core",
        allow_deprecated: bool = False,
        **kwargs
    ):
        """
        Initialize the filter.
        
        Args:
            choice_group: Name of the choice group in the registry
            domain: Domain namespace for the choice group
            allow_deprecated: Whether to include deprecated choices
            **kwargs: Additional arguments passed to MultipleChoiceFilter
        """
        self.choice_group = choice_group
        self.domain = domain
        self.allow_deprecated = allow_deprecated
        
        # Get choices from registry
        if allow_deprecated:
            choices_list = registry.get_choices(choice_group, domain)
        else:
            choices_list = registry.get_active_choices(choice_group, domain)
        
        choices = [(c.value, c.label) for c in choices_list]
        
        super().__init__(choices=choices, **kwargs)


def get_choice_filter_class(
    choice_group: str,
    domain: str = "core",
    allow_deprecated: bool = False,
    **extra_kwargs: Any
) -> type[RichChoiceFilter]:
    """
    Factory function to create a RichChoiceFilter class with preset choices.
    
    Useful when you need to define the filter class dynamically or
    when the choice_group/domain aren't available at class definition time.
    
    Usage:
        StatusFilter = get_choice_filter_class("ticket_statuses", "support")
        
        class MyFilterSet(django_filters.FilterSet):
            status = StatusFilter()
    """
    class DynamicRichChoiceFilter(RichChoiceFilter):
        def __init__(self, **kwargs):
            kwargs.setdefault("choice_group", choice_group)
            kwargs.setdefault("domain", domain)
            kwargs.setdefault("allow_deprecated", allow_deprecated)
            for key, value in extra_kwargs.items():
                kwargs.setdefault(key, value)
            super().__init__(**kwargs)
    
    return DynamicRichChoiceFilter
