from django.db.models import Q
from django.db.models.functions import TruncDate


class DynamicSearchService:
    def __init__(self, queryset, query_string: str = "", search_fields: list[str] = None,
                 start: str = None, end: str = None, date_field: str = "create_date"):
        self.queryset = queryset
        self.query = (query_string or "").strip()
        self.fields = search_fields or []
        self.start = start
        self.end = end
        self.date_field = date_field

    def apply(self):
        qs = self.queryset

        if self.query and self.fields:
            keywords = [k.strip() for k in self.query.split(",") if k.strip()]
            q_obj = Q()
            for kw in keywords:
                for field in self.fields:
                    q_obj |= Q(**{f"{field}__icontains": kw})  # noqa
            qs = qs.filter(q_obj).distinct()

        if self.start or self.end:
            qs = qs.annotate(date_only=TruncDate(self.date_field))
            if self.start:
                try:
                    qs = qs.filter(date_only__gte=self.start)
                except ValueError:
                    pass  # or log

            if self.end:
                try:
                    qs = qs.filter(date_only__lte=self.end)
                except ValueError:
                    pass  # or log

        return qs
