"""Tests for Bundle extractors."""

import pytest

from fhir_agent_tools import get_bundle_entries, get_resources_by_type, get_bundle_total


BUNDLE_JSON = '''
{
  "resourceType": "Bundle",
  "type": "searchset",
  "total": 2,
  "entry": [
    {
      "resource": {
        "resourceType": "Patient",
        "id": "p1",
        "name": [{"family": "A", "given": ["Alice"]}]
      }
    },
    {
      "resource": {
        "resourceType": "Observation",
        "id": "o1",
        "status": "final",
        "code": {"text": "Test"}
      }
    }
  ]
}
'''


def test_get_bundle_entries():
    result = get_bundle_entries(BUNDLE_JSON)
    assert result["success"] is True
    assert len(result["data"]) == 2
    assert result["data"][0]["resourceType"] == "Patient"
    assert result["data"][0]["id"] == "p1"
    assert result["data"][1]["resourceType"] == "Observation"


def test_get_resources_by_type():
    result = get_resources_by_type(BUNDLE_JSON, "Patient")
    assert result["success"] is True
    assert len(result["data"]) == 1
    assert result["data"][0]["resourceType"] == "Patient"

    result2 = get_resources_by_type(BUNDLE_JSON, "Observation")
    assert result2["success"] is True
    assert len(result2["data"]) == 1
    assert result2["data"][0]["resourceType"] == "Observation"


def test_get_bundle_total():
    result = get_bundle_total(BUNDLE_JSON)
    assert result["success"] is True
    assert result["data"] == 2


def test_get_bundle_empty_input():
    result = get_bundle_entries("")
    assert result["success"] is False
    assert result["error"]["type"] == "empty_input"
