from __future__ import annotations

from pathlib import PurePosixPath

from app.modules.rag.indexing.common.path_filter import should_skip_indexing_path


class DocsFileFilter:
    _EXTENSIONS = {".md", ".rst", ".txt", ".adoc"}
    _NAMES = ("README", "CHANGELOG", "CONTRIBUTING", "ARCHITECTURE", "SPEC", "RUNBOOK")
    _EXCLUDE_PARTS = {"node_modules", ".git", "vendor", "dist", "build", "target", "__pycache__"}

    def should_index(self, path: str) -> bool:
        if should_skip_indexing_path(path):
            return False
        candidate = PurePosixPath(path)
        if any(part in self._EXCLUDE_PARTS for part in candidate.parts):
            return False
        if candidate.suffix.lower() in self._EXTENSIONS:
            return True
        upper_name = candidate.name.upper()
        if any(upper_name.startswith(prefix) for prefix in self._NAMES):
            return True
        joined = "/".join(candidate.parts).lower()
        return any(marker in joined for marker in ("docs/", "doc/", "documentation/"))
