feagi-api 0.0.8

FEAGI REST API layer with HTTP and ZMQ transport adapters
Documentation
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
// Copyright 2025 Neuraville Inc.
// Licensed under the Apache License, Version 2.0

//! System API Endpoints - Exact port from Python `/v1/system/*`
//!
//! Reference: feagi-py/feagi/api/v1/system.py

use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::amalgamation;
use crate::common::ApiState;
use crate::common::{ApiError, ApiResult, Json, State};

// ============================================================================
// REQUEST/RESPONSE MODELS (matching Python schemas exactly)
// ============================================================================

#[allow(non_snake_case)] // Field name matches Python API for compatibility
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct FatigueInfo {
    /// Fatigue index (0-100) - maximum utilization across all fatigue criteria
    pub fatigue_index: Option<u8>,
    /// Whether fatigue is currently active (triggers fatigue neuron injection)
    pub fatigue_active: Option<bool>,
    /// Regular neuron utilization percentage (0-100)
    pub regular_neuron_util: Option<u8>,
    /// Memory neuron utilization percentage (0-100)
    pub memory_neuron_util: Option<u8>,
    /// Synapse utilization percentage (0-100)
    pub synapse_util: Option<u8>,
}

#[allow(non_snake_case)] // Field name matches Python API for compatibility
#[derive(Debug, Serialize, Deserialize, utoipa::ToSchema)]
pub struct HealthCheckResponse {
    pub burst_engine: bool,
    pub connected_agents: Option<i32>,
    pub influxdb_availability: bool,
    pub neuron_count_max: i64,
    pub synapse_count_max: i64,
    pub latest_changes_saved_externally: bool,
    pub genome_availability: bool,
    pub genome_validity: Option<bool>,
    pub brain_readiness: bool,
    pub feagi_session: Option<i64>,
    pub fitness: Option<f64>,
    pub cortical_area_count: Option<i32>,
    pub neuron_count: Option<i64>,
    pub memory_neuron_count: Option<i64>,
    pub regular_neuron_count: Option<i64>,
    pub synapse_count: Option<i64>,
    pub estimated_brain_size_in_MB: Option<f64>,
    pub genome_num: Option<i32>,
    pub genome_timestamp: Option<i64>,
    pub simulation_timestep: Option<f64>,
    pub memory_area_stats: Option<HashMap<String, HashMap<String, serde_json::Value>>>,
    pub amalgamation_pending: Option<HashMap<String, serde_json::Value>>,
    /// Hash of brain regions (hierarchy, membership, and properties)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub brain_regions_hash: Option<u64>,
    /// Hash of cortical areas and properties (excluding mappings)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cortical_areas_hash: Option<u64>,
    /// Hash of brain geometry (area positions/dimensions and 2D coordinates)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub brain_geometry_hash: Option<u64>,
    /// Hash of morphology registry
    #[serde(skip_serializing_if = "Option::is_none")]
    pub morphologies_hash: Option<u64>,
    /// Hash of cortical mappings
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cortical_mappings_hash: Option<u64>,
    /// Hash of agent data (ids, capabilities, connection properties)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub agent_data_hash: Option<u64>,
    /// Root brain region ID (UUID string) for O(1) root lookup
    #[serde(skip_serializing_if = "Option::is_none")]
    pub brain_regions_root: Option<String>,
    /// Fatigue information (index, active state, and breakdown of contributing elements)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fatigue: Option<FatigueInfo>,
}

// ============================================================================
// ENDPOINTS
// ============================================================================

/// Get comprehensive system health including burst engine status, neuron/synapse counts, and genome availability.
#[utoipa::path(
    get,
    path = "/v1/system/health_check",
    responses(
        (status = 200, description = "System health retrieved successfully", body = HealthCheckResponse),
        (status = 500, description = "Internal server error")
    ),
    tag = "system"
)]
pub async fn get_health_check(
    State(state): State<ApiState>,
) -> ApiResult<Json<HealthCheckResponse>> {
    let analytics_service = state.analytics_service.as_ref();

    // Get system health from analytics service
    let health = analytics_service
        .get_system_health()
        .await
        .map_err(|e| ApiError::internal(format!("Failed to get system health: {}", e)))?;

    // Get runtime status if available (source of truth for current burst frequency).
    let runtime_status = state.runtime_service.get_status().await.ok();
    let burst_engine_active = runtime_status
        .as_ref()
        .map(|status| status.is_running)
        .unwrap_or(false);

    let _burst_count = state.runtime_service.get_burst_count().await.ok();

    // Get connected agents count from agent service
    let connected_agents = if let Some(agent_service) = state.agent_service.as_ref() {
        agent_service
            .list_agents()
            .await
            .ok()
            .map(|agents| agents.len() as i32)
    } else {
        None
    };

    // Get total synapse count from analytics service
    let synapse_count = analytics_service
        .get_total_synapse_count()
        .await
        .ok()
        .map(|count| count as i64);

    // Get regular and memory neuron counts
    let regular_neuron_count = analytics_service
        .get_regular_neuron_count()
        .await
        .ok()
        .map(|count| count as i64);

    let memory_neuron_count = analytics_service
        .get_memory_neuron_count()
        .await
        .ok()
        .map(|count| count as i64);

    // Get genome info for simulation_timestep, genome_num, and genome_timestamp
    let genome_info = state.genome_service.get_genome_info().await.ok();

    // Prefer runtime frequency-derived timestep to reflect the active simulation rate.
    let runtime_timestep = runtime_status.as_ref().map(|status| {
        if status.frequency_hz > 0.0 {
            1.0 / status.frequency_hz
        } else {
            0.0
        }
    });
    let simulation_timestep =
        runtime_timestep.or_else(|| genome_info.as_ref().map(|info| info.simulation_timestep));
    let genome_num = genome_info.as_ref().and_then(|info| info.genome_num);
    let genome_timestamp = genome_info.as_ref().and_then(|info| info.genome_timestamp);

    // Calculate estimated brain size in MB
    // Rough estimates: ~64 bytes per neuron + ~16 bytes per synapse + metadata
    #[allow(non_snake_case)] // Matching Python API field name for compatibility
    let estimated_brain_size_in_MB = {
        let neuron_bytes = health.neuron_count * 64;
        let synapse_bytes = synapse_count.unwrap_or(0) as usize * 16;
        let metadata_bytes = health.cortical_area_count * 512; // ~512 bytes per area
        let total_bytes = neuron_bytes + synapse_bytes + metadata_bytes;
        Some((total_bytes as f64) / (1024.0 * 1024.0))
    };

    // Get actual NPU capacity from SystemHealth (single source of truth from config)
    let neuron_count_max = health.neuron_capacity as i64;
    let synapse_count_max = health.synapse_capacity as i64;

    // Configuration values (should eventually come from config service)
    let influxdb_availability = false; // TODO: Get from monitoring service
    let latest_changes_saved_externally = false; // TODO: Get from state manager
    let genome_availability = health.cortical_area_count > 0;
    let genome_validity = Some(health.brain_readiness);

    // Get FEAGI session timestamp (unique identifier for this FEAGI instance)
    let feagi_session = Some(state.feagi_session_timestamp);

    // Fields requiring future service implementations
    let fitness = None; // TODO: Get from evolution service

    // Get memory area stats from plasticity service cache (event-driven updates).
    //
    // IMPORTANT: BV expects both:
    // - per-area stats keyed by cortical_id (base64), and
    // - a global `memory_neuron_count` that matches the sum of per-area `neuron_count`.
    let (memory_area_stats, memory_neuron_count_from_cache) = state
        .memory_stats_cache
        .as_ref()
        .map(|cache| {
            let snapshot = feagi_npu_plasticity::memory_stats_cache::get_stats_snapshot(cache);
            let total = snapshot
                .values()
                .map(|s| s.neuron_count as i64)
                .sum::<i64>();
            let per_area = snapshot
                .into_iter()
                .map(|(name, stats)| {
                    let mut inner_map = HashMap::new();
                    inner_map.insert(
                        "neuron_count".to_string(),
                        serde_json::json!(stats.neuron_count),
                    );
                    inner_map.insert(
                        "created_total".to_string(),
                        serde_json::json!(stats.created_total),
                    );
                    inner_map.insert(
                        "deleted_total".to_string(),
                        serde_json::json!(stats.deleted_total),
                    );
                    inner_map.insert(
                        "last_updated".to_string(),
                        serde_json::json!(stats.last_updated),
                    );
                    (name, inner_map)
                })
                .collect::<HashMap<String, HashMap<String, serde_json::Value>>>();
            (Some(per_area), Some(total))
        })
        .unwrap_or((None, None));

    // Prefer the plasticity cache-derived total to avoid discrepancies.
    let memory_neuron_count = memory_neuron_count_from_cache.or(memory_neuron_count);

    // BV expects `amalgamation_pending` to be a dict with:
    // - amalgamation_id
    // - genome_title
    // - circuit_size
    let amalgamation_pending = state.amalgamation_state.read().pending.as_ref().map(|p| {
        let v = amalgamation::pending_summary_to_health_json(&p.summary);
        v.as_object()
            .cloned()
            .unwrap_or_default()
            .into_iter()
            .collect::<HashMap<String, serde_json::Value>>()
    });

    // Get root region ID from ConnectomeManager (only available when services feature is enabled)
    #[cfg(feature = "services")]
    let brain_regions_root = feagi_brain_development::ConnectomeManager::instance()
        .read()
        .get_root_region_id();
    #[cfg(not(feature = "services"))]
    let brain_regions_root = None; // WASM: Use connectome service instead

    // Get fatigue information from state manager
    // Note: feagi-state-manager is included in the "services" feature
    #[cfg(feature = "services")]
    let fatigue = {
        use feagi_state_manager::StateManager;
        // Initialize singleton on first access (Lazy will handle this)
        match StateManager::instance().try_read() {
            Some(state_manager) => {
                let core_state = state_manager.get_core_state();
                Some(FatigueInfo {
                    fatigue_index: Some(core_state.get_fatigue_index()),
                    fatigue_active: Some(core_state.is_fatigue_active()),
                    regular_neuron_util: Some(core_state.get_regular_neuron_util()),
                    memory_neuron_util: Some(core_state.get_memory_neuron_util()),
                    synapse_util: Some(core_state.get_synapse_util()),
                })
            }
            None => {
                // State manager is locked, return None (shouldn't happen in normal operation)
                tracing::warn!(target: "feagi-api", "StateManager is locked, cannot read fatigue data");
                None
            }
        }
    };
    #[cfg(not(feature = "services"))]
    let fatigue = {
        tracing::debug!(target: "feagi-api", "Services feature not enabled, fatigue data unavailable");
        None
    };

    let (
        brain_regions_hash,
        cortical_areas_hash,
        brain_geometry_hash,
        morphologies_hash,
        cortical_mappings_hash,
        agent_data_hash,
    ) = {
        let state_manager = feagi_state_manager::StateManager::instance();
        let state_manager = state_manager.read();
        (
            Some(state_manager.get_brain_regions_hash()),
            Some(state_manager.get_cortical_areas_hash()),
            Some(state_manager.get_brain_geometry_hash()),
            Some(state_manager.get_morphologies_hash()),
            Some(state_manager.get_cortical_mappings_hash()),
            Some(state_manager.get_agent_data_hash()),
        )
    };

    Ok(Json(HealthCheckResponse {
        burst_engine: burst_engine_active,
        connected_agents,
        influxdb_availability,
        neuron_count_max,
        synapse_count_max,
        latest_changes_saved_externally,
        genome_availability,
        genome_validity,
        brain_readiness: health.brain_readiness,
        feagi_session,
        fitness,
        cortical_area_count: Some(health.cortical_area_count as i32),
        neuron_count: Some(health.neuron_count as i64),
        memory_neuron_count,
        regular_neuron_count,
        synapse_count,
        estimated_brain_size_in_MB,
        genome_num,
        genome_timestamp,
        simulation_timestep,
        memory_area_stats,
        amalgamation_pending,
        brain_regions_hash,
        cortical_areas_hash,
        brain_geometry_hash,
        morphologies_hash,
        cortical_mappings_hash,
        agent_data_hash,
        brain_regions_root, // NEW: Root region ID for O(1) lookup
        fatigue,
    }))
}

/// Get the visualization skip rate (how many frames to skip during visualization).
#[utoipa::path(
    get,
    path = "/v1/system/cortical_area_visualization_skip_rate",
    responses(
        (status = 200, description = "Skip rate retrieved successfully", body = i32),
        (status = 500, description = "Internal server error")
    ),
    tag = "system"
)]
pub async fn get_cortical_area_visualization_skip_rate(
    State(_state): State<ApiState>,
) -> ApiResult<Json<i32>> {
    // TODO: Get from visualization config service
    // For now return default value
    Ok(Json(1))
}

/// Set the visualization skip rate to reduce visualization frequency and improve performance.
#[utoipa::path(
    put,
    path = "/v1/system/cortical_area_visualization_skip_rate",
    request_body = i32,
    responses(
        (status = 200, description = "Skip rate updated successfully"),
        (status = 500, description = "Internal server error")
    ),
    tag = "system"
)]
pub async fn set_cortical_area_visualization_skip_rate(
    State(_state): State<ApiState>,
    Json(skip_rate): Json<i32>,
) -> ApiResult<Json<serde_json::Value>> {
    // TODO: Set in visualization config service
    Ok(Json(serde_json::json!({
        "message": format!("Skip rate set to {}", skip_rate)
    })))
}

/// Get the threshold below which cortical areas are suppressed from visualization.
#[utoipa::path(
    get,
    path = "/v1/system/cortical_area_visualization_suppression_threshold",
    responses(
        (status = 200, description = "Threshold retrieved successfully", body = i32),
        (status = 500, description = "Internal server error")
    ),
    tag = "system"
)]
pub async fn get_cortical_area_visualization_suppression_threshold(
    State(_state): State<ApiState>,
) -> ApiResult<Json<i32>> {
    // TODO: Get from visualization config service
    // For now return default value
    Ok(Json(0))
}

/// Set the threshold for suppressing low-activity cortical areas from visualization.
#[utoipa::path(
    put,
    path = "/v1/system/cortical_area_visualization_suppression_threshold",
    request_body = i32,
    responses(
        (status = 200, description = "Threshold updated successfully"),
        (status = 500, description = "Internal server error")
    ),
    tag = "system"
)]
pub async fn set_cortical_area_visualization_suppression_threshold(
    State(_state): State<ApiState>,
    Json(threshold): Json<i32>,
) -> ApiResult<Json<serde_json::Value>> {
    // TODO: Set in visualization config service
    Ok(Json(serde_json::json!({
        "message": format!("Suppression threshold set to {}", threshold)
    })))
}

// ============================================================================
// SYSTEM VERSION & INFO ENDPOINTS
// ============================================================================

/// Get the current FEAGI version string.
#[utoipa::path(
    get,
    path = "/v1/system/version",
    tag = "system",
    responses(
        (status = 200, description = "Version string", body = String)
    )
)]
pub async fn get_version(State(_state): State<ApiState>) -> ApiResult<Json<String>> {
    Ok(Json(env!("CARGO_PKG_VERSION").to_string()))
}

/// Get detailed version information for all FEAGI crates and components.
#[utoipa::path(
    get,
    path = "/v1/system/versions",
    tag = "system",
    responses(
        (status = 200, description = "Version information", body = HashMap<String, String>)
    )
)]
pub async fn get_versions(
    State(state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, String>>> {
    // Use system service to get version information
    // The application (feagi-rust) provides this at startup with all crates it was compiled with
    match state.system_service.get_version().await {
        Ok(version_info) => {
            let mut versions = version_info.crates.clone();

            // Add build metadata
            versions.insert("rust".to_string(), version_info.rust_version);
            versions.insert("build_timestamp".to_string(), version_info.build_timestamp);

            Ok(Json(versions))
        }
        Err(e) => {
            // Fallback to minimal version info
            tracing::warn!(
                "Failed to get version from system service: {}, using fallback",
                e
            );
            let mut versions = HashMap::new();
            versions.insert(
                "error".to_string(),
                "system service unavailable".to_string(),
            );
            Ok(Json(versions))
        }
    }
}

/// Get system configuration including API settings, neuron capacity, and synapse limits.
#[utoipa::path(
    get,
    path = "/v1/system/configuration",
    tag = "system",
    responses(
        (status = 200, description = "System configuration", body = HashMap<String, serde_json::Value>)
    )
)]
pub async fn get_configuration(
    State(state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, serde_json::Value>>> {
    // Get actual NPU capacity from analytics service
    let health = state
        .analytics_service
        .get_system_health()
        .await
        .map_err(|e| ApiError::internal(format!("Failed to get system health: {}", e)))?;

    let mut config = HashMap::new();
    config.insert("api_host".to_string(), serde_json::json!("0.0.0.0"));
    config.insert("api_port".to_string(), serde_json::json!(8000));
    // Use actual NPU capacity from system health (NOT hardcoded values)
    config.insert(
        "max_neurons".to_string(),
        serde_json::json!(health.neuron_capacity),
    );
    config.insert(
        "max_synapses".to_string(),
        serde_json::json!(health.synapse_capacity),
    );

    Ok(Json(config))
}

/// Get user preferences including advanced mode, UI magnification, and auto-creation settings.
#[utoipa::path(
    get,
    path = "/v1/system/user_preferences",
    tag = "system",
    responses(
        (status = 200, description = "User preferences", body = HashMap<String, serde_json::Value>)
    )
)]
pub async fn get_user_preferences(
    State(_state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, serde_json::Value>>> {
    let mut prefs = HashMap::new();
    prefs.insert("adv_mode".to_string(), serde_json::json!(false));
    prefs.insert("ui_magnification".to_string(), serde_json::json!(1.0));
    prefs.insert(
        "auto_pns_area_creation".to_string(),
        serde_json::json!(true),
    );

    Ok(Json(prefs))
}

/// Update user preferences for UI customization and behavior settings.
#[utoipa::path(
    put,
    path = "/v1/system/user_preferences",
    tag = "system",
    responses(
        (status = 200, description = "Preferences updated", body = HashMap<String, String>)
    )
)]
pub async fn put_user_preferences(
    State(_state): State<ApiState>,
    Json(_prefs): Json<HashMap<String, serde_json::Value>>,
) -> ApiResult<Json<HashMap<String, String>>> {
    Ok(Json(HashMap::from([(
        "message".to_string(),
        "User preferences updated successfully".to_string(),
    )])))
}

/// Get list of available cortical area types (Sensory, Motor, Custom, Memory, Core).
#[utoipa::path(
    get,
    path = "/v1/system/cortical_area_types",
    tag = "system",
    responses(
        (status = 200, description = "Cortical area types", body = Vec<String>)
    )
)]
pub async fn get_cortical_area_types_list(
    State(_state): State<ApiState>,
) -> ApiResult<Json<Vec<String>>> {
    Ok(Json(vec![
        "Sensory".to_string(),
        "Motor".to_string(),
        "Custom".to_string(),
        "Memory".to_string(),
        "Core".to_string(),
    ]))
}

/// Enable the Fire Queue (FQ) sampler for visualization data streaming.
#[utoipa::path(
    post,
    path = "/v1/system/enable_visualization_fq_sampler",
    tag = "system",
    responses(
        (status = 200, description = "FQ sampler enabled", body = HashMap<String, String>)
    )
)]
pub async fn post_enable_visualization_fq_sampler(
    State(state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, String>>> {
    let runtime_service = state.runtime_service.as_ref();

    runtime_service
        .set_fcl_sampler_config(None, Some(1))
        .await
        .map_err(|e| ApiError::internal(format!("Failed to enable FQ sampler: {}", e)))?;

    Ok(Json(HashMap::from([(
        "message".to_string(),
        "Visualization FQ sampler enabled".to_string(),
    )])))
}

/// Disable the Fire Queue (FQ) sampler to stop visualization data streaming.
#[utoipa::path(
    post,
    path = "/v1/system/disable_visualization_fq_sampler",
    tag = "system",
    responses(
        (status = 200, description = "FQ sampler disabled", body = HashMap<String, String>)
    )
)]
pub async fn post_disable_visualization_fq_sampler(
    State(state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, String>>> {
    let runtime_service = state.runtime_service.as_ref();

    runtime_service
        .set_fcl_sampler_config(None, Some(0))
        .await
        .map_err(|e| ApiError::internal(format!("Failed to disable FQ sampler: {}", e)))?;

    Ok(Json(HashMap::from([(
        "message".to_string(),
        "Visualization FQ sampler disabled".to_string(),
    )])))
}

/// Get Fire Candidate List (FCL) sampler status including frequency and consumer state.
#[utoipa::path(
    get,
    path = "/v1/system/fcl_status",
    tag = "system",
    responses(
        (status = 200, description = "FCL status", body = HashMap<String, serde_json::Value>)
    )
)]
pub async fn get_fcl_status_system(
    State(state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, serde_json::Value>>> {
    let runtime_service = state.runtime_service.as_ref();

    let (frequency, consumer) = runtime_service
        .get_fcl_sampler_config()
        .await
        .map_err(|e| ApiError::internal(format!("Failed to get FCL status: {}", e)))?;

    let mut response = HashMap::new();
    response.insert("available".to_string(), serde_json::json!(true));
    response.insert("frequency".to_string(), serde_json::json!(frequency));
    response.insert("consumer".to_string(), serde_json::json!(consumer));
    response.insert("enabled".to_string(), serde_json::json!(consumer > 0));

    Ok(Json(response))
}

/// Reset the Fire Candidate List (FCL) to clear all pending fire candidates.
#[utoipa::path(
    post,
    path = "/v1/system/fcl_reset",
    tag = "system",
    responses(
        (status = 200, description = "FCL reset", body = HashMap<String, String>)
    )
)]
pub async fn post_fcl_reset_system(
    State(_state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, String>>> {
    tracing::info!(target: "feagi-api", "FCL reset requested");

    Ok(Json(HashMap::from([(
        "message".to_string(),
        "FCL reset successfully".to_string(),
    )])))
}

/// Get status of active system processes including burst engine and API server.
#[utoipa::path(
    get,
    path = "/v1/system/processes",
    tag = "system",
    responses(
        (status = 200, description = "Active processes", body = HashMap<String, serde_json::Value>)
    )
)]
pub async fn get_processes(
    State(state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, serde_json::Value>>> {
    let runtime_service = state.runtime_service.as_ref();

    let status = runtime_service
        .get_status()
        .await
        .map_err(|e| ApiError::internal(format!("Failed to get processes: {}", e)))?;

    let mut processes = HashMap::new();
    processes.insert(
        "burst_engine".to_string(),
        serde_json::json!({
            "active": status.is_running,
            "paused": status.is_paused
        }),
    );
    processes.insert(
        "api_server".to_string(),
        serde_json::json!({"active": true}),
    );

    Ok(Json(processes))
}

/// Get collection of unique log messages for debugging and monitoring.
#[utoipa::path(
    get,
    path = "/v1/system/unique_logs",
    tag = "system",
    responses(
        (status = 200, description = "Unique logs", body = HashMap<String, Vec<String>>)
    )
)]
pub async fn get_unique_logs(
    State(_state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, Vec<String>>>> {
    let mut response = HashMap::new();
    response.insert("logs".to_string(), Vec::new());

    Ok(Json(response))
}

/// Configure logging settings including log level and output destinations.
#[utoipa::path(
    post,
    path = "/v1/system/logs",
    tag = "system",
    responses(
        (status = 200, description = "Log config updated", body = HashMap<String, String>)
    )
)]
pub async fn post_logs(
    State(_state): State<ApiState>,
    Json(_config): Json<HashMap<String, serde_json::Value>>,
) -> ApiResult<Json<HashMap<String, String>>> {
    Ok(Json(HashMap::from([(
        "message".to_string(),
        "Log configuration updated".to_string(),
    )])))
}

/// Get list of all beacon subscribers currently monitoring system events.
#[utoipa::path(
    get,
    path = "/v1/system/beacon/subscribers",
    tag = "system",
    responses(
        (status = 200, description = "Beacon subscribers", body = Vec<String>)
    )
)]
pub async fn get_beacon_subscribers(
    State(_state): State<ApiState>,
) -> ApiResult<Json<Vec<String>>> {
    Ok(Json(Vec::new()))
}

/// Subscribe to system beacon for event notifications and status updates.
#[utoipa::path(
    post,
    path = "/v1/system/beacon/subscribe",
    tag = "system",
    responses(
        (status = 200, description = "Subscribed", body = HashMap<String, String>)
    )
)]
pub async fn post_beacon_subscribe(
    State(_state): State<ApiState>,
    Json(_request): Json<HashMap<String, String>>,
) -> ApiResult<Json<HashMap<String, String>>> {
    Ok(Json(HashMap::from([(
        "message".to_string(),
        "Subscribed to beacon".to_string(),
    )])))
}

/// Unsubscribe from system beacon to stop receiving event notifications.
#[utoipa::path(
    delete,
    path = "/v1/system/beacon/unsubscribe",
    tag = "system",
    responses(
        (status = 200, description = "Unsubscribed", body = HashMap<String, String>)
    )
)]
pub async fn delete_beacon_unsubscribe(
    State(_state): State<ApiState>,
    Json(_request): Json<HashMap<String, String>>,
) -> ApiResult<Json<HashMap<String, String>>> {
    Ok(Json(HashMap::from([(
        "message".to_string(),
        "Unsubscribed from beacon".to_string(),
    )])))
}

/// Get global activity visualization configuration including enabled state and frequency.
#[utoipa::path(
    get,
    path = "/v1/system/global_activity_visualization",
    tag = "system",
    responses(
        (status = 200, description = "Global activity viz status", body = HashMap<String, serde_json::Value>)
    )
)]
pub async fn get_global_activity_visualization(
    State(_state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, serde_json::Value>>> {
    let mut response = HashMap::new();
    response.insert("enabled".to_string(), serde_json::json!(false));
    response.insert("frequency_hz".to_string(), serde_json::json!(30.0));

    Ok(Json(response))
}

/// Configure global activity visualization settings and frequency.
#[utoipa::path(
    put,
    path = "/v1/system/global_activity_visualization",
    tag = "system",
    responses(
        (status = 200, description = "Configured", body = HashMap<String, String>)
    )
)]
pub async fn put_global_activity_visualization(
    State(_state): State<ApiState>,
    Json(_config): Json<HashMap<String, serde_json::Value>>,
) -> ApiResult<Json<HashMap<String, String>>> {
    Ok(Json(HashMap::from([(
        "message".to_string(),
        "Global activity visualization configured".to_string(),
    )])))
}

/// Set the file system path for the circuit library storage location.
#[utoipa::path(
    post,
    path = "/v1/system/circuit_library_path",
    tag = "system",
    responses(
        (status = 200, description = "Path set", body = HashMap<String, String>)
    )
)]
pub async fn post_circuit_library_path(
    State(_state): State<ApiState>,
    Json(_request): Json<HashMap<String, String>>,
) -> ApiResult<Json<HashMap<String, String>>> {
    Ok(Json(HashMap::from([(
        "message".to_string(),
        "Circuit library path updated".to_string(),
    )])))
}

/// Test connectivity to InfluxDB database for time-series data storage.
#[utoipa::path(
    get,
    path = "/v1/system/db/influxdb/test",
    tag = "system",
    responses(
        (status = 200, description = "Test result", body = HashMap<String, bool>)
    )
)]
pub async fn get_influxdb_test(
    State(_state): State<ApiState>,
) -> ApiResult<Json<HashMap<String, bool>>> {
    let mut response = HashMap::new();
    response.insert("connected".to_string(), false);
    response.insert("available".to_string(), false);

    Ok(Json(response))
}

/// Register a new system component or module with FEAGI.
#[utoipa::path(
    post,
    path = "/v1/system/register",
    tag = "system",
    responses(
        (status = 200, description = "Registered", body = HashMap<String, String>)
    )
)]
pub async fn post_register_system(
    State(_state): State<ApiState>,
    Json(_request): Json<HashMap<String, serde_json::Value>>,
) -> ApiResult<Json<HashMap<String, String>>> {
    Ok(Json(HashMap::from([(
        "message".to_string(),
        "System component registered".to_string(),
    )])))
}