from __future__ import annotations

from app.modules.rag.contracts.enums import RagLayer
from app.modules.rag.intent_router_v2.models import RepoContext


class CodeQaRepoContextFactory:
    _KNOWN_LAYERS = [
        RagLayer.CODE_ENTRYPOINTS,
        RagLayer.CODE_SYMBOL_CATALOG,
        RagLayer.CODE_DEPENDENCY_GRAPH,
        RagLayer.CODE_SEMANTIC_ROLES,
        RagLayer.CODE_SOURCE_CHUNKS,
        RagLayer.DOCS_MODULE_CATALOG,
        RagLayer.DOCS_FACT_INDEX,
        RagLayer.DOCS_SECTION_INDEX,
        RagLayer.DOCS_POLICY_INDEX,
    ]

    def build(self, files_map: dict[str, dict] | None = None) -> RepoContext:
        languages = self._languages(files_map or {})
        return RepoContext(
            languages=languages or ["python"],
            available_domains=["CODE", "DOCS"],
            available_layers=list(self._KNOWN_LAYERS),
        )

    def _languages(self, files_map: dict[str, dict]) -> list[str]:
        found: list[str] = []
        for path in files_map:
            lower = path.lower()
            if lower.endswith(".py") and "python" not in found:
                found.append("python")
            if lower.endswith((".md", ".rst", ".txt")) and "markdown" not in found:
                found.append("markdown")
        return found
