adk-gateway 1.0.0

Multi-channel AI gateway for adk-rust agents — Telegram, Slack, WhatsApp, Discord, Matrix + control panel
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
//! Phase 2 API endpoints for the control panel (Tasks 15–22).
//!
//! Provides endpoints for:
//! - Stale context configuration (Task 15)
//! - Rate limiter configuration and metrics (Task 16)
//! - ACP agent management (Task 17)
//! - Health monitor (Task 18)
//! - Multi-user management (Task 19)
//! - Config encryption (Task 20)
//! - Log rotation and storage (Task 21)
//! - Deployment / system info (Task 22)

use super::ControlPanelState;
use std::sync::Arc;

// ── Task 15: Stale Context ─────────────────────────────────────────

/// GET /ui/api/settings/stale-context — get stale context configuration.
pub(crate) async fn get_stale_context_config(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let config = state.config.load();
    let idle_hours = config.stale_context.idle_threshold_secs as f64 / 3600.0;

    axum::Json(serde_json::json!({
        "ok": true,
        "data": {
            "idle_threshold_hours": idle_hours,
            "idle_threshold_secs": config.stale_context.idle_threshold_secs,
        }
    }))
}

/// POST /ui/api/settings/stale-context — save stale context configuration.
pub(crate) async fn save_stale_context_config(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
    axum::Json(payload): axum::Json<serde_json::Value>,
) -> axum::Json<serde_json::Value> {
    let config_path = match &state.config_path {
        Some(p) => p.clone(),
        None => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": "Config file path not configured"
            }));
        }
    };

    let idle_hours = payload
        .get("idle_threshold_hours")
        .and_then(|v| v.as_f64())
        .unwrap_or(4.0);

    let idle_secs = (idle_hours * 3600.0) as u64;

    let mut cfg = state.config.load().as_ref().clone();
    cfg.stale_context.idle_threshold_secs = idle_secs;

    let output = match serde_json::to_string_pretty(&cfg) {
        Ok(s) => s,
        Err(e) => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": format!("Failed to serialize config: {e}")
            }));
        }
    };

    if let Err(e) = std::fs::write(&config_path, &output) {
        return axum::Json(serde_json::json!({
            "ok": false,
            "message": format!("Failed to write config: {e}")
        }));
    }

    state.config.store(Arc::new(cfg));
    tracing::info!(idle_threshold_secs = idle_secs, "Stale context config saved");

    axum::Json(serde_json::json!({
        "ok": true,
        "message": "Stale context configuration saved."
    }))
}

/// GET /ui/api/users/activity — get user activity data (placeholder).
pub(crate) async fn get_user_activities(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!([]))
}

// ── Task 16: Rate Limiter ──────────────────────────────────────────

/// GET /ui/api/settings/rate-limit — get rate limiter configuration.
pub(crate) async fn get_rate_limit_config(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let config = state.config.load();
    let rl = &config.rate_limiter;

    axum::Json(serde_json::json!({
        "ok": true,
        "data": {
            "max_calls": rl.max_calls,
            "window_secs": rl.window_secs,
            "cooldown_secs": rl.cooldown_secs,
            "max_triggers": rl.max_triggers,
        }
    }))
}

/// POST /ui/api/settings/rate-limit — save rate limiter configuration.
pub(crate) async fn save_rate_limit_config(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
    axum::Json(payload): axum::Json<serde_json::Value>,
) -> axum::Json<serde_json::Value> {
    let config_path = match &state.config_path {
        Some(p) => p.clone(),
        None => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": "Config file path not configured"
            }));
        }
    };

    let max_calls = payload.get("max_calls").and_then(|v| v.as_u64()).unwrap_or(10) as u32;
    let window_secs = payload.get("window_secs").and_then(|v| v.as_u64()).unwrap_or(5);
    let cooldown_secs = payload.get("cooldown_secs").and_then(|v| v.as_u64()).unwrap_or(3);
    let max_triggers = payload.get("max_triggers").and_then(|v| v.as_u64()).unwrap_or(3) as u32;

    let mut cfg = state.config.load().as_ref().clone();
    cfg.rate_limiter.max_calls = max_calls;
    cfg.rate_limiter.window_secs = window_secs;
    cfg.rate_limiter.cooldown_secs = cooldown_secs;
    cfg.rate_limiter.max_triggers = max_triggers;

    let output = match serde_json::to_string_pretty(&cfg) {
        Ok(s) => s,
        Err(e) => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": format!("Failed to serialize config: {e}")
            }));
        }
    };

    if let Err(e) = std::fs::write(&config_path, &output) {
        return axum::Json(serde_json::json!({
            "ok": false,
            "message": format!("Failed to write config: {e}")
        }));
    }

    state.config.store(Arc::new(cfg));
    tracing::info!("Rate limiter config saved via control panel");

    axum::Json(serde_json::json!({
        "ok": true,
        "message": "Rate limiter configuration saved."
    }))
}

/// GET /ui/api/settings/rate-limit/metrics — get rate limit metrics (placeholder).
pub(crate) async fn get_rate_limit_metrics(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({"triggered_today": 0}))
}

// ── Task 17: ACP ───────────────────────────────────────────────────

/// GET /ui/api/acp/agents — list ACP agents (placeholder).
pub(crate) async fn get_acp_agents(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!([]))
}

/// POST /ui/api/acp/agents — add an ACP agent (placeholder).
pub(crate) async fn add_acp_agent(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
    axum::Json(_payload): axum::Json<serde_json::Value>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({
        "ok": true,
        "message": "ACP agent added (placeholder)."
    }))
}

/// DELETE /ui/api/acp/agents/{id} — remove an ACP agent (placeholder).
pub(crate) async fn remove_acp_agent(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
    axum::extract::Path(_id): axum::extract::Path<String>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({
        "ok": true,
        "message": "ACP agent removed (placeholder)."
    }))
}

/// GET /ui/api/acp/enabled — check if ACP feature is enabled.
pub(crate) async fn get_acp_feature_enabled(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({"enabled": cfg!(feature = "acp")}))
}

// ── Task 18: Health Monitor ────────────────────────────────────────

/// GET /ui/api/health/components — list health-monitored components (placeholder).
pub(crate) async fn get_health_components(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!([]))
}

/// GET /ui/api/health/events — list health events (placeholder).
pub(crate) async fn get_health_events(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!([]))
}

/// GET /ui/api/settings/health-monitor — get health monitor configuration.
pub(crate) async fn get_health_monitor_config(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let config = state.config.load();
    let hm = &config.health_monitor;

    axum::Json(serde_json::json!({
        "ok": true,
        "data": {
            "check_interval_secs": hm.check_interval_secs,
            "failure_threshold": hm.failure_threshold,
            "alert_webhook_url": hm.alert_webhook_url,
            "alert_telegram_admin": hm.alert_telegram_admin,
        }
    }))
}

/// POST /ui/api/settings/health-monitor — save health monitor configuration.
pub(crate) async fn save_health_monitor_config(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
    axum::Json(payload): axum::Json<serde_json::Value>,
) -> axum::Json<serde_json::Value> {
    let config_path = match &state.config_path {
        Some(p) => p.clone(),
        None => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": "Config file path not configured"
            }));
        }
    };

    let check_interval_secs = payload
        .get("check_interval_secs")
        .and_then(|v| v.as_u64())
        .unwrap_or(60);
    let failure_threshold = payload
        .get("failure_threshold")
        .and_then(|v| v.as_u64())
        .unwrap_or(3) as u32;
    let alert_webhook_url = payload
        .get("alert_webhook_url")
        .and_then(|v| v.as_str())
        .filter(|s| !s.is_empty())
        .map(String::from);
    let alert_telegram_admin = payload
        .get("alert_telegram_admin")
        .and_then(|v| v.as_str())
        .filter(|s| !s.is_empty())
        .map(String::from);

    let mut cfg = state.config.load().as_ref().clone();
    cfg.health_monitor.check_interval_secs = check_interval_secs;
    cfg.health_monitor.failure_threshold = failure_threshold;
    cfg.health_monitor.alert_webhook_url = alert_webhook_url;
    cfg.health_monitor.alert_telegram_admin = alert_telegram_admin;

    let output = match serde_json::to_string_pretty(&cfg) {
        Ok(s) => s,
        Err(e) => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": format!("Failed to serialize config: {e}")
            }));
        }
    };

    if let Err(e) = std::fs::write(&config_path, &output) {
        return axum::Json(serde_json::json!({
            "ok": false,
            "message": format!("Failed to write config: {e}")
        }));
    }

    state.config.store(Arc::new(cfg));
    tracing::info!("Health monitor config saved via control panel");

    axum::Json(serde_json::json!({
        "ok": true,
        "message": "Health monitor configuration saved."
    }))
}

// ── Task 19: Multi-User ────────────────────────────────────────────

/// GET /ui/api/users/paired — list paired users (placeholder).
pub(crate) async fn get_paired_users(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!([]))
}

/// POST /ui/api/users/{id}/unpair — unpair a user (placeholder).
pub(crate) async fn unpair_user(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
    axum::extract::Path(_id): axum::extract::Path<String>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({
        "ok": true,
        "message": "User unpaired (placeholder)."
    }))
}

/// POST /ui/api/users/{id}/heartbeat — update user heartbeat (placeholder).
pub(crate) async fn update_user_heartbeat(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
    axum::extract::Path(_id): axum::extract::Path<String>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({
        "ok": true,
        "message": "Heartbeat updated (placeholder)."
    }))
}

/// GET /ui/api/users/groups — get group assignments (placeholder).
pub(crate) async fn get_group_assignments(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!([]))
}

/// POST /ui/api/users/groups — save a group assignment (placeholder).
pub(crate) async fn save_group_assignment(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
    axum::Json(_payload): axum::Json<serde_json::Value>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({
        "ok": true,
        "message": "Group assignment saved (placeholder)."
    }))
}

// ── Task 20: Config Encryption ─────────────────────────────────────

/// GET /ui/api/encryption/status — check encryption status.
pub(crate) async fn get_encryption_status(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let config = state.config.load();
    let has_key = config.gateway.encryption.is_some();
    let key_path = config
        .gateway
        .encryption
        .as_ref()
        .map(|e| e.key_file.display().to_string());

    axum::Json(serde_json::json!({
        "ok": true,
        "data": {
            "encryption_configured": has_key,
            "key_path": key_path,
        }
    }))
}

/// GET /ui/api/encryption/sensitive-fields — list sensitive fields in config.
pub(crate) async fn get_sensitive_fields(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let config = state.config.load();
    let config_json = match serde_json::to_value(config.as_ref()) {
        Ok(v) => v,
        Err(e) => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": format!("Failed to serialize config: {e}")
            }));
        }
    };

    let fields = collect_sensitive_fields(&config_json, "");

    axum::Json(serde_json::json!({
        "ok": true,
        "data": fields,
    }))
}

/// POST /ui/api/encryption/encrypt-all — encrypt all sensitive fields.
pub(crate) async fn encrypt_all(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let config = state.config.load();
    let key_file = match &config.gateway.encryption {
        Some(enc) => enc.key_file.clone(),
        None => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": "No encryption key configured. Set gateway.encryption.keyFile first."
            }));
        }
    };

    let config_path = match &state.config_path {
        Some(p) => p.clone(),
        None => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": "Config file path not configured"
            }));
        }
    };

    match crate::config_encryption::encrypt_config_file(&config_path, &key_file) {
        Ok(count) => axum::Json(serde_json::json!({
            "ok": true,
            "message": format!("Encrypted {count} sensitive field(s)."),
            "encrypted_count": count,
        })),
        Err(e) => axum::Json(serde_json::json!({
            "ok": false,
            "message": format!("Encryption failed: {e}")
        })),
    }
}

/// POST /ui/api/encryption/key-path — save encryption key path to config.
pub(crate) async fn save_encryption_key_path(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
    axum::Json(payload): axum::Json<serde_json::Value>,
) -> axum::Json<serde_json::Value> {
    let config_path = match &state.config_path {
        Some(p) => p.clone(),
        None => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": "Config file path not configured"
            }));
        }
    };

    let key_path = match payload.get("key_path").and_then(|v| v.as_str()) {
        Some(p) => p.to_string(),
        None => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": "Missing 'key_path' field."
            }));
        }
    };

    let mut cfg = state.config.load().as_ref().clone();
    cfg.gateway.encryption = Some(crate::config::EncryptionConfig {
        key_file: std::path::PathBuf::from(&key_path),
    });

    let output = match serde_json::to_string_pretty(&cfg) {
        Ok(s) => s,
        Err(e) => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": format!("Failed to serialize config: {e}")
            }));
        }
    };

    if let Err(e) = std::fs::write(&config_path, &output) {
        return axum::Json(serde_json::json!({
            "ok": false,
            "message": format!("Failed to write config: {e}")
        }));
    }

    state.config.store(Arc::new(cfg));
    tracing::info!(key_path = %key_path, "Encryption key path saved");

    axum::Json(serde_json::json!({
        "ok": true,
        "message": "Encryption key path saved."
    }))
}

/// Recursively collect sensitive field paths from a JSON value.
fn collect_sensitive_fields(value: &serde_json::Value, prefix: &str) -> Vec<serde_json::Value> {
    let mut results = Vec::new();
    if let serde_json::Value::Object(map) = value {
        for (key, val) in map {
            let path = if prefix.is_empty() {
                key.clone()
            } else {
                format!("{prefix}.{key}")
            };
            if let serde_json::Value::String(s) = val {
                if crate::config_encryption::ConfigEncryption::is_sensitive_field(key) {
                    results.push(serde_json::json!({
                        "path": path,
                        "encrypted": crate::config_encryption::ConfigEncryption::is_encrypted(s),
                    }));
                }
            } else {
                results.extend(collect_sensitive_fields(val, &path));
            }
        }
    }
    results
}

// ── Task 21: Log Rotation ──────────────────────────────────────────

/// GET /ui/api/settings/log-rotation — get log rotation configuration.
pub(crate) async fn get_log_rotation_config(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let config = state.config.load();
    let lr = &config.telemetry.log_rotation;

    axum::Json(serde_json::json!({
        "ok": true,
        "data": {
            "rotation": format!("{:?}", lr.rotation).to_lowercase(),
            "retention_days": lr.retention_days,
            "max_file_size_mb": lr.max_file_size_mb,
            "log_dir": config.telemetry.log_dir,
        }
    }))
}

/// POST /ui/api/settings/log-rotation — save log rotation configuration.
pub(crate) async fn save_log_rotation_config(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
    axum::Json(payload): axum::Json<serde_json::Value>,
) -> axum::Json<serde_json::Value> {
    let config_path = match &state.config_path {
        Some(p) => p.clone(),
        None => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": "Config file path not configured"
            }));
        }
    };

    let retention_days = payload
        .get("retention_days")
        .and_then(|v| v.as_u64())
        .unwrap_or(7) as u32;
    let max_file_size_mb = payload
        .get("max_file_size_mb")
        .and_then(|v| v.as_u64())
        .unwrap_or(100);

    let mut cfg = state.config.load().as_ref().clone();
    cfg.telemetry.log_rotation.retention_days = retention_days;
    cfg.telemetry.log_rotation.max_file_size_mb = max_file_size_mb;

    let output = match serde_json::to_string_pretty(&cfg) {
        Ok(s) => s,
        Err(e) => {
            return axum::Json(serde_json::json!({
                "ok": false,
                "message": format!("Failed to serialize config: {e}")
            }));
        }
    };

    if let Err(e) = std::fs::write(&config_path, &output) {
        return axum::Json(serde_json::json!({
            "ok": false,
            "message": format!("Failed to write config: {e}")
        }));
    }

    state.config.store(Arc::new(cfg));
    tracing::info!(retention_days, "Log rotation config saved");

    axum::Json(serde_json::json!({
        "ok": true,
        "message": "Log rotation configuration saved."
    }))
}

/// GET /ui/api/logs/storage — get log storage metrics.
pub(crate) async fn get_log_storage_metrics(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let log_dir = std::path::Path::new("logs");
    let (file_count, total_bytes) = scan_log_directory(log_dir);

    axum::Json(serde_json::json!({
        "ok": true,
        "data": {
            "file_count": file_count,
            "total_size_bytes": total_bytes,
            "total_size_mb": total_bytes as f64 / (1024.0 * 1024.0),
        }
    }))
}

/// GET /ui/api/logs/files — list log files with sizes.
pub(crate) async fn get_log_files(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let log_dir = std::path::Path::new("logs");
    let mut files = Vec::new();

    if let Ok(entries) = std::fs::read_dir(log_dir) {
        for entry in entries.flatten() {
            if let Ok(meta) = entry.metadata() {
                if meta.is_file() {
                    files.push(serde_json::json!({
                        "name": entry.file_name().to_string_lossy(),
                        "size_bytes": meta.len(),
                    }));
                }
            }
        }
    }

    // Sort by name (which includes date) descending
    files.sort_by(|a, b| {
        let name_a = a["name"].as_str().unwrap_or("");
        let name_b = b["name"].as_str().unwrap_or("");
        name_b.cmp(name_a)
    });

    axum::Json(serde_json::json!(files))
}

/// GET /ui/api/logs/files/{filename} — download a log file.
pub(crate) async fn download_log_file(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
    axum::extract::Path(filename): axum::extract::Path<String>,
) -> axum::Json<serde_json::Value> {
    // Prevent path traversal
    if filename.contains("..") || filename.contains('/') || filename.contains('\\') {
        return axum::Json(serde_json::json!({
            "ok": false,
            "message": "Invalid filename."
        }));
    }

    let path = std::path::Path::new("logs").join(&filename);
    match std::fs::read_to_string(&path) {
        Ok(contents) => axum::Json(serde_json::json!({
            "ok": true,
            "filename": filename,
            "contents": contents,
        })),
        Err(e) => axum::Json(serde_json::json!({
            "ok": false,
            "message": format!("Failed to read log file: {e}")
        })),
    }
}

/// POST /ui/api/logs/clear-old — delete log files beyond retention period.
pub(crate) async fn clear_old_logs(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let config = state.config.load();
    let retention_days = config.telemetry.log_rotation.retention_days;
    let log_dir = std::path::Path::new("logs");

    let cutoff = chrono::Utc::now() - chrono::Duration::days(retention_days as i64);
    let mut deleted = 0u32;

    if let Ok(entries) = std::fs::read_dir(log_dir) {
        for entry in entries.flatten() {
            if let Ok(meta) = entry.metadata() {
                if !meta.is_file() {
                    continue;
                }
                // Use file modified time as proxy for log date
                if let Ok(modified) = meta.modified() {
                    let modified_dt: chrono::DateTime<chrono::Utc> = modified.into();
                    if modified_dt < cutoff {
                        if std::fs::remove_file(entry.path()).is_ok() {
                            deleted += 1;
                        }
                    }
                }
            }
        }
    }

    tracing::info!(deleted, retention_days, "Old log files cleared");

    axum::Json(serde_json::json!({
        "ok": true,
        "message": format!("Deleted {deleted} old log file(s)."),
        "deleted_count": deleted,
    }))
}

/// Scan a directory for file count and total size.
fn scan_log_directory(dir: &std::path::Path) -> (u32, u64) {
    let mut count = 0u32;
    let mut total = 0u64;

    if let Ok(entries) = std::fs::read_dir(dir) {
        for entry in entries.flatten() {
            if let Ok(meta) = entry.metadata() {
                if meta.is_file() {
                    count += 1;
                    total += meta.len();
                }
            }
        }
    }

    (count, total)
}

// ── Task 22: Deployment / System Info ──────────────────────────────

/// GET /ui/api/system/info — get system information.
pub(crate) async fn get_system_info(
    axum::extract::State(state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    let config = state.config.load();
    let uptime_secs = state.start_time.elapsed().as_secs();
    let config_path = state
        .config_path
        .as_ref()
        .map(|p| p.display().to_string())
        .unwrap_or_else(|| "unknown".to_string());

    axum::Json(serde_json::json!({
        "ok": true,
        "data": {
            "version": env!("CARGO_PKG_VERSION"),
            "adk_rust_version": env!("ADK_RUST_VERSION"),
            "uptime_secs": uptime_secs,
            "config_path": config_path,
            "drain_timeout_secs": config.gateway.drain_timeout_secs,
            "features": {
                "acp": cfg!(feature = "acp"),
                "redis": cfg!(feature = "redis"),
                "firestore": cfg!(feature = "firestore"),
            }
        }
    }))
}

/// GET /ui/api/system/restart-status — get restart status.
pub(crate) async fn get_restart_status(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    axum::Json(serde_json::json!({
        "restarting": false,
        "in_flight_requests": 0,
        "phase": "idle"
    }))
}

/// POST /ui/api/system/restart — trigger a graceful restart.
pub(crate) async fn trigger_restart(
    axum::extract::State(_state): axum::extract::State<Arc<ControlPanelState>>,
) -> axum::Json<serde_json::Value> {
    // The ShutdownCoordinator lives on the GatewayState, not ControlPanelState.
    // A full restart requires sending SIGUSR1 to the process on Unix.
    #[cfg(unix)]
    {
        // Safety: sending a signal to our own process is safe.
        let result = unsafe { libc::kill(libc::getpid(), libc::SIGUSR1) };
        if result == 0 {
            tracing::info!("Graceful restart triggered via control panel (SIGUSR1)");
            return axum::Json(serde_json::json!({
                "ok": true,
                "message": "Graceful restart initiated."
            }));
        }
    }

    axum::Json(serde_json::json!({
        "ok": false,
        "message": "Restart not available on this platform."
    }))
}