use serde_json::Value;

fn assert_envelope(value: Value, field: &str) {
    assert!(
        value.get("available").and_then(Value::as_bool).is_some(),
        "missing boolean availability in {value}"
    );
    assert!(
        value.get("backend").and_then(Value::as_str).is_some(),
        "missing backend in {value}"
    );
    assert!(
        value.get(field).is_some(),
        "missing payload field {field} in {value}"
    );
}

#[test]
fn apple_capability_surfaces_return_json_envelopes() {
    let cases: [(&str, Result<Value, String>); 8] = [
        ("accounts", car_ffi_common::integrations::notes_accounts()),
        ("notes", car_ffi_common::integrations::notes_find("", 1)),
        ("lists", car_ffi_common::integrations::reminders_lists()),
        (
            "reminders",
            car_ffi_common::integrations::reminders_items(1),
        ),
        ("albums", car_ffi_common::integrations::photos_albums()),
        ("bookmarks", car_ffi_common::integrations::bookmarks_list(1)),
        ("locations", car_ffi_common::integrations::files_locations()),
        ("", car_ffi_common::integrations::keychain_status()),
    ];

    for (field, result) in cases {
        let value = result.expect("surface should return a structured JSON value");
        if field.is_empty() {
            assert!(
                value.get("available").and_then(Value::as_bool).is_some(),
                "missing boolean availability in {value}"
            );
            assert!(
                value.get("backend").and_then(Value::as_str).is_some(),
                "missing backend in {value}"
            );
        } else {
            assert_envelope(value, field);
        }
    }
}
