from __future__ import annotations

from hashlib import sha256

from app.modules.rag.indexing.code.entrypoints.registry import Entrypoint


class TyperClickEntrypointDetector:
    def detect(self, *, path: str, symbols: list) -> list[Entrypoint]:
        items: list[Entrypoint] = []
        for symbol in symbols:
            for decorator in symbol.decorators or []:
                lowered = decorator.lower()
                if ".command" not in lowered and ".callback" not in lowered:
                    continue
                framework = "typer" if "typer" in lowered else "click"
                items.append(
                    Entrypoint(
                        entry_id=sha256(f"{path}|{framework}|{symbol.symbol_id}|{decorator}".encode("utf-8")).hexdigest(),
                        entry_type="cli",
                        framework=framework,
                        route_or_command=decorator,
                        handler_symbol_id=symbol.symbol_id,
                        path=path,
                        start_line=symbol.start_line,
                        end_line=symbol.end_line,
                    )
                )
        return items
