def test_health(client):
    r = client.get("/api/health")
    assert r.status_code == 200


def test_create_garden(client):
    r = client.post("/api/gardens", json={"nom": "Mon potager", "type": "plein_air"})
    assert r.status_code == 201
    data = r.json()
    assert data["nom"] == "Mon potager"
    assert data["id"] is not None


def test_list_gardens(client):
    client.post("/api/gardens", json={"nom": "Jardin 1", "type": "plein_air"})
    client.post("/api/gardens", json={"nom": "Jardin 2", "type": "serre"})
    r = client.get("/api/gardens")
    assert r.status_code == 200
    assert len(r.json()) == 2


def test_get_garden(client):
    r = client.post("/api/gardens", json={"nom": "Potager", "type": "plein_air"})
    id = r.json()["id"]
    r2 = client.get(f"/api/gardens/{id}")
    assert r2.status_code == 200
    assert r2.json()["nom"] == "Potager"


def test_update_garden(client):
    r = client.post("/api/gardens", json={"nom": "Vieux nom", "type": "plein_air"})
    id = r.json()["id"]
    r2 = client.put(f"/api/gardens/{id}", json={"nom": "Nouveau nom", "type": "serre"})
    assert r2.status_code == 200
    assert r2.json()["nom"] == "Nouveau nom"


def test_delete_garden(client):
    r = client.post("/api/gardens", json={"nom": "À supprimer", "type": "plein_air"})
    id = r.json()["id"]
    r2 = client.delete(f"/api/gardens/{id}")
    assert r2.status_code == 204
    r3 = client.get(f"/api/gardens/{id}")
    assert r3.status_code == 404


def test_create_measurement(client):
    r = client.post("/api/gardens", json={"nom": "Potager", "type": "plein_air"})
    id = r.json()["id"]
    r2 = client.post(
        f"/api/gardens/{id}/measurements",
        json={"temp_air": 22.5, "humidite_air": 60.0},
    )
    assert r2.status_code == 201
