ccboard-web 0.12.0

Web frontend for ccboard using Leptos + Axum
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
//! Web router using Axum

use axum::{
    body::Body,
    extract::Query,
    http::{header, StatusCode, Uri},
    response::{IntoResponse, Response},
    routing::get,
    Router,
};
use ccboard_core::AlertSeverity;
use ccboard_core::DataStore;
use mime_guess::from_path;
use rust_embed::RustEmbed;
use serde::Deserialize;
use std::sync::Arc;
use tower_http::cors::{Any, CorsLayer};

/// Embedded WASM frontend assets compiled with trunk.
/// In debug builds, rust-embed reads from disk (trunk must have been run).
/// In release builds, all files are embedded into the binary at compile time.
#[derive(RustEmbed)]
#[folder = "dist/"]
struct DistAssets;

use crate::sse;

/// Query parameters for sessions pagination
#[derive(Debug, Deserialize)]
struct SessionsQuery {
    #[serde(default)]
    page: usize,
    #[serde(default = "default_page_size")]
    limit: usize,
    #[serde(default)]
    search: Option<String>,
    #[serde(default)]
    project: Option<String>,
    #[serde(default)]
    model: Option<String>,
    #[serde(default)]
    since: Option<String>, // e.g., "7d", "30d"
    #[serde(default = "default_sort")]
    sort: String, // "date", "tokens", "cost"
    #[serde(default = "default_order")]
    order: String, // "asc", "desc"
}

fn default_page_size() -> usize {
    50
}
fn default_sort() -> String {
    "date".to_string()
}
fn default_order() -> String {
    "desc".to_string()
}

/// Query parameters for recent sessions
#[derive(Debug, Deserialize)]
struct RecentQuery {
    #[serde(default = "default_recent_limit")]
    limit: usize,
}

fn default_recent_limit() -> usize {
    5
}

/// Query parameters for FTS5 session search
#[derive(Debug, Deserialize)]
struct SearchQuery {
    q: String,
    #[serde(default = "default_search_limit")]
    limit: usize,
}

fn default_search_limit() -> usize {
    20
}

/// Query parameters for activity violations
#[derive(Debug, Deserialize)]
struct ViolationsQuery {
    /// Maximum number of violations to return (default: 50)
    #[serde(default = "default_violations_limit")]
    limit: usize,
    /// Minimum severity filter: "Info", "Warning", or "Critical"
    #[serde(default)]
    min_severity: Option<String>,
}

fn default_violations_limit() -> usize {
    50
}

/// Serve a file from the embedded WASM frontend assets.
/// Returns None if the file is not found (caller handles SPA fallback).
fn get_embedded_asset(path: &str) -> Option<Response> {
    let asset = DistAssets::get(path)?;
    let mime = from_path(path).first_or_octet_stream();
    Some(
        (
            StatusCode::OK,
            [(header::CONTENT_TYPE, mime.as_ref().to_string())],
            Body::from(asset.data),
        )
            .into_response(),
    )
}

/// Fallback handler: serves embedded frontend assets with SPA fallback to index.html.
///
/// In debug builds rust-embed reads from the `dist/` directory on disk.
/// In release builds all assets are compiled into the binary — no filesystem access needed.
async fn frontend_handler(uri: Uri) -> Response {
    let path = uri.path().trim_start_matches('/');
    let path = if path.is_empty() { "index.html" } else { path };

    get_embedded_asset(path)
        .or_else(|| get_embedded_asset("index.html"))
        .unwrap_or_else(|| StatusCode::NOT_FOUND.into_response())
}

/// Create the web router
pub fn create_router(store: Arc<DataStore>) -> Router {
    let cors = CorsLayer::new()
        .allow_origin(Any)
        .allow_methods(Any)
        .allow_headers(Any);

    Router::new()
        // API routes (must be before catch-all static files)
        .route("/api/search", get(search_handler))
        .route("/api/stats", get(stats_handler))
        .route("/api/quota", get(quota_handler))
        .route("/api/sessions/recent", get(recent_sessions_handler)) // Must be before /api/sessions
        .route("/api/sessions/live", get(live_sessions_handler)) // Live sessions with CPU/RAM
        .route("/api/sessions", get(sessions_handler))
        .route("/api/config/merged", get(config_handler))
        .route("/api/hooks", get(hooks_handler))
        .route("/api/mcp", get(mcp_handler))
        .route("/api/agents", get(agents_handler))
        .route("/api/commands", get(commands_handler))
        .route("/api/skills", get(skills_handler))
        .route("/api/plugins", get(plugins_handler))
        .route("/api/task-graph", get(task_graph_handler))
        .route("/api/health", get(health_handler))
        // Activity routes — literal path before parameterised path
        .route("/api/activity/violations", get(activity_violations_handler))
        .route("/api/activity/{session_id}", get(activity_session_handler))
        .route("/api/events", get(sse_handler))
        // Serve WASM frontend (embedded in binary) + SPA fallback to index.html
        .fallback(frontend_handler)
        .layer(cors)
        .with_state(store)
}

async fn stats_handler(
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    let stats = store.stats();

    // Compute analytics for last 30 days
    let sessions = store.all_sessions();
    let analytics = ccboard_core::analytics::AnalyticsData::compute(
        &sessions,
        ccboard_core::analytics::Period::last_30d(),
    );

    // Extract forecast data points for chart
    let historical_tokens: Vec<u64> = analytics.trends.daily_tokens.clone();
    let forecast_tokens: Vec<u64> = {
        let mut forecast = Vec::new();
        if analytics.trends.dates.len() >= 7 {
            // Extend with 30 days forecast using linear projection
            for i in 1..=30 {
                // Simple linear extrapolation from last value + trend
                let projected = analytics.forecast.next_30_days_tokens as f64 / 30.0 * i as f64;
                forecast.push(projected as u64);
            }
        }
        forecast
    };

    // Get top 5 projects by cost
    let projects_by_cost: Vec<serde_json::Value> = {
        let mut project_costs: std::collections::HashMap<String, f64> =
            std::collections::HashMap::new();

        for session in &sessions {
            let cost = calculate_session_cost(
                session.input_tokens,
                session.output_tokens,
                session.cache_creation_tokens,
                session.cache_read_tokens,
                &session.models_used,
            );
            *project_costs
                .entry(session.project_path.as_str().to_string())
                .or_insert(0.0) += cost;
        }

        let mut sorted: Vec<_> = project_costs.into_iter().collect();
        sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));

        let total_cost: f64 = sorted.iter().map(|(_, c)| c).sum();

        sorted
            .iter()
            .take(5)
            .map(|(project, cost)| {
                let percentage = if total_cost > 0.0 {
                    cost / total_cost * 100.0
                } else {
                    0.0
                };
                serde_json::json!({
                    "project": project,
                    "cost": cost,
                    "percentage": percentage,
                })
            })
            .collect()
    };

    // Get most used model
    let most_used_model = analytics
        .trends
        .model_usage_over_time
        .iter()
        .max_by_key(|(_, counts)| counts.iter().sum::<usize>())
        .map(|(model, counts)| {
            let total: usize = counts.iter().sum();
            serde_json::json!({
                "name": model,
                "count": total,
            })
        });

    match stats {
        Some(s) => {
            let mut value = serde_json::to_value(&s).unwrap_or(serde_json::Value::Null);

            // Inject analytics data
            if let Some(obj) = value.as_object_mut() {
                obj.insert(
                    "dailyTokens30d".to_string(),
                    serde_json::json!(historical_tokens),
                );
                obj.insert(
                    "forecastTokens30d".to_string(),
                    serde_json::json!(forecast_tokens),
                );
                obj.insert(
                    "forecastConfidence".to_string(),
                    serde_json::json!(analytics.forecast.confidence),
                );
                obj.insert(
                    "forecastCost30d".to_string(),
                    serde_json::json!(analytics.forecast.next_30_days_cost),
                );
                obj.insert(
                    "projectsByCost".to_string(),
                    serde_json::json!(projects_by_cost),
                );
                obj.insert(
                    "mostUsedModel".to_string(),
                    serde_json::json!(most_used_model),
                );

                // Add aggregated totals for current month
                let total_cost: f64 = sessions
                    .iter()
                    .map(|s| {
                        calculate_session_cost(
                            s.input_tokens,
                            s.output_tokens,
                            s.cache_creation_tokens,
                            s.cache_read_tokens,
                            &s.models_used,
                        )
                    })
                    .sum();
                let avg_session_cost = if !sessions.is_empty() {
                    total_cost / sessions.len() as f64
                } else {
                    0.0
                };

                obj.insert("thisMonthCost".to_string(), serde_json::json!(total_cost));
                obj.insert(
                    "avgSessionCost".to_string(),
                    serde_json::json!(avg_session_cost),
                );

                // Add cache hit ratio
                let cache_hit_ratio = s.cache_ratio();
                obj.insert(
                    "cacheHitRatio".to_string(),
                    serde_json::json!(cache_hit_ratio),
                );

                // Add MCP servers count
                let mcp_count = store.mcp_config().map(|c| c.servers.len()).unwrap_or(0);
                obj.insert("mcpServersCount".to_string(), serde_json::json!(mcp_count));
            }

            axum::Json(value)
        }
        None => axum::Json(serde_json::json!({"error": "Stats not loaded"})),
    }
}

/// Quota status handler (budget tracking)
async fn quota_handler(
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    match store.quota_status() {
        Some(quota) => {
            // Serialize QuotaStatus + add alert_level as string for easier frontend handling
            let alert_level_str = match quota.alert_level {
                ccboard_core::quota::AlertLevel::Safe => "safe",
                ccboard_core::quota::AlertLevel::Warning => "warning",
                ccboard_core::quota::AlertLevel::Critical => "critical",
                ccboard_core::quota::AlertLevel::Exceeded => "exceeded",
            };

            axum::Json(serde_json::json!({
                "current_cost": quota.current_cost,
                "budget_limit": quota.budget_limit,
                "usage_pct": quota.usage_pct,
                "projected_monthly_cost": quota.projected_monthly_cost,
                "projected_overage": quota.projected_overage,
                "alert_level": alert_level_str,
            }))
        }
        None => axum::Json(serde_json::json!({
            "error": "No budget configured or stats not loaded",
        })),
    }
}

/// Recent sessions handler (lightweight, for dashboard)
async fn recent_sessions_handler(
    Query(params): Query<RecentQuery>,
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    let mut all_sessions = store.all_sessions();

    // Sort by date desc
    all_sessions.sort_by(|a, b| b.last_timestamp.cmp(&a.last_timestamp));

    let sessions: Vec<_> = all_sessions
        .iter()
        .take(params.limit)
        .map(|s| session_to_json(s))
        .collect();

    axum::Json(serde_json::json!({
        "sessions": sessions,
        "total": all_sessions.len() as u64,
    }))
}

/// Live sessions handler - returns active Claude Code processes with CPU/RAM
async fn live_sessions_handler() -> axum::Json<serde_json::Value> {
    use ccboard_core::detect_live_sessions;

    const MAX_LIVE_SESSIONS: usize = 20;

    match detect_live_sessions() {
        Ok(live_sessions) => {
            let total = live_sessions.len();
            let truncated = total > MAX_LIVE_SESSIONS;

            let sessions: Vec<_> = live_sessions
                .iter()
                .take(MAX_LIVE_SESSIONS)
                .map(|ls| {
                    serde_json::json!({
                        "pid": ls.pid,
                        "startTime": ls.start_time.to_rfc3339(),
                        "workingDirectory": ls.working_directory,
                        "command": ls.command,
                        "cpuPercent": ls.cpu_percent,
                        "memoryMb": ls.memory_mb,
                        "tokens": ls.tokens,
                        "sessionId": ls.session_id,
                        "sessionName": ls.session_name,
                    })
                })
                .collect();

            axum::Json(serde_json::json!({
                "sessions": sessions,
                "total": total,
                "displayed": sessions.len(),
                "truncated": truncated,
            }))
        }
        Err(e) => axum::Json(serde_json::json!({
            "sessions": [],
            "total": 0,
            "error": format!("Failed to detect live sessions: {}", e),
        })),
    }
}

/// Sessions handler with pagination and filters
async fn sessions_handler(
    Query(params): Query<SessionsQuery>,
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    let mut all_sessions = store.all_sessions();

    // Filter by search
    if let Some(ref search) = params.search {
        let search_lower = search.to_lowercase();
        all_sessions.retain(|s| {
            s.id.to_lowercase().contains(&search_lower)
                || s.project_path.to_lowercase().contains(&search_lower)
                || s.first_user_message
                    .as_ref()
                    .map(|m| m.to_lowercase().contains(&search_lower))
                    .unwrap_or(false)
        });
    }

    // Filter by project
    if let Some(ref project) = params.project {
        all_sessions.retain(|s| s.project_path.contains(project));
    }

    // Filter by model
    if let Some(ref model) = params.model {
        all_sessions.retain(|s| s.models_used.iter().any(|m| m.contains(model)));
    }

    // Filter by time range (since)
    if let Some(ref since) = params.since {
        if let Some(cutoff) = parse_since(since) {
            all_sessions.retain(|s| s.last_timestamp.map(|t| t >= cutoff).unwrap_or(false));
        }
    }

    // Sort
    match params.sort.as_str() {
        "date" => all_sessions.sort_by(|a, b| {
            if params.order == "asc" {
                a.last_timestamp.cmp(&b.last_timestamp)
            } else {
                b.last_timestamp.cmp(&a.last_timestamp)
            }
        }),
        "tokens" => all_sessions.sort_by(|a, b| {
            if params.order == "asc" {
                a.total_tokens.cmp(&b.total_tokens)
            } else {
                b.total_tokens.cmp(&a.total_tokens)
            }
        }),
        "cost" => all_sessions.sort_by(|a, b| {
            let cost_a = calculate_session_cost(
                a.input_tokens,
                a.output_tokens,
                a.cache_creation_tokens,
                a.cache_read_tokens,
                &a.models_used,
            );
            let cost_b = calculate_session_cost(
                b.input_tokens,
                b.output_tokens,
                b.cache_creation_tokens,
                b.cache_read_tokens,
                &b.models_used,
            );
            if params.order == "asc" {
                cost_a
                    .partial_cmp(&cost_b)
                    .unwrap_or(std::cmp::Ordering::Equal)
            } else {
                cost_b
                    .partial_cmp(&cost_a)
                    .unwrap_or(std::cmp::Ordering::Equal)
            }
        }),
        _ => {} // Keep current order
    }

    let total = all_sessions.len();
    let page_size = params.limit.min(100); // Cap at 100
    let offset = params.page * page_size;

    let sessions: Vec<_> = all_sessions
        .iter()
        .skip(offset)
        .take(page_size)
        .map(|s| session_to_json(s))
        .collect();

    axum::Json(serde_json::json!({
        "sessions": sessions,
        "total": total as u64,
        "page": params.page,
        "page_size": page_size,
    }))
}

/// Convert session to JSON (shared helper)
fn session_to_json(s: &ccboard_core::models::SessionMetadata) -> serde_json::Value {
    let cost = calculate_session_cost(
        s.input_tokens,
        s.output_tokens,
        s.cache_creation_tokens,
        s.cache_read_tokens,
        &s.models_used,
    );

    serde_json::json!({
        "id": s.id,
        "date": s.last_timestamp.map(|t: chrono::DateTime<chrono::Utc>| t.to_rfc3339()),
        "project": s.project_path,
        "model": s.models_used.first().map(|s| s.as_str()).unwrap_or("unknown"),
        "messages": s.message_count,
        "tokens": s.total_tokens,
        "input_tokens": s.input_tokens,
        "output_tokens": s.output_tokens,
        "cache_creation_tokens": s.cache_creation_tokens,
        "cache_read_tokens": s.cache_read_tokens,
        "cost": cost,
        "status": "completed",
        "first_timestamp": s.first_timestamp.map(|t: chrono::DateTime<chrono::Utc>| t.to_rfc3339()),
        "duration_seconds": s.duration_seconds,
        "preview": s.first_user_message,
    })
}

/// Parse "since" parameter (e.g., "7d", "30d", "1h")
fn parse_since(since: &str) -> Option<chrono::DateTime<chrono::Utc>> {
    let now = chrono::Utc::now();
    if let Some(days) = since.strip_suffix('d') {
        if let Ok(d) = days.parse::<i64>() {
            return Some(now - chrono::Duration::days(d));
        }
    }
    if let Some(hours) = since.strip_suffix('h') {
        if let Ok(h) = hours.parse::<i64>() {
            return Some(now - chrono::Duration::hours(h));
        }
    }
    None
}

/// Plugin analytics handler
async fn plugins_handler(
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    use ccboard_core::analytics::aggregate_plugin_usage;
    use chrono::Utc;

    // Get all sessions (limit to recent 10000 for performance)
    let sessions = store.recent_sessions(10000);

    // Extract skill and command names from invocation stats
    let invocation_stats = store.invocation_stats();
    let skills: Vec<String> = invocation_stats.skills.keys().cloned().collect();
    let commands: Vec<String> = invocation_stats.commands.keys().cloned().collect();

    // Compute plugin analytics
    let analytics = aggregate_plugin_usage(&sessions, &skills, &commands);

    // Return JSON response
    axum::Json(serde_json::json!({
        "analytics": analytics,
        "generated_at": Utc::now().to_rfc3339(),
    }))
}

/// Calculate rough cost estimate for a session
fn calculate_session_cost(
    input_tokens: u64,
    output_tokens: u64,
    cache_creation_tokens: u64,
    cache_read_tokens: u64,
    models: &[String],
) -> f64 {
    // Default to Sonnet 4.5 pricing if unknown model
    // Input: $3/MTok, Output: $15/MTok, Cache write: $3.75/MTok, Cache read: $0.30/MTok
    let is_opus = models.iter().any(|m| m.contains("opus"));
    let is_haiku = models.iter().any(|m| m.contains("haiku"));

    let (input_price, output_price, cache_write_price, cache_read_price) = if is_opus {
        (15.0, 75.0, 18.75, 1.5) // Opus 4 pricing
    } else if is_haiku {
        (0.8, 4.0, 1.0, 0.08) // Haiku 4 pricing
    } else {
        (3.0, 15.0, 3.75, 0.3) // Sonnet 4.5 pricing (default)
    };

    let input_cost = (input_tokens as f64 / 1_000_000.0) * input_price;
    let output_cost = (output_tokens as f64 / 1_000_000.0) * output_price;
    let cache_write_cost = (cache_creation_tokens as f64 / 1_000_000.0) * cache_write_price;
    let cache_read_cost = (cache_read_tokens as f64 / 1_000_000.0) * cache_read_price;

    input_cost + output_cost + cache_write_cost + cache_read_cost
}

/// Config handler - returns merged settings (global + project + local)
async fn config_handler(
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    let settings = store.settings();
    axum::Json(serde_json::to_value(&settings).unwrap_or_default())
}

async fn health_handler(
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    let state = store.degraded_state();
    axum::Json(serde_json::json!({
        "status": if state.is_healthy() { "healthy" } else { "degraded" },
        "sessions": store.session_count(),
        "stats_loaded": store.stats().is_some(),
    }))
}

/// FTS5 full-text search handler
async fn search_handler(
    Query(params): Query<SearchQuery>,
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    let results = store.search_sessions(&params.q, params.limit);

    let items: Vec<_> = results
        .iter()
        .map(|r| {
            serde_json::json!({
                "session_id": r.session_id,
                "path": r.path.to_string_lossy(),
                "project": r.project,
                "first_user_message": r.first_user_message,
                "snippet": r.snippet,
                "rank": r.rank,
            })
        })
        .collect();

    axum::Json(serde_json::json!({
        "results": items,
        "total": items.len(),
        "query": params.q,
    }))
}

/// SSE endpoint for live updates
async fn sse_handler(
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::response::Sse<
    impl futures::stream::Stream<Item = Result<axum::response::sse::Event, std::convert::Infallible>>,
> {
    // Clone EventBus to avoid lifetime issues (it's cheap - Arc internally)
    let event_bus = store.event_bus().clone();
    sse::create_sse_stream(event_bus)
}

/// Hooks handler - returns all hooks from merged settings
async fn hooks_handler(
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    let settings = store.settings();

    let mut hooks_list = Vec::new();

    if let Some(hooks_map) = &settings.merged.hooks {
        for (event_name, hook_groups) in hooks_map {
            for (group_idx, hook_group) in hook_groups.iter().enumerate() {
                for (hook_idx, hook_def) in hook_group.hooks.iter().enumerate() {
                    // Generate unique hook name
                    let hook_name = if hook_groups.len() == 1 && hook_group.hooks.len() == 1 {
                        event_name.clone()
                    } else {
                        format!("{}-{}-{}", event_name, group_idx, hook_idx)
                    };

                    // Try to read script content if command points to a .sh file
                    let (script_path, script_content) = if hook_def.command.ends_with(".sh") {
                        let path = std::path::Path::new(&hook_def.command);
                        let content = std::fs::read_to_string(path).ok();
                        (Some(hook_def.command.clone()), content)
                    } else {
                        (None, None)
                    };

                    hooks_list.push(serde_json::json!({
                        "name": hook_name,
                        "event": event_name,
                        "command": hook_def.command,
                        "description": extract_description(&hook_def.command, script_content.as_deref()),
                        "async": hook_def.r#async.unwrap_or(false),
                        "timeout": hook_def.timeout,
                        "cwd": hook_def.cwd,
                        "matcher": hook_group.matcher,
                        "scriptPath": script_path,
                        "scriptContent": script_content,
                    }));
                }
            }
        }
    }

    axum::Json(serde_json::json!({
        "hooks": hooks_list,
        "total": hooks_list.len(),
    }))
}

/// Extract description from script content (look for # Description: comment)
fn extract_description(command: &str, script_content: Option<&str>) -> Option<String> {
    if let Some(content) = script_content {
        for line in content.lines().take(20) {
            let trimmed = line.trim();
            if trimmed.starts_with("# Description:") {
                return Some(
                    trimmed
                        .trim_start_matches("# Description:")
                        .trim()
                        .to_string(),
                );
            }
        }
    }

    // Fallback: use command as description
    Some(command.to_string())
}

/// MCP handler - returns MCP server configuration
async fn mcp_handler(
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    match store.mcp_config() {
        Some(config) => {
            let mut servers_list = Vec::new();

            for (name, server) in &config.servers {
                servers_list.push(serde_json::json!({
                    "name": name,
                    "command": server.display_command(),
                    "serverType": if server.is_http() { "http" } else { "stdio" },
                    "url": server.url,
                    "args": server.args,
                    "env": server.env,
                    "hasEnv": !server.env.is_empty(),
                }));
            }

            axum::Json(serde_json::json!({
                "servers": servers_list,
                "total": servers_list.len(),
            }))
        }
        None => axum::Json(serde_json::json!({
            "servers": [],
            "total": 0,
        })),
    }
}

/// Helper to scan markdown files from a directory
fn scan_markdown_files(dir_path: &std::path::Path) -> Vec<serde_json::Value> {
    let mut items = Vec::new();

    if !dir_path.exists() {
        return items;
    }

    if let Ok(entries) = std::fs::read_dir(dir_path) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) == Some("md") {
                if let Ok(content) = std::fs::read_to_string(&path) {
                    let (frontmatter, body) = parse_frontmatter(&content);
                    let name = path
                        .file_stem()
                        .and_then(|s| s.to_str())
                        .unwrap_or("unknown")
                        .to_string();

                    items.push(serde_json::json!({
                        "name": name,
                        "frontmatter": frontmatter,
                        "body": body,
                        "path": path.to_string_lossy(),
                    }));
                }
            }
        }
    }

    // Sort by name
    items.sort_by(|a, b| {
        let a_name = a.get("name").and_then(|v| v.as_str()).unwrap_or("");
        let b_name = b.get("name").and_then(|v| v.as_str()).unwrap_or("");
        a_name.cmp(b_name)
    });

    items
}

/// Parse frontmatter (YAML between ---) and body
fn parse_frontmatter(content: &str) -> (serde_json::Value, String) {
    let lines: Vec<&str> = content.lines().collect();

    // Check if starts with ---
    if lines.first() != Some(&"---") {
        return (serde_json::json!({}), content.to_string());
    }

    // Find closing ---
    if let Some(end_idx) = lines[1..].iter().position(|&line| line == "---") {
        let yaml_lines = &lines[1..=end_idx];
        let body_lines = &lines[end_idx + 2..];

        let yaml_str = yaml_lines.join("\n");
        let frontmatter: serde_json::Value =
            serde_yaml::from_str(&yaml_str).unwrap_or_else(|_| serde_json::json!({}));

        let body = body_lines.join("\n");

        (frontmatter, body)
    } else {
        (serde_json::json!({}), content.to_string())
    }
}

/// Agents handler - returns agents from ~/.claude/agents/
async fn agents_handler() -> axum::Json<serde_json::Value> {
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    let agents_dir = std::path::Path::new(&home).join(".claude/agents");
    let agents = scan_markdown_files(&agents_dir);

    axum::Json(serde_json::json!({
        "items": agents,
        "total": agents.len(),
    }))
}

/// Commands handler - returns commands from ~/.claude/commands/
async fn commands_handler() -> axum::Json<serde_json::Value> {
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    let commands_dir = std::path::Path::new(&home).join(".claude/commands");
    let commands = scan_markdown_files(&commands_dir);

    axum::Json(serde_json::json!({
        "items": commands,
        "total": commands.len(),
    }))
}

/// Skills handler - returns skills from ~/.claude/skills/ subdirectories
async fn skills_handler() -> axum::Json<serde_json::Value> {
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    let skills_dir = std::path::Path::new(&home).join(".claude/skills");
    let skills = scan_skills_recursive(&skills_dir);

    axum::Json(serde_json::json!({
        "items": skills,
        "total": skills.len(),
    }))
}

/// Helper to scan skills from subdirectories (looks for SKILL.md files)
fn scan_skills_recursive(dir_path: &std::path::Path) -> Vec<serde_json::Value> {
    let mut items = Vec::new();

    if !dir_path.exists() {
        return items;
    }

    // Scan subdirectories for SKILL.md files
    if let Ok(entries) = std::fs::read_dir(dir_path) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                let skill_file = path.join("SKILL.md");
                if skill_file.exists() {
                    if let Ok(content) = std::fs::read_to_string(&skill_file) {
                        let (frontmatter, body) = parse_frontmatter(&content);
                        let name = path
                            .file_name()
                            .and_then(|s| s.to_str())
                            .unwrap_or("unknown")
                            .to_string();

                        items.push(serde_json::json!({
                            "name": name,
                            "frontmatter": frontmatter,
                            "body": body,
                            "path": skill_file.to_string_lossy(),
                        }));
                    }
                }
            }
        }
    }

    // Sort by name
    items.sort_by(|a, b| {
        let a_name = a.get("name").and_then(|v| v.as_str()).unwrap_or("");
        let b_name = b.get("name").and_then(|v| v.as_str()).unwrap_or("");
        a_name.cmp(b_name)
    });

    items
}

/// Task graph handler - returns task dependency graph from PLAN.md
async fn task_graph_handler(
    axum::extract::State(_store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    use ccboard_core::graph::TaskGraph;
    use ccboard_core::models::plan::PhaseStatus;
    use ccboard_core::parsers::PlanParser;

    // Try multiple PLAN.md locations (prioritize working dir)
    let home = std::env::var("HOME").unwrap_or_else(|_| ".".to_string());
    let cwd = std::env::current_dir().unwrap_or_else(|_| std::path::PathBuf::from("."));

    let possible_paths = vec![
        // Current project claudedocs/PLAN.md (highest priority - dev dir)
        cwd.join("claudedocs/PLAN.md"),
        // Current project .claude/PLAN.md
        cwd.join(".claude/PLAN.md"),
        // Claude home claudedocs/PLAN.md
        std::path::PathBuf::from(&home).join(".claude/claudedocs/PLAN.md"),
    ];

    let plan_path = possible_paths
        .into_iter()
        .find(|p| p.exists())
        .unwrap_or_else(|| cwd.join("PLAN.md"));

    let plan_result = PlanParser::parse_file(&plan_path);

    match plan_result {
        Ok(Some(plan)) => {
            // Build task graph
            let mut graph = TaskGraph::new();

            // Add all tasks to graph
            for phase in &plan.phases {
                for task in &phase.tasks {
                    graph.add_task(task.clone());
                }
            }

            // Parse dependencies from PLAN.md content directly
            // Read the file again to extract dependency information
            if let Ok(plan_content) = std::fs::read_to_string(&plan_path) {
                // Look for task sections and extract dependencies
                for phase in &plan.phases {
                    for task in &phase.tasks {
                        // Find this task in the content
                        let task_header = format!("#### Task {}:", task.id);
                        if let Some(pos) = plan_content.find(&task_header) {
                            // Extract content until next task or section
                            let rest = &plan_content[pos..];
                            // Use char boundary-safe slicing
                            let skip_offset = rest
                                .char_indices()
                                .nth(100)
                                .map(|(i, _)| i)
                                .unwrap_or(rest.len());
                            let end_pos = rest
                                .get(skip_offset..)
                                .and_then(|s| s.find("\n####").map(|p| p + skip_offset))
                                .or_else(|| {
                                    rest.get(skip_offset..)
                                        .and_then(|s| s.find("\n###").map(|p| p + skip_offset))
                                })
                                .or_else(|| {
                                    rest.get(skip_offset..)
                                        .and_then(|s| s.find("\n##").map(|p| p + skip_offset))
                                })
                                .unwrap_or(rest.len());

                            let task_content = &rest[..end_pos];

                            // Extract dependencies
                            let deps = extract_dependencies(task_content);
                            for dep_id in deps {
                                // Add dependency: dep_id blocks task_id
                                let _ = graph.add_dependency(&dep_id, &task.id);
                            }
                        }
                    }
                }
            }

            // Convert to JSON format for D3.js
            let nodes: Vec<_> = plan
                .phases
                .iter()
                .flat_map(|phase| {
                    phase.tasks.iter().map(move |task| {
                        let status = match phase.status {
                            PhaseStatus::Complete => "Complete",
                            PhaseStatus::InProgress => "InProgress",
                            PhaseStatus::Future => "Future",
                        };

                        serde_json::json!({
                            "id": task.id,
                            "label": task.title,
                            "phase": phase.id,
                            "status": status,
                            "duration": task.duration,
                            "description": task.description,
                            "priority": task.priority,
                            "difficulty": task.difficulty,
                            "crateName": task.crate_name,
                            "issue": task.issue,
                        })
                    })
                })
                .collect();

            // Extract edges from graph
            let mut edges = Vec::new();
            for task in graph.tasks() {
                let dependents = graph.dependents(&task.id);
                for dependent in dependents {
                    edges.push(serde_json::json!({
                        "source": task.id,
                        "target": dependent,
                        "type": "blocks",
                    }));
                }
            }

            axum::Json(serde_json::json!({
                "nodes": nodes,
                "edges": edges,
            }))
        }
        Ok(None) => {
            // PLAN.md not found
            axum::Json(serde_json::json!({
                "nodes": [],
                "edges": [],
                "error": "PLAN.md not found",
            }))
        }
        Err(e) => {
            // Parse error
            axum::Json(serde_json::json!({
                "nodes": [],
                "edges": [],
                "error": format!("Failed to parse PLAN.md: {}", e),
            }))
        }
    }
}

/// Activity violations handler — returns aggregated security alerts across all sessions.
///
/// Merges in-memory DashMap results (freshest) with SQLite-persisted alerts.
/// Sorted Critical → Warning → Info, then by timestamp descending.
///
/// Query params:
/// - `limit`: max violations to return (default 50)
/// - `min_severity`: "Info" | "Warning" | "Critical" (default: all)
async fn activity_violations_handler(
    Query(params): Query<ViolationsQuery>,
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    let mut violations = store.all_violations();

    // Filter by min_severity (already sorted Critical > Warning > Info by store)
    if let Some(ref min_sev) = params.min_severity {
        violations.retain(|a| match min_sev.as_str() {
            "Critical" => matches!(a.severity, AlertSeverity::Critical),
            "Warning" => !matches!(a.severity, AlertSeverity::Info),
            _ => true, // "Info" or unknown → keep all
        });
    }

    let total = violations.len();

    // Count by severity for summary cards
    let critical_count = violations
        .iter()
        .filter(|a| matches!(a.severity, AlertSeverity::Critical))
        .count();
    let warning_count = violations
        .iter()
        .filter(|a| matches!(a.severity, AlertSeverity::Warning))
        .count();
    let info_count = violations
        .iter()
        .filter(|a| matches!(a.severity, AlertSeverity::Info))
        .count();

    violations.truncate(params.limit);

    let serialized: Vec<_> = violations
        .iter()
        .map(|a| {
            serde_json::json!({
                "session_id": a.session_id,
                "timestamp": a.timestamp.to_rfc3339(),
                "severity": format!("{:?}", a.severity),
                "category": format!("{:?}", a.category),
                "detail": a.detail,
                "action_hint": a.category.action_hint(),
            })
        })
        .collect();

    axum::Json(serde_json::json!({
        "violations": serialized,
        "total": total,
        "displayed": serialized.len(),
        "critical_count": critical_count,
        "warning_count": warning_count,
        "info_count": info_count,
    }))
}

/// Activity session handler — triggers on-demand analysis of a session's tool calls.
///
/// Returns `ActivitySummary` (file accesses, bash commands, network calls, alerts).
/// Results are cached in SQLite after first analysis; subsequent calls return the cache.
async fn activity_session_handler(
    axum::extract::Path(session_id): axum::extract::Path<String>,
    axum::extract::State(store): axum::extract::State<Arc<DataStore>>,
) -> axum::Json<serde_json::Value> {
    match store.analyze_session(&session_id).await {
        Ok(summary) => {
            // Serialize with human-readable severity/category strings
            let alerts: Vec<_> = summary
                .alerts
                .iter()
                .map(|a| {
                    serde_json::json!({
                        "session_id": a.session_id,
                        "timestamp": a.timestamp.to_rfc3339(),
                        "severity": format!("{:?}", a.severity),
                        "category": format!("{:?}", a.category),
                        "detail": a.detail,
                        "action_hint": a.category.action_hint(),
                    })
                })
                .collect();

            axum::Json(serde_json::json!({
                "session_id": session_id,
                "file_accesses": summary.file_accesses,
                "bash_commands": summary.bash_commands,
                "network_calls": summary.network_calls,
                "alerts": alerts,
                "stats": {
                    "file_accesses": summary.file_accesses.len(),
                    "bash_commands": summary.bash_commands.len(),
                    "network_calls": summary.network_calls.len(),
                    "alerts": alerts.len(),
                }
            }))
        }
        Err(e) => axum::Json(serde_json::json!({
            "error": format!("Failed to analyze session: {}", e),
        })),
    }
}

/// Extract dependency task IDs from description text
/// Looks for patterns like "Depends on: F.1, F.2" or "Blocked by: #6, #7"
fn extract_dependencies(description: &str) -> Vec<String> {
    let mut deps = Vec::new();

    // Pattern 1: "Depends on: F.1, F.2"
    if let Some(start) = description.find("Depends on:") {
        let rest = &description[start + "Depends on:".len()..];
        // Take until newline or end
        let dep_text = rest.lines().next().unwrap_or("");

        // Extract task IDs like F.1, H.2, etc.
        for part in dep_text.split(',') {
            let trimmed = part.trim();
            // Match pattern: Letter.Number (e.g., F.1, H.2)
            if trimmed
                .chars()
                .next()
                .map(|c| c.is_alphabetic())
                .unwrap_or(false)
            {
                if let Some(task_id) = trimmed.split_whitespace().next() {
                    deps.push(task_id.to_string());
                }
            }
        }
    }

    // Pattern 2: "Blocked by: #6, #7" - extract task numbers
    if let Some(start) = description.find("Blocked by:") {
        let rest = &description[start + "Blocked by:".len()..];
        let dep_text = rest.lines().next().unwrap_or("");

        for part in dep_text.split(',') {
            let trimmed = part.trim();
            if let Some(task_num) = trimmed.strip_prefix('#') {
                if let Ok(_num) = task_num.parse::<u32>() {
                    // For now, skip numeric task IDs (would need task ID mapping)
                    // We'll focus on letter.number format (F.1, H.2, etc.)
                }
            }
        }
    }

    deps
}