File size: 24,081 Bytes
3374e90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
//! Pure C ABI for the Bex WASM Plugin Engine.
//!
//! This module exports `extern "C"` functions that match the declarations in
//! `bex_engine.h`. The architecture is callback-driven:
//!
//! 1. C++ calls `bex_submit_search(engine, plugin_id, query, callback, user_data)`
//! 2. Rust spawns a Tokio task that does the work
//! 3. On completion, Rust invokes `callback(user_data, request_id, success, payload, len)`
//!    from the Tokio background thread
//! 4. C++ receives the result and can parse/copy it before the callback returns
//!
//! There is NO event queue, NO polling, NO cxx dependency.
//! This is a clean, high-performance Pure C ABI boundary.

use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_void};
use std::path::PathBuf;
use std::sync::atomic::{AtomicU64, Ordering};


use bex_core::EngineConfig;
use bex_types::BexError;

use crate::runtime::BexRuntime;

// ── Opaque Engine Handle ──────────────────────────────────────────────

/// The internal representation behind the opaque `BexEngine*` pointer.
pub struct BexEngineInner {
    runtime: BexRuntime,
    next_request_id: AtomicU64,
    /// Stores the last error message from a sync operation.
    last_error: parking_lot::Mutex<Option<CString>>,
}

// ── Callback type matching bex_engine.h ───────────────────────────────

type ResultCallback = unsafe extern "C" fn(
    user_data: *mut c_void,
    request_id: u64,
    success: bool,
    payload: *const u8,
    payload_len: usize,
);

// ── FFI-visible structs ───────────────────────────────────────────────

/// FFI-visible BexPluginInfo struct β€” must match the C header exactly.
#[repr(C)]
pub struct BexPluginInfo {
    pub id: *mut c_char,
    pub name: *mut c_char,
    pub version: *mut c_char,
    pub capabilities: u32,
    pub enabled: bool,
    pub description: *mut c_char,
    pub author: *mut c_char,
    pub homepage: *mut c_char,
}

/// FFI-visible BexPluginInfoList β€” must match the C header exactly.
#[repr(C)]
pub struct BexPluginInfoList {
    pub items: *mut BexPluginInfo,
    pub count: usize,
}

// ── Helper functions ──────────────────────────────────────────────────

fn set_last_error(inner: &BexEngineInner, msg: &str) {
    if let Ok(c) = CString::new(msg) {
        *inner.last_error.lock() = Some(c);
    }
}

fn clear_last_error(inner: &BexEngineInner) {
    *inner.last_error.lock() = None;
}

fn error_to_code(inner: &BexEngineInner, e: &BexError) -> i32 {
    let msg = e.to_string();
    set_last_error(inner, &msg);
    match e {
        BexError::PluginNotFound(_) => 2,
        BexError::PluginDisabled(_) => 3,
        BexError::NotReady => 4,
        BexError::Storage(_) => 5,
        BexError::Internal(_) => 6,
        _ => -1,
    }
}

fn str_to_cstring(s: &str) -> *mut c_char {
    CString::new(s)
        .map(|c| c.into_raw())
        .unwrap_or(std::ptr::null_mut())
}

fn plugin_info_to_ffi(info: &bex_types::plugin_info::PluginInfo) -> BexPluginInfo {
    BexPluginInfo {
        id: str_to_cstring(&info.id),
        name: str_to_cstring(&info.name),
        version: str_to_cstring(&info.version),
        capabilities: info.capabilities,
        enabled: info.enabled,
        description: str_to_cstring(""),
        author: str_to_cstring(""),
        homepage: str_to_cstring(""),
    }
}

fn error_code_short(err: &BexError) -> &'static str {
    match err {
        BexError::AbiMismatch { .. } => "ABI_MISMATCH",
        BexError::ManifestInvalid(_) => "INVALID_MANIFEST",
        BexError::HashMismatch { .. } => "HASH_MISMATCH",
        BexError::PluginNotFound(_) => "NOT_FOUND",
        BexError::PluginDisabled(_) => "DISABLED",
        BexError::Unsupported(_) => "UNSUPPORTED",
        BexError::NetworkBlocked(_) => "NETWORK_BLOCKED",
        BexError::Timeout { .. } => "TIMEOUT",
        BexError::FuelExhausted => "FUEL_EXHAUSTED",
        BexError::Cancelled => "CANCELLED",
        BexError::PluginFault(_) => "PLUGIN_FAULT",
        BexError::PluginError(_) => "PLUGIN_ERROR",
        BexError::Network(_) => "NETWORK",
        BexError::Storage(_) => "STORAGE",
        BexError::NotReady => "NOT_READY",
        BexError::Internal(_) => "INTERNAL",
        _ => "UNKNOWN",
    }
}

/// Helper to convert a C string pointer to a Rust String.
/// Returns None if the pointer is null or invalid UTF-8.
unsafe fn cstr_to_string(ptr: *const c_char) -> Option<String> {
    if ptr.is_null() {
        return None;
    }
    CStr::from_ptr(ptr).to_str().ok().map(|s| s.to_string())
}

// ══════════════════════════════════════════════════════════════════════
// FFI-exported functions β€” must match bex_engine.h exactly
// ══════════════════════════════════════════════════════════════════════

// ── Lifecycle ─────────────────────────────────────────────────────────

#[no_mangle]
pub unsafe extern "C" fn bex_engine_new(data_dir: *const c_char) -> *mut BexEngineInner {
    if data_dir.is_null() {
        return std::ptr::null_mut();
    }

    let data_dir_str = match cstr_to_string(data_dir) {
        Some(s) => s,
        None => return std::ptr::null_mut(),
    };

    let config = EngineConfig {
        data_dir: PathBuf::from(data_dir_str),
        ..Default::default()
    };

    match BexRuntime::new(config) {
        Ok(runtime) => {
            let inner = Box::new(BexEngineInner {
                runtime,
                next_request_id: AtomicU64::new(1),
                last_error: parking_lot::Mutex::new(None),
            });
            Box::into_raw(inner)
        }
        Err(e) => {
            tracing::error!("Failed to create BexEngine: {}", e);
            std::ptr::null_mut()
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_engine_free(engine: *mut BexEngineInner) {
    if engine.is_null() {
        return;
    }
    let inner = Box::from_raw(engine);
    inner.runtime.shutdown();
}

// ── Plugin Management (synchronous) ──────────────────────────────────

#[no_mangle]
pub unsafe extern "C" fn bex_engine_install(
    engine: *mut BexEngineInner,
    path: *const c_char,
) -> i32 {
    if engine.is_null() || path.is_null() {
        return -1;
    }
    let inner = &*engine;
    clear_last_error(inner);

    let path_str = match cstr_to_string(path) {
        Some(s) => s,
        None => {
            set_last_error(inner, "Invalid UTF-8 in path");
            return -1;
        }
    };

    match inner.runtime.install_plugin(std::path::Path::new(&path_str)) {
        Ok(_) => 0,
        Err(e) => error_to_code(inner, &e),
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_engine_uninstall(
    engine: *mut BexEngineInner,
    id: *const c_char,
) -> i32 {
    if engine.is_null() || id.is_null() {
        return -1;
    }
    let inner = &*engine;
    clear_last_error(inner);

    let id_str = match cstr_to_string(id) {
        Some(s) => s,
        None => {
            set_last_error(inner, "Invalid UTF-8 in id");
            return -1;
        }
    };

    match inner.runtime.uninstall_plugin(&id_str) {
        Ok(_) => 0,
        Err(e) => error_to_code(inner, &e),
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_engine_list_plugins(
    engine: *mut BexEngineInner,
) -> BexPluginInfoList {
    if engine.is_null() {
        return BexPluginInfoList {
            items: std::ptr::null_mut(),
            count: 0,
        };
    }
    let inner = &*engine;
    let plugins = inner.runtime.list_plugins();
    let count = plugins.len();

    if count == 0 {
        return BexPluginInfoList {
            items: std::ptr::null_mut(),
            count: 0,
        };
    }

    // Allocate array of BexPluginInfo
    let layout = std::alloc::Layout::array::<BexPluginInfo>(count).unwrap();
    let items_ptr = std::alloc::alloc(layout) as *mut BexPluginInfo;
    if items_ptr.is_null() {
        return BexPluginInfoList {
            items: std::ptr::null_mut(),
            count: 0,
        };
    }

    for (i, info) in plugins.iter().enumerate() {
        let ffi_info = plugin_info_to_ffi(info);
        std::ptr::write(items_ptr.add(i), ffi_info);
    }

    BexPluginInfoList { items: items_ptr, count }
}

#[no_mangle]
pub unsafe extern "C" fn bex_engine_plugin_info(
    engine: *mut BexEngineInner,
    id: *const c_char,
    out: *mut BexPluginInfo,
) -> i32 {
    if engine.is_null() || id.is_null() || out.is_null() {
        return -1;
    }
    let inner = &*engine;
    clear_last_error(inner);

    let id_str = match cstr_to_string(id) {
        Some(s) => s,
        None => {
            set_last_error(inner, "Invalid UTF-8 in id");
            return -1;
        }
    };

    match inner.runtime.get_plugin_info(&id_str) {
        Some(info) => {
            std::ptr::write(out, plugin_info_to_ffi(&info));
            0
        }
        None => {
            set_last_error(inner, &format!("Plugin not found: {}", id_str));
            2
        }
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_engine_enable(
    engine: *mut BexEngineInner,
    id: *const c_char,
) -> i32 {
    if engine.is_null() || id.is_null() {
        return -1;
    }
    let inner = &*engine;
    clear_last_error(inner);

    let id_str = match cstr_to_string(id) {
        Some(s) => s,
        None => return -1,
    };

    match inner.runtime.enable_plugin(&id_str) {
        Ok(_) => 0,
        Err(e) => error_to_code(inner, &e),
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_engine_disable(
    engine: *mut BexEngineInner,
    id: *const c_char,
) -> i32 {
    if engine.is_null() || id.is_null() {
        return -1;
    }
    let inner = &*engine;
    clear_last_error(inner);

    let id_str = match cstr_to_string(id) {
        Some(s) => s,
        None => return -1,
    };

    match inner.runtime.disable_plugin(&id_str) {
        Ok(_) => 0,
        Err(e) => error_to_code(inner, &e),
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_plugin_info_list_free(list: BexPluginInfoList) {
    if list.items.is_null() || list.count == 0 {
        return;
    }

    for i in 0..list.count {
        let info = &*list.items.add(i);
        if !info.id.is_null() { let _ = CString::from_raw(info.id); }
        if !info.name.is_null() { let _ = CString::from_raw(info.name); }
        if !info.version.is_null() { let _ = CString::from_raw(info.version); }
        if !info.description.is_null() { let _ = CString::from_raw(info.description); }
        if !info.author.is_null() { let _ = CString::from_raw(info.author); }
        if !info.homepage.is_null() { let _ = CString::from_raw(info.homepage); }
    }

    let layout = std::alloc::Layout::array::<BexPluginInfo>(list.count).unwrap();
    std::alloc::dealloc(list.items as *mut u8, layout);
}

#[no_mangle]
pub unsafe extern "C" fn bex_plugin_info_free(info: BexPluginInfo) {
    if !info.id.is_null() { let _ = CString::from_raw(info.id); }
    if !info.name.is_null() { let _ = CString::from_raw(info.name); }
    if !info.version.is_null() { let _ = CString::from_raw(info.version); }
    if !info.description.is_null() { let _ = CString::from_raw(info.description); }
    if !info.author.is_null() { let _ = CString::from_raw(info.author); }
    if !info.homepage.is_null() { let _ = CString::from_raw(info.homepage); }
}

// ── API Key / Secret Management (synchronous) ────────────────────────

#[no_mangle]
pub unsafe extern "C" fn bex_engine_secret_set(
    engine: *mut BexEngineInner,
    plugin_id: *const c_char,
    key: *const c_char,
    value: *const c_char,
) -> i32 {
    if engine.is_null() || plugin_id.is_null() || key.is_null() || value.is_null() {
        return -1;
    }
    let inner = &*engine;
    clear_last_error(inner);

    let pid = match cstr_to_string(plugin_id) { Some(s) => s, None => return -1 };
    let k = match cstr_to_string(key) { Some(s) => s, None => return -1 };
    let v = match cstr_to_string(value) { Some(s) => s, None => return -1 };

    match inner.runtime.secret_set(&pid, &k, &v) {
        Ok(_) => 0,
        Err(e) => error_to_code(inner, &e),
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_engine_secret_get(
    engine: *mut BexEngineInner,
    plugin_id: *const c_char,
    key: *const c_char,
    out_buf: *mut c_char,
    out_buf_len: *mut usize,
) -> i32 {
    if engine.is_null() || plugin_id.is_null() || key.is_null()
        || out_buf.is_null() || out_buf_len.is_null()
    {
        return -1;
    }
    let inner = &*engine;
    clear_last_error(inner);

    let pid = match cstr_to_string(plugin_id) { Some(s) => s, None => return -1 };
    let k = match cstr_to_string(key) { Some(s) => s, None => return -1 };

    match inner.runtime.secret_get(&pid, &k) {
        Ok(Some(val)) => {
            let buf_size = *out_buf_len;
            let val_bytes = val.as_bytes();
            let copy_len = val_bytes.len().min(buf_size - 1);
            if copy_len < val_bytes.len() {
                set_last_error(inner, "Output buffer too small");
                *out_buf_len = val_bytes.len() + 1;
                return -2;
            }
            std::ptr::copy_nonoverlapping(val_bytes.as_ptr(), out_buf as *mut u8, copy_len);
            *out_buf.add(copy_len) = 0;
            *out_buf_len = copy_len;
            0
        }
        Ok(None) => {
            set_last_error(inner, &format!("Secret '{}' not found for plugin '{}'", k, pid));
            1
        }
        Err(e) => error_to_code(inner, &e),
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_engine_secret_delete(
    engine: *mut BexEngineInner,
    plugin_id: *const c_char,
    key: *const c_char,
) -> i32 {
    if engine.is_null() || plugin_id.is_null() || key.is_null() {
        return -1;
    }
    let inner = &*engine;
    clear_last_error(inner);

    let pid = match cstr_to_string(plugin_id) { Some(s) => s, None => return -1 };
    let k = match cstr_to_string(key) { Some(s) => s, None => return -1 };

    match inner.runtime.secret_remove(&pid, &k) {
        Ok(true) => 0,
        Ok(false) => 1,
        Err(e) => error_to_code(inner, &e),
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_engine_secret_keys(
    engine: *mut BexEngineInner,
    plugin_id: *const c_char,
) -> *mut c_char {
    if engine.is_null() || plugin_id.is_null() {
        return std::ptr::null_mut();
    }
    let inner = &*engine;

    let pid = match cstr_to_string(plugin_id) {
        Some(s) => s,
        None => return std::ptr::null_mut(),
    };

    match inner.runtime.secret_keys(&pid) {
        Ok(keys) => {
            let joined = keys.join(",");
            str_to_cstring(&joined)
        }
        Err(_) => std::ptr::null_mut(),
    }
}

#[no_mangle]
pub unsafe extern "C" fn bex_string_free(s: *mut c_char) {
    if !s.is_null() {
        let _ = CString::from_raw(s);
    }
}

// ── Async Operations ─────────────────────────────────────────────────
//
// Each submit function:
// 1. Converts all C strings to owned Rust Strings BEFORE creating the closure
// 2. Generates a request_id
// 3. Spawns a Tokio task that executes the work and invokes the callback
//
// The closure captures only owned types (String, Arc<Engine>, etc.) β€”
// no raw pointers, making it Send-safe.

#[no_mangle]
pub unsafe extern "C" fn bex_submit_search(
    engine: *mut BexEngineInner,
    plugin_id: *const c_char,
    query: *const c_char,
    callback: ResultCallback,
    user_data: *mut c_void,
) -> u64 {
    let pid = match cstr_to_string(plugin_id) { Some(s) => s, None => return 0 };
    let query_str = match cstr_to_string(query) { Some(s) => s, None => return 0 };

    submit_async(engine, pid, callback, user_data, move |engine, pid| {
        engine.call_search_json(pid, &query_str)
            .map(|s| s.into_bytes())
    })
}

#[no_mangle]
pub unsafe extern "C" fn bex_submit_home(
    engine: *mut BexEngineInner,
    plugin_id: *const c_char,
    callback: ResultCallback,
    user_data: *mut c_void,
) -> u64 {
    let pid = match cstr_to_string(plugin_id) { Some(s) => s, None => return 0 };

    submit_async(engine, pid, callback, user_data, move |engine, pid| {
        engine.call_get_home_json(pid)
            .map(|s| s.into_bytes())
    })
}

#[no_mangle]
pub unsafe extern "C" fn bex_submit_info(
    engine: *mut BexEngineInner,
    plugin_id: *const c_char,
    media_id: *const c_char,
    callback: ResultCallback,
    user_data: *mut c_void,
) -> u64 {
    let pid = match cstr_to_string(plugin_id) { Some(s) => s, None => return 0 };
    let mid = match cstr_to_string(media_id) { Some(s) => s, None => return 0 };

    submit_async(engine, pid, callback, user_data, move |engine, pid| {
        engine.call_get_info_json(pid, &mid)
            .map(|s| s.into_bytes())
    })
}

#[no_mangle]
pub unsafe extern "C" fn bex_submit_servers(
    engine: *mut BexEngineInner,
    plugin_id: *const c_char,
    id: *const c_char,
    callback: ResultCallback,
    user_data: *mut c_void,
) -> u64 {
    let pid = match cstr_to_string(plugin_id) { Some(s) => s, None => return 0 };
    let id_str = match cstr_to_string(id) { Some(s) => s, None => return 0 };

    submit_async(engine, pid, callback, user_data, move |engine, pid| {
        engine.call_get_servers_json(pid, &id_str)
            .map(|s| s.into_bytes())
    })
}

#[no_mangle]
pub unsafe extern "C" fn bex_submit_stream(
    engine: *mut BexEngineInner,
    plugin_id: *const c_char,
    server_json: *const c_char,
    callback: ResultCallback,
    user_data: *mut c_void,
) -> u64 {
    let pid = match cstr_to_string(plugin_id) { Some(s) => s, None => return 0 };
    let server_str = match cstr_to_string(server_json) { Some(s) => s, None => return 0 };

    submit_async(engine, pid, callback, user_data, move |engine, pid| {
        engine.call_resolve_stream_json(pid, &server_str)
            .map(|s| s.into_bytes())
    })
}

// ── Cancellation ─────────────────────────────────────────────────────

#[no_mangle]
pub unsafe extern "C" fn bex_cancel_request(
    engine: *mut BexEngineInner,
    request_id: u64,
) -> bool {
    if engine.is_null() {
        return false;
    }
    let inner = &*engine;
    inner.runtime.cancel_request(request_id)
}

// ── Engine Stats ─────────────────────────────────────────────────────

#[no_mangle]
pub unsafe extern "C" fn bex_engine_stats(
    engine: *mut BexEngineInner,
) -> *mut c_char {
    if engine.is_null() {
        return std::ptr::null_mut();
    }
    let inner = &*engine;
    let stats = inner.runtime.stats();
    match serde_json::to_string(&stats) {
        Ok(json) => str_to_cstring(&json),
        Err(_) => std::ptr::null_mut(),
    }
}

// ── Last Error ───────────────────────────────────────────────────────

#[no_mangle]
pub unsafe extern "C" fn bex_engine_last_error(
    engine: *mut BexEngineInner,
) -> *mut c_char {
    if engine.is_null() {
        return std::ptr::null_mut();
    }
    let inner = &*engine;
    match inner.last_error.lock().as_ref() {
        Some(cstr) => {
            let bytes = cstr.as_bytes();
            let dup = CString::from_vec_unchecked(bytes.to_vec());
            dup.into_raw()
        }
        None => std::ptr::null_mut(),
    }
}

// ══════════════════════════════════════════════════════════════════════
// Internal async submit helper
// ══════════════════════════════════════════════════════════════════════

/// Common implementation for all async submit functions.
///
/// - `engine`: raw pointer to BexEngineInner
/// - `pid`: owned String (plugin_id, already converted from C)
/// - `callback`: C function pointer
/// - `user_data`: opaque pointer from C++
/// - `work`: closure that takes `&bex_core::Engine` + `&str` (plugin_id),
///           does the work, and returns `Result<Vec<u8>, BexError>`
///
/// All C strings must be converted to Rust Strings BEFORE calling this,
/// so the closure only captures Send types.
unsafe fn submit_async<F>(
    engine: *mut BexEngineInner,
    pid: String,
    callback: ResultCallback,
    user_data: *mut c_void,
    work: F,
) -> u64
where
    F: FnOnce(&bex_core::Engine, &str) -> Result<Vec<u8>, BexError> + Send + 'static,
{
    if engine.is_null() {
        return 0;
    }

    let inner = &*engine;
    let request_id = inner.next_request_id.fetch_add(1, Ordering::Relaxed);

    // Clone the engine (Arc-based, cheap) for the spawned task
    let engine_clone = inner.runtime.clone_engine();

    // Store callback and user_data as usize for Send-safety across threads.
    // The callback is a function pointer (inherently Send), and user_data
    // is an opaque pointer that C++ guarantees remains valid until callback
    // is invoked.
    let callback_addr = callback as usize;
    let user_data_addr = user_data as usize;

    // Get cancellation token
    let cancel_token = tokio_util::sync::CancellationToken::new();
    inner.runtime.insert_cancellation(request_id, cancel_token.clone());

    // Spawn on the BexRuntime's internal Tokio runtime
    let rt_handle = inner.runtime.tokio_handle();

    rt_handle.spawn(async move {
        // Check cancellation before starting
        if cancel_token.is_cancelled() {
            invoke_callback(callback_addr, user_data_addr, request_id, false,
                format!("CANCELLED: Request {} was cancelled", request_id).as_bytes());
            return;
        }

        // Execute the work using spawn_blocking since bex-core Engine
        // internally uses its own Tokio runtime for HTTP/WASM operations.
        let result = tokio::task::spawn_blocking(move || {
            work(&engine_clone, &pid)
        }).await;

        match result {
            Ok(Ok(payload)) => {
                invoke_callback(callback_addr, user_data_addr, request_id, true, &payload);
            }
            Ok(Err(e)) => {
                let err_msg = format!("{}: {}", error_code_short(&e), e);
                invoke_callback(callback_addr, user_data_addr, request_id, false, err_msg.as_bytes());
            }
            Err(_) => {
                invoke_callback(callback_addr, user_data_addr, request_id, false,
                    b"INTERNAL: Worker thread panicked");
            }
        }
    });

    request_id
}

/// Invoke the C callback with a byte payload.
unsafe fn invoke_callback(
    callback_addr: usize,
    user_data_addr: usize,
    request_id: u64,
    success: bool,
    payload: &[u8],
) {
    let cb: ResultCallback = std::mem::transmute(callback_addr);
    let ud = user_data_addr as *mut c_void;
    cb(ud, request_id, success, payload.as_ptr(), payload.len());
}