complior-cli 1.0.1

AI Act Compliance Scanner & Fixer — CLI
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
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
use tokio::sync::mpsc;

use super::{App, AppCommand};
use crate::components;
use crate::config;
use crate::headless::common::url_encode;
use crate::session;
use crate::types;
use crate::views;
use crate::watcher;

impl App {
    /// Extract the project path and name from the first loaded passport.
    /// Returns `None` if no passport is loaded.
    fn passport_path_name(&self) -> Option<(String, String)> {
        let passport = self
            .passport_view
            .loaded_passports
            .get(self.passport_view.selected_passport)?;
        let name = passport
            .get("name")
            .and_then(|v| v.as_str())
            .unwrap_or("unknown")
            .to_string();
        let path = self.project_path.to_string_lossy().to_string();
        Some((path, name))
    }
}

#[allow(clippy::too_many_lines)]
pub async fn execute_command(
    app: &mut App,
    cmd: AppCommand,
    watch_tx: &mpsc::UnboundedSender<std::path::PathBuf>,
    watch_handle: &mut Option<tokio::task::JoinHandle<()>>,
) {
    match cmd {
        AppCommand::ToggleWatch => {
            if app.watch_active {
                // Stop watcher
                if let Some(handle) = watch_handle.take() {
                    handle.abort();
                }
                app.watch_active = false;
                app.mode = types::Mode::Scan;
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    "Watch mode stopped.".to_string(),
                ));
            } else {
                // Start watcher
                *watch_handle = Some(watcher::spawn_watcher(
                    app.project_path.clone(),
                    watch_tx.clone(),
                ));
                app.watch_active = true;
                app.mode = types::Mode::Watch;
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    "Watch mode started. Editing files will trigger auto-scan.".to_string(),
                ));
            }
        }
        AppCommand::AutoScan => {
            // Save previous score for regression detection
            let prev_score = app.last_scan.as_ref().map(|s| s.score.total_score);
            app.watch_last_score = prev_score;

            // T904: Check if this auto-scan is a fix validation
            let is_fix_validation = app.pre_fix_score.is_some();
            let fix_old_score = app.pre_fix_score.take();

            let path = app.project_path.to_string_lossy().to_string();
            match app.engine_client.scan(&path).await {
                Ok(result) => {
                    let new_score = result.score.total_score;
                    app.set_scan_result(result);

                    if is_fix_validation {
                        // T904: Fix validation — show delta toast
                        if let Some(old) = fix_old_score {
                            let diff = new_score - old;
                            let msg = format!(
                                "Fix verified: Score {old:.0}{new_score:.0} ({diff:+.0})"
                            );
                            if diff > 0.0 {
                                app.toasts.push(components::toast::ToastKind::Success, &msg);
                            } else {
                                app.toasts.push(components::toast::ToastKind::Warning, &msg);
                            }
                            app.messages
                                .push(types::ChatMessage::new(types::MessageRole::System, msg));
                        }
                    } else {
                        // Regular watch-mode regression detection
                        if let Some(old) = prev_score {
                            let diff = new_score - old;
                            if diff < -5.0 {
                                app.messages.push(types::ChatMessage::new(
                                    types::MessageRole::System,
                                    format!(
                                        "REGRESSION: Score dropped {old:.0}{new_score:.0} ({diff:+.0})"
                                    ),
                                ));
                            } else if diff > 0.0 {
                                app.messages.push(types::ChatMessage::new(
                                    types::MessageRole::System,
                                    format!(
                                        "IMPROVED: Score {old:.0}{new_score:.0} ({diff:+.0})"
                                    ),
                                ));
                            }
                        }
                    }
                }
                Err(e) => {
                    if is_fix_validation {
                        app.toasts.push(
                            components::toast::ToastKind::Warning,
                            "Re-scan failed after fix. Run /scan manually.",
                        );
                    }
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        format!("Auto-scan failed: {e}"),
                    ));
                }
            }
        }
        AppCommand::Scan => {
            let path = app.project_path.to_string_lossy().to_string();
            match app.engine_client.scan(&path).await {
                Ok(result) => app.set_scan_result(result),
                Err(e) => {
                    let msg = format!("Scan failed: {e}");
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        msg.clone(),
                    ));
                    app.scan_view.scanning = false;
                    app.scan_view.scan_error = Some(msg);
                    app.operation_start = None;
                }
            }
        }
        AppCommand::OpenFile(path) => match app.engine_client.read_file(&path).await {
            Ok(content) => app.open_file(&path, content),
            Err(_) => {
                // Fallback: try reading locally
                match tokio::fs::read_to_string(&path).await {
                    Ok(content) => app.open_file(&path, content),
                    Err(e) => {
                        app.messages.push(types::ChatMessage::new(
                            types::MessageRole::System,
                            format!("Cannot open file: {e}"),
                        ));
                    }
                }
            }
        },
        AppCommand::RunCommand(command) => {
            app.add_terminal_line(format!("$ {command}"));
            match app.engine_client.run_command(&command).await {
                Ok(output) => {
                    for line in output.lines() {
                        app.add_terminal_line(line.to_string());
                    }
                }
                Err(e) => {
                    app.add_terminal_line(format!("Error: {e}"));
                }
            }
        }
        AppCommand::Reconnect => {
            app.messages.push(types::ChatMessage::new(
                types::MessageRole::System,
                "Reconnecting to engine...".to_string(),
            ));
            match app.engine_client.status().await {
                Ok(status) if status.ready => {
                    app.engine_status = types::EngineConnectionStatus::Connected;
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        "Reconnected successfully.".to_string(),
                    ));
                }
                _ => {
                    app.engine_status = types::EngineConnectionStatus::Disconnected;
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        "Reconnect failed. Is engine running?".to_string(),
                    ));
                }
            }
        }
        AppCommand::SwitchTheme(name) => {
            crate::theme::init_theme(&name);
            app.messages.push(types::ChatMessage::new(
                types::MessageRole::System,
                format!("Theme switched to: {name}"),
            ));
        }
        AppCommand::SaveSession(name) => {
            let data = app.to_session_data();
            match session::save_session(&data, &name).await {
                Ok(()) => {
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        format!("Session saved: {name}"),
                    ));
                }
                Err(e) => {
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        format!("Save failed: {e}"),
                    ));
                }
            }
        }
        AppCommand::LoadSession(name) => match session::load_session(&name).await {
            Ok(data) => {
                app.load_session_data(data);
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    format!("Session loaded: {name}"),
                ));
            }
            Err(e) => {
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    format!("Load failed: {e}"),
                ));
            }
        },
        AppCommand::Undo(id) => match app.engine_client.undo(id).await {
            Ok(result) => {
                let msg = result
                    .get("message")
                    .and_then(|v| v.as_str())
                    .unwrap_or("Undo applied");
                app.toasts
                    .push(components::toast::ToastKind::Success, msg.to_string());
                app.push_activity(types::ActivityKind::Fix, "Undo");
                app.animation.start_checkmark();
            }
            Err(_) => {
                app.toasts
                    .push(components::toast::ToastKind::Warning, "Nothing to undo");
            }
        },
        AppCommand::FetchUndoHistory => match app.engine_client.undo_history().await {
            Ok(entries) => {
                app.undo_history.entries = entries
                    .iter()
                    .filter_map(|v| {
                        Some(components::undo_history::UndoEntry {
                            id: v.get("id")?.as_u64()? as u32,
                            timestamp: v
                                .get("timestamp")
                                .and_then(|t| t.as_str())
                                .unwrap_or("")
                                .to_string(),
                            action: v
                                .get("action")
                                .and_then(|a| a.as_str())
                                .unwrap_or("")
                                .to_string(),
                            status: match v
                                .get("status")
                                .and_then(|s| s.as_str())
                                .unwrap_or("applied")
                            {
                                "undone" => components::undo_history::UndoStatus::Undone,
                                "baseline" => components::undo_history::UndoStatus::Baseline,
                                _ => components::undo_history::UndoStatus::Applied,
                            },
                            score_delta: v.get("scoreDelta").and_then(serde_json::Value::as_f64),
                        })
                    })
                    .collect();
                app.undo_history.selected = 0;
            }
            Err(_) => {
                app.undo_history.entries.clear();
            }
        },
        AppCommand::FetchSuggestions => {
            app.idle_suggestions.fetch_pending = false;
            match app.engine_client.suggestions().await {
                Ok(items) if !items.is_empty() => {
                    if let Some(first) = items.first() {
                        let kind_str = first.get("kind").and_then(|k| k.as_str()).unwrap_or("tip");
                        let kind = match kind_str {
                            "fix" => components::suggestions::SuggestionKind::Fix,
                            "deadline" => components::suggestions::SuggestionKind::DeadlineWarning,
                            "score" => components::suggestions::SuggestionKind::ScoreImprovement,
                            "new" => components::suggestions::SuggestionKind::NewFeature,
                            _ => components::suggestions::SuggestionKind::Tip,
                        };
                        app.idle_suggestions.current = Some(components::suggestions::Suggestion {
                            kind,
                            text: first
                                .get("text")
                                .and_then(|t| t.as_str())
                                .unwrap_or("")
                                .to_string(),
                            detail: first
                                .get("detail")
                                .and_then(|d| d.as_str())
                                .map(String::from),
                        });
                    }
                }
                _ => {
                    // Engine doesn't have /suggestions or returned empty — use local context
                    let suggestion = build_local_suggestion(app);
                    app.idle_suggestions.current = Some(suggestion);
                }
            }
        }
        // T905: What-If scenario analysis
        AppCommand::WhatIf(scenario) => {
            app.whatif.pending = true;
            let current_score = app.last_scan.as_ref().map_or(50.0, |s| s.score.total_score);

            if let Ok(result) = app.engine_client.whatif(&scenario).await {
                // Parse engine response
                let projected = result
                    .get("projectedScore")
                    .and_then(serde_json::Value::as_f64)
                    .unwrap_or(current_score - 5.0);
                let obligations: Vec<String> = result
                    .get("newObligations")
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| v.as_str().map(String::from))
                            .collect()
                    })
                    .unwrap_or_default();
                let effort = result
                    .get("effortDays")
                    .and_then(serde_json::Value::as_u64)
                    .map(|d| d as u32);

                let whatif_result = components::whatif::WhatIfResult {
                    scenario: scenario.clone(),
                    current_score,
                    projected_score: projected,
                    new_obligations: obligations,
                    effort_days: effort,
                };
                let msg = components::whatif::format_whatif_message(&whatif_result);
                app.whatif.result = Some(whatif_result);
                app.messages
                    .push(types::ChatMessage::new(types::MessageRole::Assistant, msg));
            } else {
                app.toasts.push(
                    components::toast::ToastKind::Warning,
                    "What-if requires engine connection",
                );
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    "What-if analysis unavailable — engine not connected.".to_string(),
                ));
            }
            app.whatif.pending = false;
        }
        // T906: Dry-run mode
        AppCommand::FixDryRun(selected) => {
            let known_score = app.last_scan.as_ref().map_or(0.0, |s| s.score.total_score);
            if let Ok(result) = app.engine_client.fix_dry_run(known_score).await {
                // Parse dry-run response
                let changes: Vec<String> = result
                    .get("changes")
                    .and_then(|v| v.as_array())
                    .map(|arr| {
                        arr.iter()
                            .filter_map(|v| {
                                let path = v.get("path")?.as_str()?;
                                let action = v.get("action")?.as_str().unwrap_or("MODIFY");
                                let delta = v.get("scoreDelta")?.as_f64().unwrap_or(0.0);
                                Some(format!("  {path:<40} [{action}]  +{delta:.0} score"))
                            })
                            .collect()
                    })
                    .unwrap_or_default();
                let predicted = result
                    .get("predictedScore")
                    .and_then(serde_json::Value::as_f64)
                    .unwrap_or(0.0);

                let current = app.last_scan.as_ref().map_or(0.0, |s| s.score.total_score);
                let delta = predicted - current;
                let mut msg = format!(
                    "Dry-Run Fix Analysis (no files modified)\n\
                     Would modify {} files:\n",
                    changes.len()
                );
                for change in &changes {
                    msg.push_str(change);
                    msg.push('\n');
                }
                msg.push_str(&format!(
                    "\nPredicted score: {current:.0} -> {predicted:.0} ({delta:+.0})\n\
                     Run /fix to apply."
                ));

                app.messages
                    .push(types::ChatMessage::new(types::MessageRole::Assistant, msg));
            } else {
                // Offline: simulate from predicted impact
                let current = app.last_scan.as_ref().map_or(0.0, |s| s.score.total_score);
                let impact = f64::from(app.fix_view.total_predicted_impact());
                let predicted = (current + impact).min(100.0);
                let msg = format!(
                    "Dry-Run Fix Analysis (offline estimate)\n\
                     Selected fixes: {}\n\
                     Predicted score: {current:.0} -> {predicted:.0} (+{impact:.0})\n\n\
                     Note: Detailed file changes unavailable offline.\n\
                     Run /fix to apply.",
                    selected.len()
                );
                app.messages
                    .push(types::ChatMessage::new(types::MessageRole::Assistant, msg));
                app.toasts.push(
                    components::toast::ToastKind::Info,
                    "Dry-run estimate (offline)",
                );
            }
        }
        AppCommand::ApplyFixes => {
            use views::fix::{FixItemStatus, apply_fix_to_file};

            let old_score = app.last_scan.as_ref().map_or(0.0, |s| s.score.total_score);
            app.pre_fix_score = Some(old_score);

            let selected_indices: Vec<usize> = app
                .fix_view
                .fixable_findings
                .iter()
                .enumerate()
                .filter(|(_, item)| item.selected)
                .map(|(i, _)| i)
                .collect();

            let mut applied: u32 = 0;
            let mut failed: u32 = 0;
            let mut details: Vec<String> = Vec::new();

            for idx in &selected_indices {
                let finding_index = app.fix_view.fixable_findings[*idx].finding_index;
                let finding = app
                    .last_scan
                    .as_ref()
                    .and_then(|s| s.findings.get(finding_index))
                    .cloned();

                if let Some(f) = finding {
                    let result = apply_fix_to_file(&app.project_path, &f);
                    if result.success {
                        app.fix_view.fixable_findings[*idx].status = FixItemStatus::Applied;
                        applied += 1;
                    } else {
                        app.fix_view.fixable_findings[*idx].status = FixItemStatus::Failed;
                        failed += 1;
                    }
                    details.push(result.detail);
                } else {
                    app.fix_view.fixable_findings[*idx].status = FixItemStatus::Failed;
                    failed += 1;
                    details.push("Finding not found in scan".to_string());
                }
            }

            app.fix_view.applying = false;

            // Log details
            for d in &details {
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    d.clone(),
                ));
            }

            // Show results (predicted score — will be updated by AutoScan)
            let impact = f64::from(app.fix_view.total_predicted_impact());
            app.fix_view.results = Some(views::fix::FixResults {
                applied,
                failed,
                old_score,
                new_score: (old_score + impact).min(100.0),
            });

            if applied > 0 {
                app.toasts.push(
                    components::toast::ToastKind::Success,
                    format!("{applied} fix(es) applied to disk. Re-scanning..."),
                );
                let now = std::time::SystemTime::now()
                    .duration_since(std::time::UNIX_EPOCH)
                    .unwrap_or_default()
                    .as_secs();
                app.activity_log.push(types::ActivityEntry {
                    timestamp: format!("{:02}:{:02}", (now % 86400) / 3600, (now % 3600) / 60),
                    kind: types::ActivityKind::Fix,
                    detail: format!("{applied} applied, {failed} failed"),
                });
                // Auto-rescan to validate actual score (inline to avoid recursion)
                let path = app.project_path.to_string_lossy().to_string();
                let fix_old_score = app.pre_fix_score.take();
                match app.engine_client.scan(&path).await {
                    Ok(result) => {
                        let new_score = result.score.total_score;
                        app.set_scan_result(result);
                        // Update fix results with real score
                        if let Some(ref mut r) = app.fix_view.results {
                            r.new_score = new_score;
                        }
                        if let Some(old) = fix_old_score {
                            let diff = new_score - old;
                            let msg = format!(
                                "Fix verified: Score {old:.0}{new_score:.0} ({diff:+.0})"
                            );
                            app.toasts.push(
                                if diff > 0.0 {
                                    components::toast::ToastKind::Success
                                } else {
                                    components::toast::ToastKind::Warning
                                },
                                &msg,
                            );
                        }
                    }
                    Err(e) => {
                        app.toasts.push(
                            components::toast::ToastKind::Warning,
                            "Re-scan failed after fix. Run /scan manually.",
                        );
                        app.messages.push(types::ChatMessage::new(
                            types::MessageRole::System,
                            format!("Re-scan failed: {e}"),
                        ));
                    }
                }
            } else {
                app.toasts.push(
                    components::toast::ToastKind::Warning,
                    format!("No fixes applied. {failed} failed."),
                );
            }
        }
        AppCommand::SaveTheme(name) => {
            config::save_theme(&name).await;
        }
        AppCommand::MarkOnboardingComplete => {
            config::mark_onboarding_complete().await;
        }
        AppCommand::MarkFirstRunDone => {
            session::mark_first_run_done().await;
        }
        AppCommand::ListSessions => {
            let sessions = session::list_sessions().await;
            if sessions.is_empty() {
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    "No saved sessions.".to_string(),
                ));
            } else {
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    format!("Sessions: {}", sessions.join(", ")),
                ));
            }
        }
        AppCommand::ExportReport => {
            if let Some(scan) = &app.last_scan {
                match views::report::export_report(scan).await {
                    Ok(path) => {
                        app.report_view.export_status =
                            views::report::ExportStatus::Done(path.clone());
                        app.toasts.push(
                            components::toast::ToastKind::Success,
                            format!("Exported: {path}"),
                        );
                        app.messages.push(types::ChatMessage::new(
                            types::MessageRole::System,
                            format!("Report exported: {path}"),
                        ));
                    }
                    Err(e) => {
                        app.report_view.export_status =
                            views::report::ExportStatus::Error(e.clone());
                        app.toasts.push(
                            components::toast::ToastKind::Error,
                            format!("Export failed: {e}"),
                        );
                    }
                }
            }
        }
        AppCommand::CompleteOnboarding => {
            // 1. Save config from wizard
            if let Some(ref wiz) = app.onboarding {
                config::save_onboarding_results(wiz).await;
            }

            // 2. Collect project type for post-completion action
            let project_type = app
                .onboarding
                .as_ref()
                .and_then(|w| w.project_type.clone())
                .unwrap_or_else(|| "existing".to_string());

            // 3. Close wizard, reload config with newly saved values
            app.onboarding = None;
            app.overlay = types::Overlay::None;
            let fresh = config::load_config();
            app.llm_config = types::LlmSessionConfig {
                provider: fresh.llm_provider.clone(),
                model: fresh.llm_model.clone(),
                api_key: fresh
                    .llm_provider
                    .as_deref()
                    .and_then(config::load_llm_api_key),
            };
            app.config = fresh;
            app.config.onboarding_completed = true;

            app.toasts.push(
                components::toast::ToastKind::Info,
                "Setup complete! Use /scan to check compliance.",
            );

            // 4. Post-completion action based on project_type
            match project_type.as_str() {
                "existing" => {
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        "Running first scan...".to_string(),
                    ));
                    // Trigger scan directly (avoid recursive execute_command)
                    let path = app.project_path.to_string_lossy().to_string();
                    match app.engine_client.scan(&path).await {
                        Ok(result) => app.set_scan_result(result),
                        Err(e) => {
                            app.messages.push(types::ChatMessage::new(
                                types::MessageRole::System,
                                format!("First scan failed: {e}. Use /scan to retry."),
                            ));
                        }
                    }
                }
                "new" => {
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        "Compliance structure created. Use /scan when you add AI tools."
                            .to_string(),
                    ));
                }
                "demo" => {
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        "Run `complior init` in your project, then /scan to check compliance."
                            .to_string(),
                    ));
                }
                _ => {}
            }
        }
        AppCommand::SaveOnboardingPartial(last_step) => {
            config::save_onboarding_partial(last_step).await;
        }
        AppCommand::LoadPassports => {
            app.passport_view.passport_loading = true;
            app.passport_view.passport_error = None;
            let path = app.project_path.to_string_lossy().to_string();
            let url = format!("/passport/list?path={}", url_encode(&path));
            let client = app.engine_client.clone();
            let tx = app.bg_tx.clone();
            tokio::spawn(async move {
                let result =
                    tokio::time::timeout(std::time::Duration::from_secs(15), client.get_json(&url))
                        .await;
                let mapped = match result {
                    Ok(inner) => inner.map_err(|e| e.to_string()),
                    Err(_) => Err("Loading timed out after 15s".to_string()),
                };
                let _ = tx.send(AppCommand::PassportsLoaded(mapped));
            });
        }
        AppCommand::PassportsLoaded(result) => {
            app.passport_view.passport_loading = false;
            match result {
                Ok(value) => {
                    if let Some(arr) = value.as_array() {
                        app.passport_view.loaded_passports = arr.clone();
                        app.passport_view.load_from_passports();
                        let count = arr.len();
                        if count > 0 {
                            app.messages.push(types::ChatMessage::new(
                                types::MessageRole::System,
                                format!("Loaded {count} passport(s) from engine."),
                            ));
                        }
                    }
                }
                Err(e) => {
                    let msg = format!("Failed to load passports: {e}");
                    app.passport_view.passport_error = Some(msg.clone());
                    app.messages
                        .push(types::ChatMessage::new(types::MessageRole::System, msg));
                }
            }
        }
        AppCommand::LoadPassportCompleteness => {
            if let Some((path, name)) = app.passport_path_name() {
                let url = format!(
                    "/passport/completeness?path={}&name={}",
                    url_encode(&path),
                    url_encode(&name)
                );
                match app.engine_client.get_json(&url).await {
                    Ok(result) => {
                        app.passport_view.completeness_data = Some(result);
                    }
                    Err(e) => {
                        app.messages.push(types::ChatMessage::new(
                            types::MessageRole::System,
                            format!("Failed to load completeness: {e}"),
                        ));
                    }
                }
            }
        }
        AppCommand::ValidatePassport => {
            if let Some((path, name)) = app.passport_path_name() {
                let url = format!(
                    "/passport/validate?path={}&name={}",
                    url_encode(&path),
                    url_encode(&name)
                );
                match app.engine_client.get_json(&url).await {
                    Ok(result) => {
                        let valid = result
                            .get("valid")
                            .and_then(serde_json::Value::as_bool)
                            .unwrap_or(false);
                        let msg = if valid {
                            format!("Passport '{name}' is valid.")
                        } else {
                            let errors = result
                                .get("errors")
                                .and_then(|v| v.as_array())
                                .map(|arr| {
                                    arr.iter()
                                        .filter_map(|v| v.as_str())
                                        .collect::<Vec<_>>()
                                        .join(", ")
                                })
                                .unwrap_or_default();
                            format!("Passport '{name}' validation failed: {errors}")
                        };
                        app.toasts.push(
                            if valid {
                                components::toast::ToastKind::Success
                            } else {
                                components::toast::ToastKind::Warning
                            },
                            &msg,
                        );
                        app.messages
                            .push(types::ChatMessage::new(types::MessageRole::System, msg));
                    }
                    Err(e) => {
                        app.toasts.push(
                            components::toast::ToastKind::Warning,
                            format!("Validation failed: {e}"),
                        );
                    }
                }
            } else {
                app.toasts.push(
                    components::toast::ToastKind::Warning,
                    "No passport loaded. Run `complior agent init` first.",
                );
            }
        }
        AppCommand::GeneratePassportFria => {
            if let Some((path, name)) = app.passport_path_name() {
                let body = serde_json::json!({ "path": path, "name": name });
                match app.engine_client.post_json("/fix/doc/fria", &body).await {
                    Ok(result) => {
                        let output_path = result
                            .get("savedPath")
                            .and_then(|v| v.as_str())
                            .unwrap_or("(unknown)");
                        let msg = format!("FRIA report generated: {output_path}");
                        app.toasts.push(components::toast::ToastKind::Success, &msg);
                        app.messages
                            .push(types::ChatMessage::new(types::MessageRole::System, msg));
                    }
                    Err(e) => {
                        app.toasts.push(
                            components::toast::ToastKind::Warning,
                            format!("FRIA generation failed: {e}"),
                        );
                    }
                }
            } else {
                app.toasts.push(
                    components::toast::ToastKind::Warning,
                    "No passport loaded. Run `complior agent init` first.",
                );
            }
        }
        AppCommand::ExportPassport => {
            if let Some((path, name)) = app.passport_path_name() {
                let url = format!(
                    "/passport/show?path={}&name={}",
                    url_encode(&path),
                    url_encode(&name)
                );
                match app.engine_client.get_json(&url).await {
                    Ok(result) => {
                        let json_str = serde_json::to_string_pretty(&result).unwrap_or_default();
                        let export_path = format!("{name}-passport-export.json");
                        match tokio::fs::write(&export_path, &json_str).await {
                            Ok(()) => {
                                let msg = format!("Passport exported: {export_path}");
                                app.toasts.push(components::toast::ToastKind::Success, &msg);
                                app.messages
                                    .push(types::ChatMessage::new(types::MessageRole::System, msg));
                            }
                            Err(e) => {
                                app.toasts.push(
                                    components::toast::ToastKind::Warning,
                                    format!("Export failed: {e}"),
                                );
                            }
                        }
                    }
                    Err(e) => {
                        app.toasts.push(
                            components::toast::ToastKind::Warning,
                            format!("Export failed: {e}"),
                        );
                    }
                }
            } else {
                app.toasts.push(
                    components::toast::ToastKind::Warning,
                    "No passport loaded. Run `complior agent init` first.",
                );
            }
        }
        AppCommand::LoadObligations => match app.engine_client.get_json("/obligations").await {
            Ok(result) => {
                if let Some(arr) = result.as_array() {
                    app.obligations_view.load_from_json(arr);
                    let count = arr.len();
                    let covered = app.obligations_view.covered_count();
                    if count > 0 {
                        app.messages.push(types::ChatMessage::new(
                            types::MessageRole::System,
                            format!("Loaded {count} obligations ({covered} covered)."),
                        ));
                    }
                }
            }
            Err(e) => {
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    format!("Failed to load obligations: {e}"),
                ));
            }
        },
        AppCommand::LoadRegistry => {
            if app.passport_view.registry_loading {
                return; // Already loading, skip duplicate request
            }
            app.passport_view.registry_loading = true;
            let path = app.project_path.to_string_lossy().to_string();
            let url = format!("/passport/registry?path={}", url_encode(&path));
            let client = app.engine_client.clone();
            let tx = app.bg_tx.clone();
            tokio::spawn(async move {
                let result =
                    tokio::time::timeout(std::time::Duration::from_secs(15), client.get_json(&url))
                        .await;
                let mapped = match result {
                    Ok(inner) => inner.map_err(|e| e.to_string()),
                    Err(_) => Err("Registry load timed out".to_string()),
                };
                let _ = tx.send(AppCommand::RegistryLoaded(mapped));
            });
        }
        AppCommand::RegistryLoaded(result) => {
            app.passport_view.registry_loading = false;
            match result {
                Ok(value) => {
                    app.passport_view.registry_data = Some(value);
                }
                Err(e) => {
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        format!("Failed to load registry: {e}"),
                    ));
                }
            }
        }
        AppCommand::LoadAuditTrail => {
            if app.passport_view.audit_loading {
                return; // Already loading, skip duplicate request
            }
            app.passport_view.audit_loading = true;
            let path = app.project_path.to_string_lossy().to_string();
            let url = format!("/passport/audit?path={}&limit=50", url_encode(&path));
            let client = app.engine_client.clone();
            let tx = app.bg_tx.clone();
            tokio::spawn(async move {
                let result =
                    tokio::time::timeout(std::time::Duration::from_secs(15), client.get_json(&url))
                        .await;
                let mapped = match result {
                    Ok(Ok(val)) => {
                        if let Some(arr) = val.as_array() {
                            Ok(arr.clone())
                        } else {
                            Ok(vec![])
                        }
                    }
                    Ok(Err(e)) => Err(e.to_string()),
                    Err(_) => Err("Audit trail load timed out".to_string()),
                };
                let _ = tx.send(AppCommand::AuditTrailLoaded(mapped));
            });
        }
        AppCommand::AuditTrailLoaded(result) => {
            app.passport_view.audit_loading = false;
            match result {
                Ok(entries) => {
                    app.passport_view.audit_entries = Some(entries);
                }
                Err(e) => {
                    app.messages.push(types::ChatMessage::new(
                        types::MessageRole::System,
                        format!("Failed to load audit trail: {e}"),
                    ));
                }
            }
        }
        AppCommand::LoadFrameworkScores => {
            let client = app.engine_client.clone();
            let tx = app.bg_tx.clone();
            tokio::spawn(async move {
                let result = tokio::time::timeout(
                    std::time::Duration::from_secs(10),
                    client.framework_scores(),
                )
                .await;
                let mapped = match result {
                    Ok(inner) => inner.map_err(|e| e.to_string()),
                    Err(_) => Err("Framework scores load timed out".to_string()),
                };
                let _ = tx.send(AppCommand::FrameworkScoresLoaded(mapped));
            });
        }
        AppCommand::FrameworkScoresLoaded(result) => match result {
            Ok(scores) => {
                app.focused_framework = None;
                app.framework_scores = Some(scores);
            }
            Err(e) => {
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    format!("Failed to load framework scores: {e}"),
                ));
            }
        },
        AppCommand::LoadDashboardMetrics => {
            let client = app.engine_client.clone();
            let tx = app.bg_tx.clone();
            let project_path = app.project_path.to_string_lossy().to_string();
            // Use first loaded passport name, or "default"
            let agent_name = app
                .passport_view
                .loaded_passports
                .first()
                .and_then(|p| p.get("name"))
                .and_then(|v| v.as_str())
                .unwrap_or("default")
                .to_string();
            tokio::spawn(async move {
                let (cost_res, debt_res, readiness_res) = tokio::join!(
                    client.cost_estimate(),
                    client.debt_score(),
                    client.readiness_score(&agent_name, &project_path),
                );
                let _ = tx.send(AppCommand::DashboardMetricsLoaded {
                    cost: cost_res.map_err(|e| e.to_string()),
                    debt: debt_res.map_err(|e| e.to_string()),
                    readiness: readiness_res.map_err(|e| e.to_string()),
                });
            });
        }
        AppCommand::DashboardMetricsLoaded {
            cost,
            debt,
            readiness,
        } => {
            match cost {
                Ok(c) => app.cost_estimate = Some(c),
                Err(e) => {
                    tracing::debug!("Cost estimate unavailable: {e}");
                }
            }
            match debt {
                Ok(d) => app.debt_score = Some(d),
                Err(e) => {
                    tracing::debug!("Debt score unavailable: {e}");
                }
            }
            match readiness {
                Ok(r) => app.readiness_score = Some(r),
                Err(e) => {
                    tracing::debug!("Readiness score unavailable: {e}");
                }
            }
        }
        AppCommand::ChatSend(msg) => {
            // Push user message
            app.messages.push(types::ChatMessage::new(
                types::MessageRole::User,
                msg.clone(),
            ));
            app.streaming = types::StreamingState {
                partial_text: String::new(),
                blocks: Vec::new(),
                active: true,
                stream_start: Some(std::time::Instant::now()),
            };
            app.chat_auto_scroll = true;

            // Build request body
            let mut body = serde_json::json!({ "message": msg });
            if let Some(ref provider) = app.llm_config.provider {
                body["provider"] = serde_json::Value::String(provider.clone());
            }
            if let Some(ref model) = app.llm_config.model {
                body["model"] = serde_json::Value::String(model.clone());
            }
            if let Some(ref api_key) = app.llm_config.api_key {
                body["apiKey"] = serde_json::Value::String(api_key.clone());
            }

            let client = app.engine_client.clone();
            let tx = app.bg_tx.clone();
            let cancel = std::sync::Arc::new(tokio::sync::Notify::new());
            app.chat_cancel = Some(cancel.clone());

            tokio::spawn(async move {
                match client.post_stream("/chat", &body).await {
                    Ok(resp) => {
                        if crate::chat_stream::is_json_response(&resp) {
                            // Slash command response (JSON, not SSE)
                            let text = resp.text().await.unwrap_or_default();
                            if let Ok(val) = serde_json::from_str::<serde_json::Value>(&text) {
                                let display = format_slash_command_response(&val);
                                let _ = tx.send(AppCommand::ChatStreamDelta(display));
                            } else {
                                let _ = tx.send(AppCommand::ChatStreamDelta(text));
                            }
                            let _ = tx.send(AppCommand::ChatStreamDone);
                        } else {
                            crate::chat_stream::spawn_stream_reader(resp, tx, cancel);
                        }
                    }
                    Err(e) => {
                        let _ = tx.send(AppCommand::ChatStreamError(e.to_string()));
                    }
                }
            });
        }
        AppCommand::ChatStreamDelta(text) => {
            app.streaming.partial_text.push_str(&text);
            app.chat_auto_scroll = true;
        }
        AppCommand::ChatStreamBlock(block) => {
            app.streaming.blocks.push(block);
            app.chat_auto_scroll = true;
        }
        AppCommand::ChatStreamDone => {
            if app.streaming.active {
                let content = if app.streaming.partial_text.is_empty() {
                    "(no response)".to_string()
                } else {
                    std::mem::take(&mut app.streaming.partial_text)
                };
                let mut msg = types::ChatMessage::new(types::MessageRole::Assistant, content);
                msg.blocks = std::mem::take(&mut app.streaming.blocks);
                app.messages.push(msg);
                app.streaming.active = false;
                app.chat_cancel = None;
                app.chat_auto_scroll = true;
            }
        }
        AppCommand::ChatStreamError(err) => {
            app.streaming.active = false;
            app.chat_cancel = None;

            let err_lower = err.to_lowercase();
            let user_msg = if err_lower.contains("insufficient credits")
                || err_lower.contains("402")
                || err_lower.contains("payment required")
                || err_lower.contains("billing")
            {
                app.toasts.push(
                    components::toast::ToastKind::Warning,
                    "LLM provider: insufficient balance. Top up your account.",
                );
                "Insufficient balance on your LLM provider account. Please add credits and try again.".to_string()
            } else if err_lower.contains("401")
                || err_lower.contains("unauthorized")
                || err_lower.contains("invalid.*key")
            {
                app.toasts.push(
                    components::toast::ToastKind::Warning,
                    "LLM provider: invalid API key.",
                );
                "Invalid API key. Check your LLM provider settings (:llm).".to_string()
            } else if err_lower.contains("429") || err_lower.contains("rate limit") {
                app.toasts.push(
                    components::toast::ToastKind::Warning,
                    "Rate limited. Wait a moment and try again.",
                );
                "Rate limit exceeded. Wait a moment and try again.".to_string()
            } else {
                format!("LLM error: {err}")
            };

            app.messages.push(types::ChatMessage::new(
                types::MessageRole::System,
                user_msg,
            ));
            app.chat_auto_scroll = true;
        }
        AppCommand::ChatCancel => {
            if let Some(cancel) = app.chat_cancel.take() {
                cancel.notify_one();
            }
            if app.streaming.active {
                app.streaming.active = false;
                app.messages.push(types::ChatMessage::new(
                    types::MessageRole::System,
                    "Streaming cancelled.".to_string(),
                ));
            }
        }
        AppCommand::TestLlmConnection => {
            let mut body = serde_json::json!({ "message": "/cost" });
            if let Some(ref provider) = app.llm_config.provider {
                body["provider"] = serde_json::Value::String(provider.clone());
            }
            if let Some(ref api_key) = app.llm_config.api_key {
                body["apiKey"] = serde_json::Value::String(api_key.clone());
            }

            let client = app.engine_client.clone();
            let tx = app.bg_tx.clone();
            tokio::spawn(async move {
                let result = match client.post_json("/chat", &body).await {
                    Ok(_) => Ok("Connection successful".to_string()),
                    Err(e) => Err(e.to_string()),
                };
                let _ = tx.send(AppCommand::LlmConnectionTestResult(result));
            });
        }
        AppCommand::LlmConnectionTestResult(result) => {
            if let Some(ref mut settings) = app.llm_settings {
                settings.test_status = Some(result);
            }
        }
        AppCommand::SaveLlmSettings => {
            config::save_llm_config(
                app.llm_config.provider.as_deref(),
                app.llm_config.model.as_deref(),
                app.llm_config.api_key.as_deref(),
            )
            .await;
        }
    }
}

/// Format a JSON slash command response for display.
fn format_slash_command_response(val: &serde_json::Value) -> String {
    if let Some(cmd) = val.get("command").and_then(|v| v.as_str()) {
        match cmd {
            "mode" => {
                let label = val
                    .get("label")
                    .and_then(|v| v.as_str())
                    .unwrap_or("unknown");
                format!("Mode: {label}")
            }
            "cost" => {
                let cost = val
                    .get("totalCost")
                    .and_then(serde_json::Value::as_f64)
                    .unwrap_or(0.0);
                let tokens = val
                    .get("totalTokens")
                    .and_then(serde_json::Value::as_u64)
                    .unwrap_or(0);
                format!("Session cost: ${cost:.4}  ({tokens} tokens)")
            }
            "model" => val
                .get("message")
                .and_then(|v| v.as_str())
                .unwrap_or("Model updated")
                .to_string(),
            _ => serde_json::to_string_pretty(val).unwrap_or_default(),
        }
    } else {
        serde_json::to_string_pretty(val).unwrap_or_default()
    }
}

/// Build a context-aware suggestion from local app state when engine /suggestions is unavailable.
pub fn build_local_suggestion(app: &App) -> components::suggestions::Suggestion {
    use components::suggestions::{Suggestion, SuggestionKind};

    // Priority 1: If no scan yet, suggest scanning
    if app.last_scan.is_none() {
        return Suggestion {
            kind: SuggestionKind::Tip,
            text: "Try /scan to check your project's compliance score".into(),
            detail: Some("Press any key to dismiss".into()),
        };
    }

    let scan = app
        .last_scan
        .as_ref()
        .expect("last_scan: guarded by is_none check above");
    let score = scan.score.total_score;

    // Priority 2: Findings present — suggest fix
    let finding_count = scan.findings.len();
    if finding_count > 0 {
        return Suggestion {
            kind: SuggestionKind::Fix,
            text: format!(
                "Score {score:.0}/100. {finding_count} findings to fix — press 3 for Fix view"
            ),
            detail: Some("Quick wins can boost your score significantly".into()),
        };
    }

    // Priority 3: Deadline warning
    if score < 80.0 {
        return Suggestion {
            kind: SuggestionKind::DeadlineWarning,
            text: format!("Score {score:.0}/100 — EU AI Act full enforcement Aug 2, 2026"),
            detail: Some("Press 5 for Timeline view".into()),
        };
    }

    // Priority 4: High score celebration
    Suggestion {
        kind: SuggestionKind::ScoreImprovement,
        text: format!("Score {score:.0}/100 — Looking good! Run /scan to verify latest changes"),
        detail: None,
    }
}