synheart-sensor-agent 0.2.2

Privacy-first PC background sensor for behavioral research
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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
//! Synheart Sensor Agent CLI
//!
//! Privacy-first behavioral sensor for research.

use chrono::Utc;
use clap::{Parser, Subcommand};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::thread;
use std::time::Duration;
use synheart_sensor_agent::{
    collector::{check_permission, Collector, CollectorConfig, SensorEvent},
    config::{Config, SourceConfig},
    core::{compute_features, HsiBuilder, HsiSnapshot, WindowManager},
    transparency::create_shared_log_with_persistence,
    PRIVACY_DECLARATION, VERSION,
};

#[cfg(feature = "gateway")]
use synheart_sensor_agent::{BlockingGatewayClient, GatewayConfig};

#[derive(Parser)]
#[command(name = "synheart-sensor")]
#[command(author = "Synheart")]
#[command(version = VERSION)]
#[command(about = "Privacy-first behavioral sensor for research", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Start capturing behavioral data
    Start {
        /// Input sources to capture (keyboard, mouse, or all)
        #[arg(long, default_value = "all")]
        sources: String,

        /// Run in foreground (don't daemonize)
        #[arg(long)]
        foreground: bool,

        /// Enable synheart-flux baseline tracking (requires flux feature)
        #[arg(long)]
        flux: bool,

        /// Baseline window size (number of sessions for rolling baseline)
        #[arg(long, default_value = "20")]
        baseline_window: usize,

        /// Enable gateway sync (requires gateway feature)
        #[arg(long)]
        gateway: bool,

        /// Gateway port (auto-detected from runtime dir if not specified)
        #[arg(long)]
        gateway_port: Option<u16>,

        /// Gateway token (auto-detected from runtime dir if not specified)
        #[arg(long)]
        gateway_token: Option<String>,

        /// Sync interval in seconds (how often to sync to gateway)
        #[arg(long, default_value = "10")]
        sync_interval: u64,
    },

    /// Start HTTP server to receive behavioral data from Chrome extension
    #[cfg(feature = "server")]
    Serve {
        /// Port to listen on
        #[arg(long, default_value = "8081")]
        port: u16,

        /// Gateway host
        #[arg(long, default_value = "127.0.0.1")]
        gateway_host: String,

        /// Gateway port
        #[arg(long, default_value = "8080")]
        gateway_port: u16,

        /// Gateway auth token
        #[arg(long)]
        gateway_token: String,
    },

    /// Pause data collection
    Pause,

    /// Resume data collection
    Resume,

    /// Show current collection status
    Status,

    /// Display privacy declaration
    Privacy,

    /// Export collected HSI snapshots
    Export {
        /// Output directory for snapshots
        #[arg(long, short)]
        output: Option<PathBuf>,

        /// Export format (json or jsonl)
        #[arg(long, default_value = "json")]
        format: String,
    },

    /// Show configuration
    Config,
}

fn main() {
    let cli = Cli::parse();

    match cli.command {
        Commands::Start {
            sources,
            foreground,
            flux,
            baseline_window,
            gateway,
            gateway_port,
            gateway_token,
            sync_interval,
        } => {
            cmd_start(
                &sources,
                foreground,
                flux,
                baseline_window,
                gateway,
                gateway_port,
                gateway_token,
                sync_interval,
            );
        }
        #[cfg(feature = "server")]
        Commands::Serve {
            port,
            gateway_host,
            gateway_port,
            gateway_token,
        } => {
            cmd_serve(port, &gateway_host, gateway_port, &gateway_token);
        }
        Commands::Pause => {
            cmd_pause();
        }
        Commands::Resume => {
            cmd_resume();
        }
        Commands::Status => {
            cmd_status();
        }
        Commands::Privacy => {
            cmd_privacy();
        }
        Commands::Export { output, format } => {
            cmd_export(output, &format);
        }
        Commands::Config => {
            cmd_config();
        }
    }
}

#[allow(unused_variables)]
#[allow(clippy::too_many_arguments)]
fn cmd_start(
    sources: &str,
    _foreground: bool,
    enable_flux: bool,
    baseline_window: usize,
    enable_gateway: bool,
    gateway_port: Option<u16>,
    gateway_token: Option<String>,
    sync_interval: u64,
) {
    println!("Synheart Sensor Agent v{VERSION}");
    println!();

    // Check for input capture permission
    if !check_permission() {
        eprintln!("Error: Insufficient permissions to capture input events.");
        eprintln!();

        #[cfg(target_os = "macos")]
        {
            eprintln!("To grant permission:");
            eprintln!("1. Open System Preferences > Security & Privacy > Privacy");
            eprintln!("2. Select 'Input Monitoring' in the left sidebar");
            eprintln!("3. Add this application to the allowed list");
            eprintln!("4. Restart the application");
        }

        #[cfg(target_os = "windows")]
        {
            eprintln!("Possible causes:");
            eprintln!("1. The application may need to be run as Administrator");
            eprintln!("2. Antivirus or security software may be blocking input hooks");
            eprintln!("3. Group Policy may restrict low-level input access");
            eprintln!();
            eprintln!("Try running with elevated privileges (Run as Administrator).");
        }

        #[cfg(not(any(target_os = "macos", target_os = "windows")))]
        {
            eprintln!("Input capture is not supported on this platform.");
        }

        std::process::exit(1);
    }

    // Parse source configuration
    let source_config = SourceConfig::from_csv(sources);
    if !source_config.any_enabled() {
        eprintln!("Error: At least one source must be enabled (keyboard or mouse)");
        std::process::exit(1);
    }

    // Load or create configuration
    let config = Config::load().unwrap_or_default();
    if let Err(e) = config.ensure_directories() {
        eprintln!("Warning: Could not create directories: {e}");
    }

    println!("Starting collection...");
    println!(
        "  Keyboard: {}",
        if source_config.keyboard {
            "enabled"
        } else {
            "disabled"
        }
    );
    println!(
        "  Mouse: {}",
        if source_config.mouse {
            "enabled"
        } else {
            "disabled"
        }
    );
    println!("  Window duration: {}s", config.window_duration.as_secs());

    // Show flux status
    #[cfg(feature = "flux")]
    if enable_flux {
        println!("  Flux baseline tracking: enabled (window: {baseline_window} sessions)");
    } else {
        println!("  Flux baseline tracking: disabled");
    }
    #[cfg(not(feature = "flux"))]
    if enable_flux {
        eprintln!("Warning: --flux flag ignored (flux feature not enabled at compile time)");
    }

    // Show gateway status
    #[cfg(feature = "gateway")]
    let gateway_client = if enable_gateway {
        match create_gateway_client(gateway_port, gateway_token) {
            Ok(client) => {
                println!("  Gateway sync: enabled (interval: {sync_interval}s)");
                println!("  Device ID: {}", client.device_id());

                // Test connection
                match client.test_connection() {
                    Ok(true) => println!("  Gateway connection: OK"),
                    Ok(false) => {
                        eprintln!("Warning: Gateway health check failed");
                    }
                    Err(e) => {
                        eprintln!("Warning: Could not connect to gateway: {e}");
                    }
                }
                Some(client)
            }
            Err(e) => {
                eprintln!("Warning: Gateway initialization failed: {e}");
                eprintln!("Continuing without gateway sync.");
                None
            }
        }
    } else {
        println!("  Gateway sync: disabled");
        None
    };

    #[cfg(not(feature = "gateway"))]
    if enable_gateway {
        eprintln!("Warning: --gateway flag ignored (gateway feature not enabled at compile time)");
    }

    println!();
    println!("Press Ctrl+C to stop");
    println!();

    // Set up transparency log
    let transparency_log =
        create_shared_log_with_persistence(config.data_path.join("transparency.json"));

    // Create collector
    let collector_config = CollectorConfig {
        capture_keyboard: source_config.keyboard,
        capture_mouse: source_config.mouse,
    };
    let mut collector = Collector::new(collector_config);

    // Create window manager
    let mut window_manager = WindowManager::new(
        config.window_duration.as_secs(),
        config.session_gap_threshold_secs,
    );

    // Create HSI builder
    let hsi_builder = HsiBuilder::new();
    println!("Instance ID: {}", hsi_builder.instance_id());

    // Storage for completed snapshots
    let mut snapshots: Vec<HsiSnapshot> = Vec::new();

    // Initialize flux processor if enabled
    #[cfg(feature = "flux")]
    let mut flux_processor = if enable_flux {
        let mut processor = synheart_sensor_agent::flux::SensorFluxProcessor::new(baseline_window);

        // Try to load existing baselines
        let baselines_path = config.data_path.join("flux_baselines.json");
        if baselines_path.exists() {
            if let Ok(baselines_json) = std::fs::read_to_string(&baselines_path) {
                match processor.load_baselines(&baselines_json) {
                    Ok(_) => println!("Loaded existing baselines from {baselines_path:?}"),
                    Err(e) => eprintln!("Warning: Could not load baselines: {e}"),
                }
            }
        }

        Some(processor)
    } else {
        None
    };

    // Storage for enriched snapshots (when flux is enabled)
    #[cfg(feature = "flux")]
    let mut enriched_snapshots: Vec<synheart_sensor_agent::flux::EnrichedSnapshot> = Vec::new();

    // Set up Ctrl+C handler
    let running = Arc::new(AtomicBool::new(true));
    let r = running.clone();
    ctrlc_handler(r);

    // Support pause/resume from another process by polling the config file.
    // If paused at startup, wait until resumed before starting the collector.
    let mut paused = config.paused;
    let mut last_config_check = std::time::Instant::now();

    if paused {
        println!("Collection is currently paused.");
        println!("Run `synheart-sensor resume` to start collecting.");
        println!();
    } else if let Err(e) = collector.start() {
        eprintln!("Error starting collector: {e}");
        std::process::exit(1);
    }

    // Gateway sync state
    #[cfg(feature = "gateway")]
    let mut pending_sync_snapshots: Vec<HsiSnapshot> = Vec::new();
    #[cfg(feature = "gateway")]
    let mut last_gateway_sync = std::time::Instant::now();
    #[cfg(feature = "gateway")]
    let session_id = format!("SESS-{}", Utc::now().timestamp_millis());

    // Main event loop
    let receiver = collector.receiver().clone();
    let mut last_window_check = std::time::Instant::now();

    while running.load(Ordering::SeqCst) {
        // Periodically reload config so `synheart-sensor pause/resume` can control a running agent.
        if last_config_check.elapsed() >= Duration::from_secs(1) {
            if let Ok(cfg) = Config::load() {
                if cfg.paused != paused {
                    paused = cfg.paused;

                    if paused {
                        println!();
                        println!("Pausing collection...");
                        collector.stop();

                        // Flush any in-progress window and drop partial data.
                        window_manager.flush();
                        let _ = window_manager.take_completed_windows();

                        // Drain any queued events.
                        while receiver.try_recv().is_ok() {}
                    } else {
                        println!();
                        println!("Resuming collection...");
                        if let Err(e) = collector.start() {
                            eprintln!("Error resuming collector: {e}");
                            std::process::exit(1);
                        }
                    }
                }
            }
            last_config_check = std::time::Instant::now();
        }

        if paused {
            thread::sleep(Duration::from_millis(100));
            continue;
        }

        // Process events with timeout
        match receiver.recv_timeout(Duration::from_millis(100)) {
            Ok(event) => {
                // Update transparency log
                match &event {
                    SensorEvent::Keyboard(_) => transparency_log.record_keyboard_event(),
                    SensorEvent::Mouse(_) => transparency_log.record_mouse_event(),
                    SensorEvent::Shortcut(_) => transparency_log.record_shortcut_event(),
                }

                // Add to window
                window_manager.process_event(event);
            }
            Err(crossbeam_channel::RecvTimeoutError::Timeout) => {
                // Check for window expiry periodically
                if last_window_check.elapsed() >= Duration::from_secs(1) {
                    window_manager.check_window_expiry();
                    last_window_check = std::time::Instant::now();
                }
            }
            Err(crossbeam_channel::RecvTimeoutError::Disconnected) => {
                eprintln!("Collector disconnected unexpectedly");
                break;
            }
        }

        // Process completed windows
        for window in window_manager.take_completed_windows() {
            let features = compute_features(&window);
            let snapshot = hsi_builder.build(&window, &features);

            transparency_log.record_window_completed();

            // Process with flux if enabled
            #[cfg(feature = "flux")]
            if let Some(ref mut processor) = flux_processor {
                match processor.process_window(&window, &features, snapshot.clone()) {
                    Ok(enriched) => {
                        let baseline_info = if let Some(ref baseline) = enriched.baseline {
                            format!(
                                " | baseline: {} sessions, dev: {:.1}%",
                                baseline.sessions_in_baseline,
                                baseline.distraction_deviation_pct.unwrap_or(0.0)
                            )
                        } else {
                            String::new()
                        };

                        let flux_info = if let Some(ref flux) = enriched.flux_behavior {
                            format!(
                                " | distraction: {:.2}, focus: {:.2}",
                                flux.distraction_score, flux.focus_hint
                            )
                        } else {
                            String::new()
                        };

                        println!(
                            "[{}] Window completed: {} keyboard, {} mouse events{}{}",
                            window.end.format("%H:%M:%S"),
                            window.keyboard_events.len(),
                            window.mouse_events.len(),
                            flux_info,
                            baseline_info
                        );
                        enriched_snapshots.push(enriched);
                    }
                    Err(e) => {
                        eprintln!("Warning: Flux processing failed: {e}");
                        println!(
                            "[{}] Window completed: {} keyboard, {} mouse events",
                            window.end.format("%H:%M:%S"),
                            window.keyboard_events.len(),
                            window.mouse_events.len()
                        );
                    }
                }
            } else {
                println!(
                    "[{}] Window completed: {} keyboard, {} mouse events",
                    window.end.format("%H:%M:%S"),
                    window.keyboard_events.len(),
                    window.mouse_events.len()
                );
            }

            #[cfg(not(feature = "flux"))]
            println!(
                "[{}] Window completed: {} keyboard, {} mouse events",
                window.end.format("%H:%M:%S"),
                window.keyboard_events.len(),
                window.mouse_events.len()
            );

            snapshots.push(snapshot.clone());

            // Add to gateway sync buffer
            #[cfg(feature = "gateway")]
            if gateway_client.is_some() {
                pending_sync_snapshots.push(snapshot);
            }
        }

        // Sync to gateway if enabled and interval has passed
        #[cfg(feature = "gateway")]
        if let Some(ref client) = gateway_client {
            if last_gateway_sync.elapsed() >= Duration::from_secs(sync_interval)
                && !pending_sync_snapshots.is_empty()
            {
                match client.sync_snapshots(&pending_sync_snapshots, &session_id) {
                    Ok(response) => {
                        if let Some(state) = response.state {
                            println!(
                                "[Gateway] Synced {} snapshots | HSI: {}",
                                pending_sync_snapshots.len(),
                                state
                            );
                        } else {
                            println!(
                                "[Gateway] Synced {} snapshots",
                                pending_sync_snapshots.len()
                            );
                        }
                        pending_sync_snapshots.clear();
                    }
                    Err(e) => {
                        eprintln!("[Gateway] Sync failed: {e}");
                        // Keep snapshots for retry
                    }
                }
                last_gateway_sync = std::time::Instant::now();
            }
        }
    }

    // Final gateway sync before exit
    #[cfg(feature = "gateway")]
    if let Some(ref client) = gateway_client {
        if !pending_sync_snapshots.is_empty() {
            println!(
                "Syncing remaining {} snapshots to gateway...",
                pending_sync_snapshots.len()
            );
            match client.sync_snapshots(&pending_sync_snapshots, &session_id) {
                Ok(response) => {
                    if let Some(state) = response.state {
                        println!("[Gateway] Final sync complete | HSI: {state}");
                    } else {
                        println!("[Gateway] Final sync complete");
                    }
                }
                Err(e) => {
                    eprintln!("[Gateway] Final sync failed: {e}");
                }
            }
        }
    }

    // Stop collection
    println!();
    println!("Stopping collection...");
    collector.stop();

    // Flush remaining window
    window_manager.flush();
    for window in window_manager.take_completed_windows() {
        let features = compute_features(&window);
        let snapshot = hsi_builder.build(&window, &features);
        transparency_log.record_window_completed();
        snapshots.push(snapshot);
    }

    // Save transparency log
    if let Err(e) = transparency_log.save() {
        eprintln!("Warning: Could not save transparency log: {e}");
    }

    // Export snapshots
    if !snapshots.is_empty() {
        let export_path = config.export_path.join(format!(
            "session_{}.json",
            Utc::now().format("%Y%m%d_%H%M%S")
        ));

        if let Some(parent) = export_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }

        match serde_json::to_string_pretty(&snapshots) {
            Ok(json) => {
                if let Err(e) = std::fs::write(&export_path, json) {
                    eprintln!("Error writing snapshots: {e}");
                } else {
                    println!(
                        "Exported {} snapshots to {:?}",
                        snapshots.len(),
                        export_path
                    );
                    for _ in &snapshots {
                        transparency_log.record_snapshot_exported();
                    }
                }
            }
            Err(e) => {
                eprintln!("Error serializing snapshots: {e}");
            }
        }
    }

    // Export enriched snapshots if flux was enabled
    #[cfg(feature = "flux")]
    if !enriched_snapshots.is_empty() {
        let enriched_path = config.export_path.join(format!(
            "session_{}_enriched.json",
            Utc::now().format("%Y%m%d_%H%M%S")
        ));

        if let Some(parent) = enriched_path.parent() {
            let _ = std::fs::create_dir_all(parent);
        }

        match serde_json::to_string_pretty(&enriched_snapshots) {
            Ok(json) => {
                if let Err(e) = std::fs::write(&enriched_path, json) {
                    eprintln!("Error writing enriched snapshots: {e}");
                } else {
                    println!(
                        "Exported {} enriched snapshots to {:?}",
                        enriched_snapshots.len(),
                        enriched_path
                    );
                }
            }
            Err(e) => {
                eprintln!("Error serializing enriched snapshots: {e}");
            }
        }

        // Save baselines for next session
        if let Some(ref processor) = flux_processor {
            let baselines_path = config.data_path.join("flux_baselines.json");
            match processor.save_baselines() {
                Ok(baselines_json) => {
                    if let Err(e) = std::fs::write(&baselines_path, baselines_json) {
                        eprintln!("Error saving baselines: {e}");
                    } else {
                        println!("Saved baselines to {baselines_path:?}");
                    }
                }
                Err(e) => {
                    eprintln!("Error serializing baselines: {e}");
                }
            }
        }
    }

    // Final stats
    println!();
    println!("{}", transparency_log.summary());
}

/// Start HTTP server for receiving behavioral data from Chrome extension
#[cfg(feature = "server")]
fn cmd_serve(port: u16, gateway_host: &str, gateway_port: u16, gateway_token: &str) {
    use synheart_sensor_agent::gateway::GatewayConfig;
    use synheart_sensor_agent::server::ServerConfig;

    println!("Synheart Sensor Agent v{VERSION}");
    println!();
    println!("Starting HTTP server for Chrome extension...");
    println!("  Listen port: {port}");
    println!("  Gateway: {gateway_host}:{gateway_port}");
    println!();

    // Load config for state directory
    let config = Config::load().unwrap_or_default();
    if let Err(e) = config.ensure_directories() {
        eprintln!("Warning: Could not create directories: {e}");
    }

    // Create server config
    let gateway_config = GatewayConfig::new(gateway_host, gateway_port, gateway_token.to_string());
    let server_config = ServerConfig::new(port, gateway_config, config.data_path.clone());

    // Set up runtime
    let rt = tokio::runtime::Runtime::new().expect("Failed to create Tokio runtime");

    // Set up Ctrl+C handler
    let running = Arc::new(AtomicBool::new(true));
    let r = running.clone();
    ctrlc_handler(r);

    rt.block_on(async {
        // Initialize tracing
        tracing_subscriber::fmt()
            .with_env_filter(
                tracing_subscriber::EnvFilter::from_default_env()
                    .add_directive(tracing::Level::INFO.into()),
            )
            .init();

        match synheart_sensor_agent::server::run(server_config).await {
            Ok((addr, shutdown_tx)) => {
                println!("Server listening on http://{addr}");
                println!();
                println!("Chrome extension should POST to: http://{addr}/ingest");
                println!();
                println!("Press Ctrl+C to stop");
                println!();

                // Wait for Ctrl+C
                while running.load(Ordering::SeqCst) {
                    tokio::time::sleep(std::time::Duration::from_millis(100)).await;
                }

                println!();
                println!("Shutting down server...");
                let _ = shutdown_tx.send(());
            }
            Err(e) => {
                eprintln!("Failed to start server: {e}");
                std::process::exit(1);
            }
        }
    });
}

fn cmd_pause() {
    let mut config = Config::load().unwrap_or_default();
    config.paused = true;
    if let Err(e) = config.save() {
        eprintln!("Error saving config: {e}");
        std::process::exit(1);
    }
    println!("Collection paused. Use 'synheart-sensor resume' to continue.");
}

fn cmd_resume() {
    let mut config = Config::load().unwrap_or_default();
    config.paused = false;
    if let Err(e) = config.save() {
        eprintln!("Error saving config: {e}");
        std::process::exit(1);
    }
    println!("Collection resumed.");
}

fn cmd_status() {
    let config = Config::load().unwrap_or_default();

    println!("Synheart Sensor Agent Status");
    println!("============================");
    println!();

    // Check permission
    let has_permission = check_permission();
    println!(
        "Input Monitoring Permission: {}",
        if has_permission {
            "Granted ✓"
        } else {
            "Not Granted ✗"
        }
    );
    println!();

    // Show config
    println!("Configuration:");
    println!(
        "  Keyboard capture: {}",
        if config.sources.keyboard {
            "enabled"
        } else {
            "disabled"
        }
    );
    println!(
        "  Mouse capture: {}",
        if config.sources.mouse {
            "enabled"
        } else {
            "disabled"
        }
    );
    println!("  Window duration: {}s", config.window_duration.as_secs());
    println!("  Paused: {}", config.paused);
    println!();

    // Load and show transparency stats if available
    let stats_path = config.data_path.join("transparency.json");
    if stats_path.exists() {
        if let Ok(content) = std::fs::read_to_string(&stats_path) {
            if let Ok(stats) = serde_json::from_str::<serde_json::Value>(&content) {
                println!("Cumulative Statistics:");
                if let Some(kb) = stats.get("keyboard_events") {
                    println!("  Keyboard events: {kb}");
                }
                if let Some(mouse) = stats.get("mouse_events") {
                    println!("  Mouse events: {mouse}");
                }
                if let Some(windows) = stats.get("windows_completed") {
                    println!("  Windows completed: {windows}");
                }
                if let Some(snapshots) = stats.get("snapshots_exported") {
                    println!("  Snapshots exported: {snapshots}");
                }
            }
        }
    } else {
        println!("No previous session data found.");
    }
}

fn cmd_privacy() {
    println!("{PRIVACY_DECLARATION}");
}

fn cmd_export(output: Option<PathBuf>, format: &str) {
    let config = Config::load().unwrap_or_default();
    let export_dir = output.unwrap_or(config.export_path.clone());

    // Find all session files
    let session_files: Vec<PathBuf> = std::fs::read_dir(&export_dir)
        .map(|entries| {
            entries
                .filter_map(|e| e.ok())
                .map(|e| e.path())
                .filter(|p| p.extension().map(|e| e == "json").unwrap_or(false))
                .collect()
        })
        .unwrap_or_default();

    if session_files.is_empty() {
        println!("No session data found in {export_dir:?}");
        println!("Run 'synheart-sensor start' to begin collecting data.");
        return;
    }

    println!(
        "Found {} session file(s) in {:?}",
        session_files.len(),
        export_dir
    );

    // Combine all snapshots
    let mut all_snapshots: Vec<HsiSnapshot> = Vec::new();
    for file in &session_files {
        if let Ok(content) = std::fs::read_to_string(file) {
            if let Ok(snapshots) = serde_json::from_str::<Vec<HsiSnapshot>>(&content) {
                all_snapshots.extend(snapshots);
            }
        }
    }

    println!("Total snapshots: {}", all_snapshots.len());

    // Export based on format
    let output_path = export_dir.join(format!(
        "export_{}.{}",
        Utc::now().format("%Y%m%d_%H%M%S"),
        if format == "jsonl" { "jsonl" } else { "json" }
    ));

    let result = if format == "jsonl" {
        // JSON Lines format
        let lines: Vec<String> = all_snapshots
            .iter()
            .filter_map(|s| serde_json::to_string(s).ok())
            .collect();
        std::fs::write(&output_path, lines.join("\n"))
    } else {
        // Pretty JSON format
        match serde_json::to_string_pretty(&all_snapshots) {
            Ok(json) => std::fs::write(&output_path, json),
            Err(e) => {
                eprintln!("Error serializing: {e}");
                return;
            }
        }
    };

    match result {
        Ok(_) => println!("Exported to {output_path:?}"),
        Err(e) => eprintln!("Error writing export: {e}"),
    }
}

fn cmd_config() {
    let config = Config::load().unwrap_or_default();

    println!("Configuration");
    println!("=============");
    println!();
    println!("Config file: {:?}", Config::config_path());
    println!();
    println!(
        "{}",
        serde_json::to_string_pretty(&config).unwrap_or_else(|_| "Error".to_string())
    );
}

/// Set up Ctrl+C handler.
fn ctrlc_handler(running: Arc<AtomicBool>) {
    ctrlc::set_handler(move || {
        running.store(false, Ordering::SeqCst);
    })
    .expect("Error setting Ctrl+C handler");
}

/// Create gateway client from CLI args or runtime directory.
#[cfg(feature = "gateway")]
fn create_gateway_client(
    port: Option<u16>,
    token: Option<String>,
) -> Result<BlockingGatewayClient, synheart_sensor_agent::GatewayError> {
    // If both port and token are provided, use them directly
    if let (Some(p), Some(t)) = (port, token.clone()) {
        let config = GatewayConfig::new("127.0.0.1", p, t);
        return BlockingGatewayClient::new(config);
    }

    // Try to load from runtime directory
    match BlockingGatewayClient::from_runtime() {
        Ok(client) => Ok(client),
        Err(e) => {
            // If partial args provided, try to fill in the gaps
            if port.is_some() || token.is_some() {
                eprintln!("Warning: Partial gateway config provided, trying runtime directory...");
            }
            Err(e)
        }
    }
}