from __future__ import annotations

from app.core.agent.processes.v2.intent_router.models import QueryFeatures
from app.core.agent.utils.process_v2.models import V2Subintent


class DocsSubintentResolver:
    _API_ENUM_MARKERS = (
        "какие api",
        "какие эндпоинты",
        "какие endpoint",
        "список api",
        "список эндпоинтов",
        "список endpoint",
        "все api",
        "все эндпоинты",
        "перечисли api",
        "перечисли эндпоинты",
        "доступные api",
        "available endpoints",
        "exposed api",
    )
    _API_WORD_MARKERS = ("api", "эндпоинт", "endpoint", "роут", "route", "метод")
    _LIST_WORD_MARKERS = ("какие", "список", "перечисли", "все", "доступные", "list", "available", "exposed")

    def resolve(self, features: QueryFeatures) -> str | None:
        if features.file_markers or self._has_file_like_anchor(features):
            return V2Subintent.FIND_FILES
        if self._is_api_exposed_request(features):
            return V2Subintent.API_EXPOSED
        if any(
            (
                features.endpoint_paths,
                features.endpoint_markers,
                features.architecture_markers,
                features.logic_markers,
                features.domain_markers,
                features.target_doc_hints,
            )
        ):
            return V2Subintent.SUMMARY
        return None

    def _has_file_like_anchor(self, features: QueryFeatures) -> bool:
        return any(
            hint.endswith((".md", ".yaml", ".yml", ".json"))
            for hint in features.target_doc_hints
        ) or any(token.endswith((".md", ".yaml", ".yml", ".json")) for token in features.file_names)

    def _is_api_exposed_request(self, features: QueryFeatures) -> bool:
        query = features.normalized_query.lower()
        if features.endpoint_paths:
            return False
        if any(marker in query for marker in self._API_ENUM_MARKERS):
            return True
        has_api_words = any(marker in query for marker in self._API_WORD_MARKERS)
        has_list_words = any(marker in query for marker in self._LIST_WORD_MARKERS)
        return has_api_words and has_list_words
