import json

import httpx
import respx
from fastmcp import Client

from tests.conftest import (
    SAMPLE_S2_CITATIONS,
    SAMPLE_S2_REFERENCES,
    make_test_server,
)

S2_BASE = "https://api.semanticscholar.org/graph/v1"


@respx.mock
async def test_get_citations():
    respx.get(f"{S2_BASE}/paper/DOI:10.1038/nature12373/citations").mock(
        return_value=httpx.Response(200, json=SAMPLE_S2_CITATIONS)
    )

    async with Client(make_test_server()) as client:
        result = await client.call_tool("get_citations", {"doi": "10.1038/nature12373"})
        data = json.loads(result.content[0].text)
        assert data["count"] == 1
        assert "BERT" in data["citations"][0]["title"]
        assert data["citations"][0]["is_influential"] is True


@respx.mock
async def test_get_references():
    respx.get(f"{S2_BASE}/paper/DOI:10.1038/nature12373/references").mock(
        return_value=httpx.Response(200, json=SAMPLE_S2_REFERENCES)
    )

    async with Client(make_test_server()) as client:
        result = await client.call_tool("get_references", {"doi": "10.1038/nature12373"})
        data = json.loads(result.content[0].text)
        assert data["count"] == 1
        assert "Neural Machine Translation" in data["references"][0]["title"]


@respx.mock
async def test_get_citations_empty():
    respx.get(f"{S2_BASE}/paper/DOI:10.0000/new/citations").mock(
        return_value=httpx.Response(200, json={"data": []})
    )

    async with Client(make_test_server()) as client:
        result = await client.call_tool("get_citations", {"doi": "10.0000/new"})
        data = json.loads(result.content[0].text)
        assert data["citations"] == []
