"""Tests for agent tools."""

from fhir_agent_tools import get_tools, get_extractor_functions


def test_get_tools_returns_list():
    tools = get_tools()
    assert isinstance(tools, list)
    assert len(tools) > 0


def test_tool_schema_structure():
    tools = get_tools()
    for t in tools:
        assert "type" in t
        assert t["type"] == "function"
        assert "function" in t
        fn = t["function"]
        assert "name" in fn
        assert "description" in fn
        assert "parameters" in fn
        assert fn["parameters"]["type"] == "object"
        assert "properties" in fn["parameters"]


def test_extract_resources_by_type_has_resource_type_param():
    tools = get_tools()
    rbt = next(t for t in tools if t["function"]["name"] == "extract_resources_by_type")
    assert "resource_type" in rbt["function"]["parameters"]["properties"]
    assert "resource_type" in rbt["function"]["parameters"]["required"]


def test_get_extractor_functions():
    funcs = get_extractor_functions()
    assert isinstance(funcs, list)
    assert len(funcs) == len(get_tools())
    for name, fn in funcs:
        assert isinstance(name, str)
        assert callable(fn)


def test_extractor_function_invocation():
    funcs = dict(get_extractor_functions())
    patient_json = '{"resourceType":"Patient","id":"x","name":[{"family":"Smith","given":["John"]}]}'

    result = funcs["extract_patient_summary"](patient_json)
    import json
    data = json.loads(result)
    assert data["success"] is True
    assert data["data"]["name"] == "Smith, John"
