def test_member_insights_returns_static_object(client):
    response = client.get("/api/member-insights")
    assert response.status_code == 200
    body = response.json()
    assert body["result"]["member"]["name"] == "Charlie Smith"
    assert body["result"]["member"]["plan"] == "Gold PPO"
    assert len(body["result"]["overdue_procedures"]) == 4


def test_schedule_returns_text_wrapped(client):
    response = client.get("/api/schedule")
    assert response.status_code == 200
    body = response.json()
    assert "agendar una cita" in body["result"].lower()
    assert "1-800-FIT-CARE" in body["result"]


def test_historical_procedures_no_filter_returns_all(client):
    response = client.post(
        "/api/historical-procedures",
        json={"filters": "[]", "group_by": "[]"},
    )
    assert response.status_code == 200
    assert len(response.json()["result"]) == 50


def test_historical_procedures_filter_equals(client):
    response = client.post(
        "/api/historical-procedures",
        json={
            "filters": '[{"column": "member_name", "operator": "equals", "value": "Charlie Smith"}]',
            "group_by": "[]",
        },
    )
    rows = response.json()["result"]
    assert len(rows) == 5
    assert all(r["member_name"] == "Charlie Smith" for r in rows)


def test_historical_procedures_filter_contains(client):
    response = client.post(
        "/api/historical-procedures",
        json={
            "filters": '[{"column": "procedure", "operator": "contains", "value": "mri"}]',
            "group_by": "[]",
        },
    )
    rows = response.json()["result"]
    assert len(rows) > 0
    assert all("MRI" in r["procedure"] for r in rows)


def test_historical_procedures_filter_gt(client):
    response = client.post(
        "/api/historical-procedures",
        json={
            "filters": '[{"column": "total_cost", "operator": "gt", "value": 5000}]',
            "group_by": "[]",
        },
    )
    rows = response.json()["result"]
    assert len(rows) > 0
    assert all(r["total_cost"] > 5000 for r in rows)


def test_available_procedures_no_filter(client):
    response = client.post(
        "/api/available-procedures",
        json={"filters": "[]", "group_by": "[]"},
    )
    rows = response.json()["result"]
    assert len(rows) == 28


def test_available_procedures_filter_mri(client):
    response = client.post(
        "/api/available-procedures",
        json={
            "filters": '[{"column": "procedure", "operator": "contains", "value": "MRI"}]',
            "group_by": "[]",
        },
    )
    rows = response.json()["result"]
    assert len(rows) >= 3
    assert all(r["procedure"] == "MRI" for r in rows)
