kto 0.1.5

A generic, flexible web change watcher with AI-powered analysis
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
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
//! Miscellaneous commands: doctor, export, import, memory, diff, logs, ui, completions, init

use clap::CommandFactory;
use clap_complete::{generate, Shell};
use colored::Colorize;
use serde_json;
use std::io;
use std::time::Duration;

use kto::agent;
use kto::cli::{Cli, CompletionShell};
use kto::config::Config;
use kto::db::Database;
use kto::fetch::{self, check_playwright, PlaywrightStatus};
use kto::watch::Change;
use kto::error::Result;

/// Interactive TUI dashboard
#[cfg(feature = "tui")]
pub fn cmd_ui() -> Result<()> {
    kto::tui::run()
}

#[cfg(not(feature = "tui"))]
pub fn cmd_ui() -> Result<()> {
    eprintln!("TUI not available. Rebuild with: cargo build --features tui");
    Ok(())
}

/// Export watches to JSON
pub fn cmd_export(watch: Option<String>) -> Result<()> {
    let db = Database::open()?;

    let watches = if let Some(id_or_name) = watch {
        let watch = db.get_watch(&id_or_name)?
            .ok_or_else(|| kto::KtoError::WatchNotFound(id_or_name.clone()))?;
        vec![watch]
    } else {
        db.list_watches()?
    };

    let json = serde_json::to_string_pretty(&watches)?;
    println!("{}", json);
    Ok(())
}

/// Import watches from JSON
pub fn cmd_import(dry_run: bool) -> Result<()> {
    use std::io::{self, Read};

    // Read JSON from stdin
    let mut input = String::new();
    io::stdin().read_to_string(&mut input)?;

    let watches: Vec<kto::watch::Watch> = serde_json::from_str(&input)
        .map_err(|e| kto::KtoError::ConfigError(format!("Invalid JSON: {}", e)))?;

    if watches.is_empty() {
        println!("No watches to import.");
        return Ok(());
    }

    let db = Database::open()?;
    let existing_names: std::collections::HashSet<String> = db.list_watches()?
        .into_iter()
        .map(|w| w.name)
        .collect();

    println!("\n{} watch(es) to import:\n", watches.len());
    for watch in &watches {
        let status = if existing_names.contains(&watch.name) {
            "SKIP (exists)"
        } else {
            "NEW"
        };
        println!("  [{}] {} - {}", status, watch.name, watch.url);
    }

    if dry_run {
        println!("\n(dry-run mode - no changes made)");
        return Ok(());
    }

    let mut imported = 0;
    let mut skipped = 0;

    for watch in watches {
        if existing_names.contains(&watch.name) {
            skipped += 1;
            continue;
        }

        // Create a new watch with a fresh ID
        let mut new_watch = watch;
        new_watch.id = uuid::Uuid::new_v4();
        new_watch.created_at = chrono::Utc::now();

        db.insert_watch(&new_watch)?;
        imported += 1;
    }

    println!("\nImported {} watch(es), skipped {} (already exist).", imported, skipped);
    Ok(())
}

/// Show the latest diff for a watch
pub fn cmd_diff(id_or_name: &str, limit: usize) -> Result<()> {
    let db = Database::open()?;
    let watch = db.get_watch(id_or_name)?
        .ok_or_else(|| kto::KtoError::WatchNotFound(id_or_name.to_string()))?;

    let changes = db.get_recent_changes(&watch.id, limit)?;

    if changes.is_empty() {
        println!("\nNo changes recorded for '{}'.", watch.name);
        println!("Run `kto test \"{}\"` to check for changes.", watch.name);
        return Ok(());
    }

    println!("\nRecent changes for '{}':\n", watch.name);

    for (i, change) in changes.iter().enumerate() {
        // Format time ago
        let ago = chrono::Utc::now().signed_duration_since(change.detected_at);
        let time_ago = if ago.num_seconds() < 60 {
            format!("{}s ago", ago.num_seconds())
        } else if ago.num_minutes() < 60 {
            format!("{}m ago", ago.num_minutes())
        } else if ago.num_hours() < 24 {
            format!("{}h ago", ago.num_hours())
        } else {
            format!("{}d ago", ago.num_days())
        };

        println!("{}. {} ({})", i + 1, change.detected_at.format("%Y-%m-%d %H:%M"), time_ago);

        if let Some(ref resp) = change.agent_response {
            if let Some(summary) = resp.get("summary").and_then(|s: &serde_json::Value| s.as_str()) {
                println!("   AI: {}", summary.cyan());
            }
        }

        // Show colored diff
        for line in change.diff.lines().take(20) {
            if line.starts_with('+') && !line.starts_with("+++") {
                println!("   {}", line.green());
            } else if line.starts_with('-') && !line.starts_with("---") {
                println!("   {}", line.red());
            } else if line.starts_with('@') {
                println!("   {}", line.cyan());
            } else {
                println!("   {}", line);
            }
        }

        if change.diff.lines().count() > 20 {
            println!("   ... ({} more lines)", change.diff.lines().count() - 20);
        }
        println!();
    }

    Ok(())
}

/// View or manage AI agent memory for a watch
pub fn cmd_memory(id_or_name: &str, json: bool, clear: bool) -> Result<()> {
    let db = Database::open()?;
    let watch = db.get_watch(id_or_name)?
        .ok_or_else(|| kto::KtoError::WatchNotFound(id_or_name.to_string()))?;

    if clear {
        // Clear memory
        db.clear_agent_memory(&watch.id)?;
        println!("Cleared AI memory for '{}'.", watch.name);
        return Ok(());
    }

    let memory = db.get_agent_memory(&watch.id)?;

    if json {
        println!("{}", serde_json::to_string_pretty(&memory)?);
        return Ok(());
    }

    println!("\nAI Memory for '{}':\n", watch.name);

    // Counters
    if memory.counters.is_empty() {
        println!("  Counters: (none)");
    } else {
        println!("  {}:", "Counters".bold());
        for (key, value) in &memory.counters {
            println!("    {}: {}", key, value.to_string().cyan());
        }
    }
    println!();

    // Last Values
    if memory.last_values.is_empty() {
        println!("  Last Values: (none)");
    } else {
        println!("  {}:", "Last Values".bold());
        for (key, value) in &memory.last_values {
            let display = match value {
                serde_json::Value::String(s) => s.clone(),
                serde_json::Value::Number(n) => n.to_string(),
                _ => value.to_string(),
            };
            println!("    {}: {}", key, display.green());
        }
    }
    println!();

    // Notes
    if memory.notes.is_empty() {
        println!("  Notes: (none)");
    } else {
        println!("  {}:", "Notes".bold());
        for note in &memory.notes {
            println!("{}", note.yellow());
        }
    }

    // Agent config info
    if let Some(ref config) = watch.agent_config {
        println!();
        println!("  {}:", "Agent Config".bold());
        println!("    Enabled: {}", if config.enabled { "yes".green() } else { "no".red() });
        if let Some(ref inst) = config.instructions {
            println!("    Intent: \"{}\"", inst);
        }
    }

    println!();
    println!("  Tip: Use 'kto memory \"{}\" --clear' to reset memory.", watch.name);

    Ok(())
}

/// Skip reason for structured logging
#[derive(Debug, Clone, Copy, PartialEq)]
enum SkipReason {
    /// Filter rules didn't match
    Filter,
    /// AI decided not to notify
    AiSkip,
    /// Within quiet hours
    QuietHours,
    /// No skip - was notified
    None,
}

impl SkipReason {
    fn code(&self) -> &'static str {
        match self {
            SkipReason::Filter => "filter",
            SkipReason::AiSkip => "ai_skip",
            SkipReason::QuietHours => "quiet",
            SkipReason::None => "notified",
        }
    }

    fn label(&self) -> &'static str {
        match self {
            SkipReason::Filter => "⊘ filtered",
            SkipReason::AiSkip => "○ AI skip",
            SkipReason::QuietHours => "◑ quiet",
            SkipReason::None => "✓ sent",
        }
    }
}

/// Determine skip reason from change data
fn get_skip_reason(change: &Change) -> SkipReason {
    if change.notified {
        return SkipReason::None;
    }
    if !change.filter_passed {
        return SkipReason::Filter;
    }
    // Check if AI said not to notify
    if let Some(ref resp) = change.agent_response {
        if let Some(notify) = resp.get("notify").and_then(|v| v.as_bool()) {
            if !notify {
                return SkipReason::AiSkip;
            }
        }
    }
    // Default to quiet hours if notified=false but filter passed and AI didn't skip
    SkipReason::QuietHours
}

/// Truncate string at word boundary, preserving UTF-8
fn truncate_at_word(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        return s.to_string();
    }

    // Find the last space before max_len
    let truncated: String = s.chars().take(max_len).collect();
    if let Some(last_space) = truncated.rfind(' ') {
        if last_space > max_len / 2 {
            return format!("{}", &truncated[..last_space]);
        }
    }
    format!("{}", truncated)
}

/// Clean diff markers: convert [-old][+new] to "-old +new" format
fn clean_diff_preview(diff: &str, max_len: usize) -> String {
    let mut result = String::new();
    let mut chars = diff.chars().peekable();

    while let Some(c) = chars.next() {
        if c == '[' {
            // Check for [-...] or [+...]
            if let Some(&marker) = chars.peek() {
                if marker == '-' || marker == '+' {
                    chars.next(); // consume marker
                    result.push(marker);
                    // Collect until ]
                    while let Some(inner) = chars.next() {
                        if inner == ']' {
                            result.push(' ');
                            break;
                        }
                        result.push(inner);
                    }
                    continue;
                }
            }
        }
        if c == '\n' {
            result.push(' ');
        } else {
            result.push(c);
        }
    }

    // Clean up multiple spaces
    let cleaned: String = result.split_whitespace().collect::<Vec<_>>().join(" ");
    truncate_at_word(&cleaned, max_len)
}

/// Get a human-readable summary from the change
fn get_change_summary(change: &Change) -> String {
    // Priority 1: AI title
    if let Some(ref resp) = change.agent_response {
        if let Some(title) = resp.get("title").and_then(|v| v.as_str()) {
            if !title.is_empty() {
                return truncate_at_word(title, 50);
            }
        }
        // Priority 2: AI summary
        if let Some(summary) = resp.get("summary").and_then(|v| v.as_str()) {
            if !summary.is_empty() {
                return truncate_at_word(summary, 50);
            }
        }
    }
    // Priority 3: Cleaned diff preview
    clean_diff_preview(&change.diff, 50)
}

/// Tail activity log
pub fn cmd_logs(lines: usize, follow: bool, json: bool) -> Result<()> {
    let db = Database::open()?;

    // Initial display
    let changes = db.get_all_recent_changes(lines)?;

    if changes.is_empty() {
        if json {
            println!("[]");
        } else {
            println!("No changes recorded yet.");
        }
        if !follow {
            return Ok(());
        }
    } else if json {
        print_changes_json(&changes);
    } else {
        println!("\nRecent changes:\n");
        for (change, watch_name) in &changes {
            print_change_log(change, watch_name);
        }
    }

    if follow {
        if !json {
            println!("\nWatching for new changes... (Ctrl+C to stop)\n");
        }

        let mut last_seen = changes.first().map(|(c, _)| c.detected_at);

        loop {
            std::thread::sleep(Duration::from_secs(2));

            let new_changes = db.get_all_recent_changes(10)?;

            for (change, watch_name) in new_changes {
                if let Some(last) = last_seen {
                    if change.detected_at <= last {
                        continue;
                    }
                }
                if json {
                    print_change_json(&change, &watch_name);
                } else {
                    print_change_log(&change, &watch_name);
                }
                last_seen = Some(change.detected_at);
            }
        }
    }

    Ok(())
}

fn print_changes_json(changes: &[(Change, String)]) {
    let entries: Vec<serde_json::Value> = changes.iter().map(|(change, watch_name)| {
        change_to_json(change, watch_name)
    }).collect();

    if let Ok(json) = serde_json::to_string_pretty(&entries) {
        println!("{}", json);
    }
}

fn print_change_json(change: &Change, watch_name: &str) {
    if let Ok(json) = serde_json::to_string(&change_to_json(change, watch_name)) {
        println!("{}", json);
    }
}

fn change_to_json(change: &Change, watch_name: &str) -> serde_json::Value {
    let skip_reason = get_skip_reason(change);

    let mut entry = serde_json::json!({
        "timestamp": change.detected_at.to_rfc3339(),
        "watch": watch_name,
        "status": skip_reason.code(),
        "notified": change.notified,
        "filter_passed": change.filter_passed,
        "diff_size": change.diff.len(),
    });

    // Add AI response fields if present
    if let Some(ref resp) = change.agent_response {
        if let Some(title) = resp.get("title").and_then(|v| v.as_str()) {
            entry["ai_title"] = serde_json::Value::String(title.to_string());
        }
        if let Some(summary) = resp.get("summary").and_then(|v| v.as_str()) {
            entry["ai_summary"] = serde_json::Value::String(summary.to_string());
        }
        if let Some(notify) = resp.get("notify").and_then(|v| v.as_bool()) {
            entry["ai_notify"] = serde_json::Value::Bool(notify);
        }
    }

    entry
}

fn print_change_log(change: &Change, watch_name: &str) {
    let time = change.detected_at.format("%Y-%m-%d %H:%M:%S");
    let skip_reason = get_skip_reason(change);

    // Truncate watch name to keep alignment
    let watch_display = truncate_at_word(watch_name, 20);

    // Get the best available summary
    let summary = get_change_summary(change);

    // Use colored output for status
    let status_display = match skip_reason {
        SkipReason::None => skip_reason.label().green(),
        SkipReason::Filter => skip_reason.label().yellow(),
        SkipReason::AiSkip => skip_reason.label().blue(),
        SkipReason::QuietHours => skip_reason.label().cyan(),
    };

    println!("  {} │ {:20} │ {:12} │ {}",
             time, watch_display, status_display, summary);
}

/// Check all dependencies and suggest fixes
pub fn cmd_doctor() -> Result<()> {
    println!("\nkto doctor\n");

    // Check kto itself
    println!("  kto binary: v{}", env!("CARGO_PKG_VERSION"));

    // Check Claude CLI
    match agent::claude_version() {
        Some(v) => println!("  Claude CLI: {} (installed)", v),
        None => println!("  Claude CLI: NOT INSTALLED"),
    }

    // Check Node.js
    let node = std::process::Command::new("node")
        .arg("--version")
        .output();
    match node {
        Ok(o) if o.status.success() => {
            let v = String::from_utf8_lossy(&o.stdout);
            println!("  Node.js: {}", v.trim());
        }
        _ => println!("  Node.js: NOT INSTALLED"),
    }

    // Check Playwright
    match check_playwright() {
        PlaywrightStatus::Ready => println!("  Playwright: ready"),
        PlaywrightStatus::NodeMissing => println!("  Playwright: Node.js required"),
        PlaywrightStatus::PlaywrightMissing => println!("  Playwright: not installed"),
        PlaywrightStatus::BrowserMissing => println!("  Playwright: browser not installed"),
    }

    // Check database
    match Database::open() {
        Ok(_) => println!("  Database: OK"),
        Err(e) => println!("  Database: ERROR - {}", e),
    }

    println!();
    Ok(())
}

/// Set up Playwright/Chromium for JS rendering
pub fn cmd_enable_js() -> Result<()> {
    println!("\nSetting up JavaScript rendering...\n");

    // Check if Node.js is available
    let node_available = std::process::Command::new("node")
        .arg("--version")
        .output()
        .map(|o| o.status.success())
        .unwrap_or(false);

    if !node_available {
        println!("  Node.js is required for JavaScript rendering.");
        println!("  Install from: https://nodejs.org/");
        return Ok(());
    }

    let data_dir = Config::data_dir()?;
    std::fs::create_dir_all(&data_dir)?;

    // Check if playwright is already installed locally
    let node_modules = data_dir.join("node_modules").join("playwright");
    let needs_install = !node_modules.exists();

    if needs_install {
        // Create package.json if it doesn't exist
        let package_json = data_dir.join("package.json");
        if !package_json.exists() {
            println!("  Initializing kto JavaScript environment...");
            let output = std::process::Command::new("npm")
                .args(["init", "-y"])
                .current_dir(&data_dir)
                .output()?;

            if !output.status.success() {
                return Err(kto::KtoError::PlaywrightError("Failed to initialize npm".into()));
            }
        }

        // Install playwright locally in the data directory
        println!("  Installing Playwright...");
        let output = std::process::Command::new("npm")
            .args(["install", "playwright"])
            .current_dir(&data_dir)
            .status()?;

        if !output.success() {
            return Err(kto::KtoError::PlaywrightError("Failed to install Playwright".into()));
        }
    } else {
        println!("  Playwright package is installed.");
    }

    // Check if Chromium browser is installed
    let status = check_playwright();
    if !status.is_ready() {
        println!("  Installing Chromium browser (~280MB)...");
        let output = std::process::Command::new("npx")
            .args(["playwright", "install", "chromium"])
            .current_dir(&data_dir)
            .status()?;

        if !output.success() {
            return Err(kto::KtoError::PlaywrightError("Failed to install Chromium".into()));
        }
    } else {
        println!("  Chromium browser is installed.");
    }

    // Install system dependencies (requires sudo for system packages)
    // Check if we're in an interactive terminal
    if atty::is(atty::Stream::Stdin) {
        println!("  Installing system dependencies for Chromium...");
        println!("  (This requires sudo - you may be prompted for your password)\n");

        // Run sudo with inherited stdio so user can enter password interactively
        let deps_result = std::process::Command::new("sudo")
            .args(["npx", "playwright", "install-deps", "chromium"])
            .current_dir(&data_dir)
            .stdin(std::process::Stdio::inherit())
            .stdout(std::process::Stdio::inherit())
            .stderr(std::process::Stdio::inherit())
            .status();

        match deps_result {
            Ok(status) if status.success() => {
                println!("\n  System dependencies installed.");
            }
            Ok(_) => {
                return Err(kto::KtoError::PlaywrightError(
                    "Failed to install system dependencies".into()
                ));
            }
            Err(e) => {
                return Err(kto::KtoError::PlaywrightError(
                    format!("Failed to run sudo: {}", e)
                ));
            }
        }
    } else {
        // Non-interactive mode - can't prompt for sudo password
        return Err(kto::KtoError::PlaywrightError(
            "Cannot install system dependencies in non-interactive mode. Please run `kto enable-js` in a terminal.".into()
        ));
    }

    // Ensure render script is in place
    fetch::ensure_render_script()?;

    println!("\n  JavaScript rendering is now enabled.");
    println!("  Create a watch with: kto new \"https://...\" --js");
    Ok(())
}

/// Generate shell completions
pub fn cmd_completions(shell: CompletionShell) -> Result<()> {
    let mut cmd = Cli::command();
    let shell = match shell {
        CompletionShell::Bash => Shell::Bash,
        CompletionShell::Zsh => Shell::Zsh,
        CompletionShell::Fish => Shell::Fish,
        CompletionShell::Powershell => Shell::PowerShell,
    };
    generate(shell, &mut cmd, "kto", &mut io::stdout());
    Ok(())
}

/// Interactive first-time setup wizard
pub fn cmd_init() -> Result<()> {
    use inquire::{Confirm, Password, Select, Text};

    println!("\n{}", "Welcome to kto!".bold());
    println!("This wizard will help you set up kto for the first time.\n");

    // Step 1: Run doctor
    println!("{}", "Step 1: Checking dependencies...".bold());
    println!();
    cmd_doctor()?;

    // Check if Claude CLI is available
    let claude_available = agent::claude_version().is_some();
    if !claude_available {
        println!("\n{}: Claude CLI not found. AI features will be unavailable.", "Note".yellow());
        println!("Install from: https://claude.ai/cli\n");
    }

    // Check if Playwright/JS rendering is available
    let playwright_status = fetch::check_playwright();
    if !playwright_status.is_ready() {
        let setup_js = Confirm::new("JavaScript rendering is not set up. Install it now?")
            .with_default(false)
            .with_help_message("Required for JS-heavy sites (SPAs). Downloads ~280MB")
            .prompt();

        match setup_js {
            Ok(true) => {
                println!("\n  Setting up JavaScript rendering...\n");
                if let Err(e) = cmd_enable_js() {
                    println!("  {}: JS setup failed: {}", "Warning".yellow(), e);
                    println!("  You can try again later with: kto enable-js\n");
                }
            }
            Ok(false) | Err(_) => {
                println!("  Skipping JS setup. Run `kto enable-js` later if needed.\n");
            }
        }
    }

    // Step 2: Set up notifications
    println!("\n{}", "Step 2: Set up notifications".bold());

    let config = Config::load()?;

    // Determine if we should run notification setup
    let run_setup = if let Some(ref target) = config.default_notify {
        println!("  Current: {}", crate::commands::describe_notify_target(target));
        let change = Confirm::new("Would you like to change notification settings?")
            .with_default(false)
            .prompt();
        matches!(change, Ok(true))
    } else {
        let setup = Confirm::new("Would you like to set up notifications now?")
            .with_default(true)
            .prompt();
        matches!(setup, Ok(true))
    };

    if run_setup {
        let options = vec![
            "ntfy (recommended - free, simple push notifications)",
            "Slack (webhook)",
            "Discord (webhook)",
            "Telegram (bot)",
            "Gotify (self-hosted)",
            "Pushover",
            "Matrix",
            "Skip for now",
        ];

        let choice = Select::new("Which notification service would you like to use?", options)
            .prompt();

        match choice {
            Ok(selected) => {
                if selected.starts_with("ntfy") {
                    let topic = Text::new("Enter your ntfy topic (e.g., my-alerts):")
                        .prompt();
                    if let Ok(topic) = topic {
                        if !topic.is_empty() {
                            crate::commands::cmd_notify_set(
                                Some(topic), None, None, None, None, None,
                                None, None, None, None, None, None, None,
                            )?;
                            println!("  {} ntfy notifications configured!", "".green());
                        }
                    }
                } else if selected.starts_with("Slack") {
                    let webhook = Password::new("Enter your Slack webhook URL:")
                        .without_confirmation()
                        .prompt();
                    if let Ok(webhook) = webhook {
                        if !webhook.is_empty() {
                            crate::commands::cmd_notify_set(
                                None, Some(webhook), None, None, None, None,
                                None, None, None, None, None, None, None,
                            )?;
                            println!("  {} Slack notifications configured!", "".green());
                        }
                    }
                } else if selected.starts_with("Discord") {
                    let webhook = Password::new("Enter your Discord webhook URL:")
                        .without_confirmation()
                        .prompt();
                    if let Ok(webhook) = webhook {
                        if !webhook.is_empty() {
                            crate::commands::cmd_notify_set(
                                None, None, Some(webhook), None, None, None,
                                None, None, None, None, None, None, None,
                            )?;
                            println!("  {} Discord notifications configured!", "".green());
                        }
                    }
                } else if selected.starts_with("Telegram") {
                    let token = Password::new("Enter your Telegram bot token:")
                        .without_confirmation()
                        .prompt();
                    let chat = Text::new("Enter your Telegram chat ID:")
                        .prompt();
                    if let (Ok(token), Ok(chat)) = (token, chat) {
                        if !token.is_empty() && !chat.is_empty() {
                            crate::commands::cmd_notify_set(
                                None, None, None, None, None, None,
                                Some(token), Some(chat), None, None, None, None, None,
                            )?;
                            println!("  {} Telegram notifications configured!", "".green());
                        }
                    }
                } else if selected.starts_with("Gotify") {
                    let server = Text::new("Enter your Gotify server URL:")
                        .prompt();
                    let token = Password::new("Enter your Gotify app token:")
                        .without_confirmation()
                        .prompt();
                    if let (Ok(server), Ok(token)) = (server, token) {
                        if !server.is_empty() && !token.is_empty() {
                            crate::commands::cmd_notify_set(
                                None, None, None, Some(server), Some(token), None,
                                None, None, None, None, None, None, None,
                            )?;
                            println!("  {} Gotify notifications configured!", "".green());
                        }
                    }
                } else if selected.starts_with("Pushover") {
                    let user = Password::new("Enter your Pushover user key:")
                        .without_confirmation()
                        .prompt();
                    let token = Password::new("Enter your Pushover API token:")
                        .without_confirmation()
                        .prompt();
                    if let (Ok(user), Ok(token)) = (user, token) {
                        if !user.is_empty() && !token.is_empty() {
                            crate::commands::cmd_notify_set(
                                None, None, None, None, None, None,
                                None, None, Some(user), Some(token), None, None, None,
                            )?;
                            println!("  {} Pushover notifications configured!", "".green());
                        }
                    }
                } else if selected.starts_with("Matrix") {
                    let server = Text::new("Enter your Matrix homeserver URL:")
                        .prompt();
                    let room = Text::new("Enter your Matrix room ID:")
                        .prompt();
                    let token = Password::new("Enter your Matrix access token:")
                        .without_confirmation()
                        .prompt();
                    if let (Ok(server), Ok(room), Ok(token)) = (server, room, token) {
                        if !server.is_empty() && !room.is_empty() && !token.is_empty() {
                            crate::commands::cmd_notify_set(
                                None, None, None, None, None, None,
                                None, None, None, None, Some(server), Some(room), Some(token),
                            )?;
                            println!("  {} Matrix notifications configured!", "".green());
                        }
                    }
                } else {
                    println!("  Skipping notification setup.");
                }
            }
            Err(_) => {
                println!("  Skipping notification setup.");
            }
        }
    } else {
        if config.default_notify.is_some() {
            println!("  Keeping current notification settings.");
        } else {
            println!("  Skipping notification setup.");
            println!("  You can configure notifications later with: kto notify set");
        }
    }

    // Step 3: Create first watch
    println!("\n{}", "Step 3: Create your first watch".bold());

    let db = Database::open()?;
    let watches = db.list_watches()?;

    if !watches.is_empty() {
        println!("  You already have {} watch(es) configured.", watches.len());
    } else {
        let create_watch = Confirm::new("Would you like to create your first watch now?")
            .with_default(true)
            .prompt();

        match create_watch {
            Ok(true) => {
                let url = Text::new("Enter a URL to watch (or describe what you want to monitor):")
                    .with_help_message("e.g., https://news.ycombinator.com for AI news")
                    .prompt();

                if let Ok(url) = url {
                    if !url.is_empty() {
                        // Call the new command
                        crate::commands::cmd_new(
                            Some(url),
                            None,
                            "15m".to_string(),
                            false, false, false,
                            claude_available, // Enable AI if available
                            None, None, false,
                            vec![], false, false, false,
                        )?;
                    }
                }
            }
            Ok(false) | Err(_) => {
                println!("  Skipping watch creation.");
                println!("  You can create a watch later with: kto new \"https://...\"");
            }
        }
    }

    // Step 4: Install as service
    println!("\n{}", "Step 4: Run in background".bold());

    let install_service = Confirm::new("Would you like to install kto as a background service?")
        .with_default(true)
        .with_help_message("Recommended: kto will check your watches automatically")
        .prompt();

    match install_service {
        Ok(true) => {
            crate::commands::cmd_service_install(false, 5)?;
        }
        Ok(false) | Err(_) => {
            println!("  Skipping service installation.");
            println!("  You can install the service later with: kto service install");
        }
    }

    // Step 5: Shell completions
    println!("\n{}", "Step 5: Shell completions".bold());

    // Detect shell from SHELL env var
    let shell_path = std::env::var("SHELL").unwrap_or_default();
    let detected_shell = if shell_path.contains("zsh") {
        Some(("zsh", "~/.zshrc"))
    } else if shell_path.contains("bash") {
        Some(("bash", "~/.bashrc"))
    } else if shell_path.contains("fish") {
        Some(("fish", "~/.config/fish/completions/kto.fish"))
    } else {
        None
    };

    if let Some((shell_name, config_path)) = detected_shell {
        println!("  Detected shell: {}", shell_name);

        let install_completions = Confirm::new(&format!("Add tab completions to {}?", config_path))
            .with_default(true)
            .prompt();

        match install_completions {
            Ok(true) => {
                let home = std::env::var("HOME").unwrap_or_default();
                let full_path = config_path.replace("~", &home);

                // For fish, we need to create the directory and write the file directly
                if shell_name == "fish" {
                    let fish_dir = format!("{}/.config/fish/completions", home);
                    if let Err(e) = std::fs::create_dir_all(&fish_dir) {
                        println!("  {}: Could not create fish completions directory: {}", "Warning".yellow(), e);
                    } else {
                        // Generate completions directly to file
                        let fish_path = format!("{}/kto.fish", fish_dir);
                        match std::fs::File::create(&fish_path) {
                            Ok(mut file) => {
                                let mut cmd = Cli::command();
                                clap_complete::generate(Shell::Fish, &mut cmd, "kto", &mut file);
                                println!("  {} Completions written to {}", "".green(), fish_path);
                            }
                            Err(e) => {
                                println!("  {}: Could not write completions: {}", "Warning".yellow(), e);
                            }
                        }
                    }
                } else {
                    // For bash/zsh, append eval to rc file
                    let completion_line = format!("\n# kto shell completions\neval \"$(kto completions {})\"\n", shell_name);

                    // Check if already installed
                    let existing = std::fs::read_to_string(&full_path).unwrap_or_default();
                    if existing.contains("kto completions") {
                        println!("  Completions already configured in {}", config_path);
                    } else {
                        match std::fs::OpenOptions::new().append(true).open(&full_path) {
                            Ok(mut file) => {
                                use std::io::Write;
                                if let Err(e) = file.write_all(completion_line.as_bytes()) {
                                    println!("  {}: Could not write to {}: {}", "Warning".yellow(), config_path, e);
                                } else {
                                    println!("  {} Completions added to {}", "".green(), config_path);
                                    println!("  Run `source {}` or restart your shell to activate", config_path);
                                }
                            }
                            Err(e) => {
                                println!("  {}: Could not open {}: {}", "Warning".yellow(), config_path, e);
                                println!("  You can manually add: eval \"$(kto completions {})\"", shell_name);
                            }
                        }
                    }
                }
            }
            Ok(false) | Err(_) => {
                println!("  Skipping completions setup.");
                println!("  You can add them later with: kto completions {}", shell_name);
            }
        }
    } else {
        println!("  Could not detect shell. You can manually set up completions:");
        println!("    kto completions bash >> ~/.bashrc");
        println!("    kto completions zsh >> ~/.zshrc");
        println!("    kto completions fish > ~/.config/fish/completions/kto.fish");
    }

    // Done
    println!("\n{}", "Setup complete!".green().bold());
    println!();
    println!("Useful commands:");
    println!("  kto list              List all watches");
    println!("  kto new \"https://...\" Create a new watch");
    println!("  kto ui                Interactive dashboard");
    println!("  kto service status    Check service status");
    println!("  kto --help            Show all commands");
    println!();

    Ok(())
}