# -*- coding: utf-8 -*-
from __future__ import annotations

import uuid

from django.db import models

from hotpocket_backend.apps.saves.models import Save
from hotpocket_common.constants import AssociationsSearchMode
from hotpocket_soa.dto.associations import AssociationsQuery


class SaveAdapter:
    def get_for_processing(self, *, pk: uuid.UUID) -> Save | None:
        return Save.active_objects.get(pk=pk)


class AssociationAdapter:
    def get_search_term_filters(self, *, query: AssociationsQuery) -> list[models.Q]:
        # I suck at naming things, LOL.
        result = [
            (
                models.Q(target__url__icontains=query.search)
                |
                models.Q(target_title__icontains=query.search)
                |
                models.Q(target__title__icontains=query.search)
                |
                models.Q(target_description__icontains=query.search)
                |
                models.Q(target__description__icontains=query.search)
            ),
        ]

        return result

    def get_search_filters(self, *, query: AssociationsQuery) -> list[models.Q]:
        result = [
            models.Q(account_uuid=query.account_uuid),
            models.Q(target__deleted_at__isnull=True),
        ]

        if query.mode == AssociationsSearchMode.ARCHIVED:
            result.append(models.Q(archived_at__isnull=False))
        else:
            result.append(models.Q(archived_at__isnull=True))

        match query.mode:
            case AssociationsSearchMode.STARRED:
                result.append(models.Q(starred_at__isnull=False))

        if query.before is not None:
            result.append(models.Q(pk__lt=query.before))

        if query.after is not None:
            result.append(models.Q(pk__gte=query.after))

        if query.search is not None:
            result.extend(self.get_search_term_filters(query=query))

        return result
