from __future__ import annotations

import pytest

from app.intent.repo_classifier import RepoClassifier, RepoClassifierResult
from app.models.repo_resolution import RepoCandidate


@pytest.mark.asyncio
async def test_repo_classifier_parses_valid_selection():
    class FakeLLM:
        async def complete_json(self, messages, model=None):
            return {
                "selected_repo_id": "ad/commit-intel.com",
                "confidence": 0.9,
            }

    from app.generation.llm_client import LLMClient

    llm = LLMClient(client=object())  # type: ignore[arg-type]
    llm.complete_json = FakeLLM().complete_json  # type: ignore[method-assign]

    classifier = RepoClassifier(llm_client=llm)
    result = await classifier.classify(
        "Where is ProcessDelta in commit intel?",
        [
            RepoCandidate(repo_id="ad/commit-intel.com", label="commit-intel.com"),
            RepoCandidate(repo_id="ad/code-parser.com", label="code-parser.com"),
        ],
    )
    assert result is not None
    assert result.selected_repo_id == "ad/commit-intel.com"
    assert result.confidence == 0.9


@pytest.mark.asyncio
async def test_repo_classifier_rejects_unknown_repo_id():
    class FakeLLM:
        async def complete_json(self, messages, model=None):
            return {
                "selected_repo_id": "ad/unknown.com",
                "confidence": 0.95,
            }

    from app.generation.llm_client import LLMClient

    llm = LLMClient(client=object())  # type: ignore[arg-type]
    llm.complete_json = FakeLLM().complete_json  # type: ignore[method-assign]

    classifier = RepoClassifier(llm_client=llm)
    result = await classifier.classify(
        "test?",
        [RepoCandidate(repo_id="ad/commit-intel.com", label="commit-intel.com")],
    )
    assert result is not None
    assert result.selected_repo_id is None
