from fastapi.testclient import TestClient

from tests.conftest import TRUSTED_HEADERS


def test_upload_chunks_returns_counts(monkeypatch):
    from app.main import app
    from app.models.upload import ChunkUploadCounts, ChunkUploadResponse
    from app.services import upload_service as upload_module

    async def _fake_upload(body, ctx):
        return ChunkUploadResponse(
            repo_id=body.repo_id,
            counts=ChunkUploadCounts(indexed=1, skipped=0, failed=0),
            request_id=ctx.request_id,
        )

    monkeypatch.setattr(upload_module.upload_service, "upload_chunks", _fake_upload)

    client = TestClient(app)
    response = client.post(
        "/api/v1/indexing/chunks",
        headers=TRUSTED_HEADERS,
        json={
            "repo_id": "lucos:ws:abc123",
            "chunks": [
                {
                    "chunk_id": "chk_1",
                    "chunk_hash": "sha256:aaa",
                    "file_path": "src/main.ts",
                    "start_line": 1,
                    "end_line": 5,
                    "content": "export {}",
                    "source_type": "repo_code",
                }
            ],
        },
    )

    assert response.status_code == 200
    body = response.json()
    assert body["repo_id"] == "lucos:ws:abc123"
    assert body["counts"]["indexed"] == 1
    assert body["request_id"] == "req-test"
