from __future__ import annotations

import asyncio

from app.modules.rag.services.rag_service import RagService


class _FakeEmbedder:
    def embed(self, texts: list[str]) -> list[list[float]]:
        return [[0.0, 0.1, 0.2] for _ in texts]


class _FakeRepository:
    def __init__(self) -> None:
        self.replaced_docs = []

    def get_session(self, rag_session_id: str) -> dict:
        return {"project_id": rag_session_id}

    def get_cached_documents(self, repo_id: str, blob_sha: str) -> list:
        return []

    def cache_documents(self, repo_id: str, path: str, blob_sha: str, docs: list) -> None:
        return None

    def replace_documents(self, rag_session_id: str, docs: list) -> None:
        self.replaced_docs = docs


def test_rag_service_progress_uses_only_indexable_files() -> None:
    repository = _FakeRepository()
    service = RagService(embedder=_FakeEmbedder(), repository=repository)
    files = [
        {"path": ".env", "content": "SECRET=1", "content_hash": "h1"},
        {"path": "src/.hidden/config.py", "content": "A=1", "content_hash": "h2"},
        {"path": "src/__pycache__/cache.py", "content": "A=2", "content_hash": "h3"},
        {"path": "src/main.py", "content": "def main():\n    return 1\n", "content_hash": "h4"},
    ]
    progress: list[tuple[int, int, str]] = []

    def progress_cb(current: int, total: int, path: str) -> None:
        progress.append((current, total, path))

    indexed, failed, cache_hits, cache_misses = asyncio.run(
        service.index_snapshot("project-1", files, progress_cb=progress_cb)
    )

    assert indexed == 1
    assert failed == 0
    assert cache_hits == 0
    assert cache_misses == 1
    assert progress == [(1, 1, "src/main.py")]
