kto 0.1.3

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
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
//! Watch management commands: new, list, show, edit, delete, pause, resume

use std::thread;

use chrono::Utc;
use colored::Colorize;
use inquire::{Confirm, Select, Text};
use uuid::Uuid;

use kto::agent::{self, EnhancedSetupSuggestion};
use kto::config::Config;
use kto::db::Database;
use kto::extract;
use kto::fetch::{self, check_playwright, PageContent, PlaywrightStatus};
use kto::normalize::{hash_content, normalize};
use kto::watch::{AgentConfig, Engine, Extraction, Snapshot, Watch};
use kto::error::Result;

use crate::utils::{extract_url, format_interval, get_clipboard_content, parse_interval_str, truncate_str};
use super::prompt_notification_setup;

/// Confidence threshold below which we show low-confidence UI
const CONFIDENCE_THRESHOLD: f32 = 0.7;

/// Create a new watch
pub fn cmd_new(
    description: Option<String>,
    name_override: Option<String>,
    interval_str: String,
    use_js: bool,
    use_rss: bool,
    use_shell: bool,
    use_agent: bool,
    agent_instructions: Option<String>,
    selector: Option<String>,
    clipboard: bool,
    tags: Vec<String>,
    use_profile: bool,
    yes: bool,
) -> Result<()> {
    let db = Database::open()?;

    // Parse interval (supports 30s, 5m, 2h, 1d, 1w formats)
    let interval = parse_interval_str(&interval_str)?;

    // --yes requires a description
    if yes && description.is_none() && !clipboard {
        return Err(kto::KtoError::ConfigError(
            "--yes requires a description argument or --clipboard".into()
        ));
    }

    // Determine if we're in interactive mode (--yes disables interactivity)
    let interactive = !yes && name_override.is_none() && atty::is(atty::Stream::Stdin);

    // Get the description/URL from user or clipboard
    let input = if clipboard {
        // Try to read from clipboard
        match get_clipboard_content() {
            Some(content) => {
                println!("  Read from clipboard: {}", truncate_str(&content, 60));
                content
            }
            None => {
                return Err(kto::KtoError::ConfigError(
                    "Could not read from clipboard. Make sure you have content copied.".into()
                ));
            }
        }
    } else {
        match description {
            Some(d) => d,
            None if interactive => {
                Text::new("What do you want to watch?")
                    .with_help_message("Enter a URL and optionally describe what to watch for")
                    .prompt()
                    .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?
            }
            None => {
                return Err(kto::KtoError::ConfigError(
                    "URL required. Usage: kto new <URL> --name <NAME>".into()
                ));
            }
        }
    };

    // Handle shell command case - input is the command, not a URL
    if use_shell {
        let command = input.trim().to_string();
        let name = name_override.unwrap_or_else(|| {
            // Generate name from command (first word or truncated)
            let first_word = command.split_whitespace().next().unwrap_or("shell");
            format!("shell:{}", first_word)
        });

        // Execute command to get initial content
        println!("\n  Executing: {}", command);
        let content = fetch::fetch("", Engine::Shell { command: command.clone() }, &std::collections::HashMap::new())?;
        let extracted = content.text.clone().unwrap_or_default();

        if extracted.is_empty() {
            println!("  Warning: Command produced no output.");
        } else {
            println!("  Got {} bytes of output.", extracted.len());
        }

        // Create watch with shell engine
        let mut watch = Watch::new(name.clone(), format!("shell://{}", command));
        watch.interval_secs = interval.max(10);
        watch.engine = Engine::Shell { command };
        watch.extraction = Extraction::Full;
        watch.tags = tags;

        // Configure agent if requested
        if use_agent {
            watch.agent_config = Some(AgentConfig {
                enabled: true,
                prompt_template: None,
                instructions: agent_instructions,
            });
        }

        let db = Database::open()?;
        db.insert_watch(&watch)?;

        // Create initial snapshot
        let normalized = normalize(&extracted, &watch.normalization);
        let hash = hash_content(&normalized);

        let snapshot = Snapshot {
            id: Uuid::new_v4(),
            watch_id: watch.id,
            fetched_at: Utc::now(),
            raw_html: None, // No HTML for shell commands
            extracted: normalized,
            content_hash: hash.clone(),
        };
        db.insert_snapshot(&snapshot)?;

        println!("\n  Created shell watch \"{}\"", name);
        println!("  Initial hash: {}", &hash[..8]);
        if watch.agent_config.is_some() {
            println!("  AI Agent: enabled");
        }
        if !watch.tags.is_empty() {
            println!("  Tags: {}", watch.tags.join(", "));
        }
        println!("  Checking every {}", format_interval(watch.interval_secs));
        println!("\n  Run `kto daemon` to start monitoring.");

        return Ok(());
    }

    // Try to extract URL from input
    let url = extract_url(&input).ok_or_else(|| {
        kto::KtoError::ConfigError("Could not find a valid URL in your input".into())
    })?;

    // Detect if user expressed intent (what to watch for)
    let has_intent = input.contains(" for ") || input.contains(" when ") || input.contains(" if ")
        || input.contains("watch for") || input.contains("notify me") || input.contains("alert")
        || input.contains("price") || input.contains("stock") || input.contains("available")
        || input.contains("back in") || input.contains("drop");

    // Check if Claude CLI is available for enhanced wizard
    let claude_available = agent::claude_version().is_some();

    // Use enhanced wizard flow when intent detected and Claude available
    // Works in both interactive and --yes mode (auto-accepts in --yes mode)
    let use_enhanced_wizard = has_intent && claude_available && !use_agent && !use_rss && !use_shell;

    // Enhanced wizard flow with dual fetch and smart analysis
    let (engine, content, extracted, title, enhanced_suggestion) = if use_enhanced_wizard {
        println!("\n  Analyzing {}...", url);

        // Perform dual fetch: HTTP and Playwright in parallel
        let (http_content, js_content) = dual_fetch(&url)?;

        // Extract content from both fetches
        let http_extracted = http_content.as_ref()
            .and_then(|c| extract::extract(c, &Extraction::Auto).ok());
        let js_extracted = js_content.as_ref()
            .and_then(|c| extract::extract(c, &Extraction::Auto).ok());

        // Get title from whichever fetch succeeded
        let title = js_content.as_ref()
            .and_then(|c| extract::extract_title(&c.html))
            .or_else(|| http_content.as_ref().and_then(|c| extract::extract_title(&c.html)))
            .unwrap_or_else(|| "Untitled".to_string());

        // Call enhanced AI analysis with both content versions
        println!("  Analyzing with AI (dual fetch)...");
        let suggestion = match agent::analyze_for_setup_v2(
            &input,
            http_extracted.as_deref(),
            js_extracted.as_deref(),
        ) {
            Ok(s) => s,
            Err(e) => {
                eprintln!("  AI analysis failed: {} (using fallback)", e);
                EnhancedSetupSuggestion::fallback(&url, &input)
            }
        };

        // Determine which content/engine to use based on AI recommendation
        let (final_engine, final_content) = if suggestion.needs_js && js_content.is_some() {
            (Engine::Playwright, js_content.unwrap())
        } else if http_content.is_some() {
            (Engine::Http, http_content.unwrap())
        } else if js_content.is_some() {
            (Engine::Playwright, js_content.unwrap())
        } else {
            return Err(kto::KtoError::ConfigError("Both HTTP and JS fetches failed".into()));
        };

        let final_extracted = if suggestion.needs_js && js_extracted.is_some() {
            js_extracted.unwrap()
        } else {
            http_extracted.or(js_extracted).unwrap_or_default()
        };

        (final_engine, final_content, final_extracted, title, Some(suggestion))
    } else {
        // Traditional flow: determine engine first, then fetch

        // Determine engine to use - with smart probing in interactive mode
        let engine = if use_rss {
            // Validate RSS flag - warn if URL doesn't look like RSS
            if !fetch::detect_rss_url(&url) {
                eprintln!("  Note: URL doesn't look like an RSS feed, but --rss was specified.");
                eprintln!("  Will attempt to parse as RSS anyway.");
            }
            Engine::Rss
        } else if use_js {
            // Check if Playwright is available
            match check_playwright() {
                PlaywrightStatus::Ready => Engine::Playwright,
                status => {
                    eprintln!("  Warning: Playwright not ready. {}", status.install_instructions());
                    eprintln!("  Falling back to HTTP fetch.");
                    Engine::Http
                }
            }
        } else if interactive {
            // In interactive mode, probe the URL to suggest the best engine
            println!("\n  Analyzing {}...", url);
            match fetch::probe_url(&url) {
                Ok(probe) => {
                    // Show what we found
                    if let Some(ref msg) = probe.message {
                        println!("  {}", msg);
                    }

                    // If RSS detected in content or URL, offer to use it
                    if probe.suggested_engine == Engine::Rss {
                        println!("  Using RSS engine.");
                        Engine::Rss
                    }
                    // If RSS link found in page, offer to use it instead
                    else if let Some(ref rss_link) = probe.rss_url {
                        let use_rss = Confirm::new(&format!("RSS feed found at {}. Use that instead?", rss_link))
                            .with_default(true)
                            .prompt()
                            .unwrap_or(false);
                        if use_rss {
                            // Note: we'd need to change the URL too - for now just suggest
                            println!("  Tip: Run `kto new \"{}\" --rss` to watch the feed directly.", rss_link);
                            probe.suggested_engine
                        } else {
                            probe.suggested_engine
                        }
                    }
                    // If Playwright suggested
                    else if probe.suggested_engine == Engine::Playwright {
                        // Check if available
                        match check_playwright() {
                            PlaywrightStatus::Ready => {
                                let use_js = Confirm::new("Enable JavaScript rendering?")
                                    .with_default(true)
                                    .prompt()
                                    .unwrap_or(false);
                                if use_js { Engine::Playwright } else { Engine::Http }
                            }
                            status => {
                                println!("  JavaScript rendering recommended but not available.");
                                println!("  {}", status.install_instructions());
                                Engine::Http
                            }
                        }
                    } else {
                        probe.suggested_engine
                    }
                }
                Err(e) => {
                    // Probe failed, fall back to simple URL pattern detection
                    eprintln!("  Could not analyze page: {}", e);
                    if fetch::detect_rss_url(&url) {
                        println!("  URL looks like RSS feed, using RSS engine.");
                        Engine::Rss
                    } else {
                        Engine::Http
                    }
                }
            }
        } else if fetch::detect_rss_url(&url) {
            // Non-interactive: auto-detect RSS from URL pattern
            println!("\n  Detected RSS feed URL, using RSS engine.");
            Engine::Rss
        } else {
            Engine::Http
        };

        let engine_label = match &engine {
            Engine::Playwright => " (with JS)".to_string(),
            Engine::Rss => " (as RSS feed)".to_string(),
            Engine::Http => "".to_string(),
            Engine::Shell { .. } => " (shell command)".to_string(),
        };
        println!("  Fetching {}{}...", url, engine_label);

        // Fetch the page
        let content = fetch::fetch(&url, engine.clone(), &std::collections::HashMap::new())?;

        // Determine extraction strategy
        let extraction = match (&selector, &engine) {
            (Some(ref sel), _) => Extraction::Selector { selector: sel.clone() },
            (None, Engine::Rss) => Extraction::Rss,
            (None, _) => Extraction::Auto,
        };

        // Extract content
        let extracted = extract::extract(&content, &extraction)?;
        let title = extract::extract_title(&content.html)
            .unwrap_or_else(|| "Untitled".to_string());

        // Check if extraction got reasonable content, suggest JS if not
        if extracted.len() < 50 && !use_js {
            println!("\n  Warning: Very little content extracted ({} chars).", extracted.len());
            println!("  This page may require JavaScript rendering. Try: kto new <URL> --js");
        }

        (engine, content, extracted, title, None)
    };

    // Determine extraction strategy based on selector or engine
    let extraction = match (&selector, &engine) {
        (Some(ref sel), _) => Extraction::Selector { selector: sel.clone() },
        (None, Engine::Rss) => Extraction::Rss,
        (None, _) => Extraction::Auto,
    };

    // Apply enhanced AI suggestions or use traditional flow
    let (name, final_url, final_interval, final_agent_enabled, final_agent_instructions, final_extraction, final_engine) =
        if let Some(ref suggestion) = enhanced_suggestion {
            // Enhanced wizard flow with variant display
            let result = display_enhanced_confirmation(
                &url,
                suggestion,
                &extraction,
                engine.clone(),
                &name_override,
                interval,
                yes,
            )?;
            result
        } else {
            // Traditional flow - No enhanced AI suggestion
            if !yes {
                let preview: String = extracted.chars().take(200).collect();
                println!("\n  Title: {}", title);
                println!("  Content preview: {}...\n", preview.trim());
            }

            let name = match name_override {
                Some(n) => n,
                None if interactive => {
                    Text::new("Name for this watch?")
                        .with_default(&title)
                        .prompt()
                        .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?
                }
                None => title.clone(),
            };

            // Intent-first flow: ask what changes matter BEFORE asking about AI
            let (agent_enabled, final_instructions) = if use_agent {
                // Explicit --agent flag always enables, use provided instructions
                (true, agent_instructions.clone())
            } else if interactive {
                // Interactive mode: ask about intent first
                println!();
                let intent = Text::new("What changes matter to you?")
                    .with_help_message("e.g., 'price drops', 'new articles', 'back in stock' (Enter to skip)")
                    .prompt()
                    .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;

                if !intent.trim().is_empty() {
                    // User provided intent
                    if claude_available {
                        // Preview the intent and confirm
                        println!();
                        println!("  Intent captured: \"{}\"", intent.trim());

                        // Warn about potential shell escaping issues
                        if !intent.contains('$') && intent.chars().any(|c| c.is_ascii_digit()) {
                            println!("  Note: If you meant a price like $100, make sure the '$' is included.");
                        }

                        println!("  AI will filter changes based on this intent.");
                        (true, Some(intent.trim().to_string()))
                    } else {
                        // No Claude CLI - warn user
                        println!("  Warning: Claude CLI not found. Notifications will be basic.");
                        println!("  Install: curl -fsSL https://claude.ai/install.sh | bash");
                        (false, None)
                    }
                } else {
                    // User skipped intent - don't enable AI
                    (false, None)
                }
            } else {
                // Non-interactive mode: require explicit --agent flag
                (false, agent_instructions.clone())
            };

            (name, url.clone(), interval, agent_enabled, final_instructions, extraction.clone(), engine)
        };

    // Shell safety: warn if instructions contain $ which may have been mangled by bash
    if let Some(ref instructions) = final_agent_instructions {
        if instructions.contains('$') {
            println!("  Note: Instructions contain '$' - if using prices, this looks correct.");
        } else if instructions.chars().any(|c| c.is_ascii_digit()) {
            // Check if there's a number that might have lost its $ prefix
            let has_bare_number = instructions.split_whitespace().any(|word| {
                word.chars().all(|c| c.is_ascii_digit() || c == '.')
                    && word.parse::<f64>().is_ok()
            });
            if has_bare_number && !instructions.contains('$') {
                println!("  Warning: Instructions contain numbers without '$' symbol.");
                println!("  If you meant a price (e.g., $170), the '$' may have been");
                println!("  eaten by bash. Use single quotes: --agent-instructions 'price < $170'");
            }
        }
    }

    // Create watch with final options (enforce minimum interval)
    let mut watch = Watch::new(name.clone(), final_url.clone());
    watch.interval_secs = final_interval.max(10);
    watch.engine = final_engine;
    watch.extraction = final_extraction;
    watch.tags = tags;
    watch.use_profile = use_profile;

    // Configure agent
    if final_agent_enabled {
        watch.agent_config = Some(AgentConfig {
            enabled: true,
            prompt_template: None,
            instructions: final_agent_instructions,
        });
    }

    db.insert_watch(&watch)?;

    // Create initial snapshot
    let normalized = normalize(&extracted, &watch.normalization);
    let hash = hash_content(&normalized);

    let snapshot = Snapshot {
        id: Uuid::new_v4(),
        watch_id: watch.id,
        fetched_at: Utc::now(),
        raw_html: Some(zstd::encode_all(content.html.as_bytes(), 3)?),
        extracted: normalized,
        content_hash: hash.clone(),
    };
    db.insert_snapshot(&snapshot)?;

    println!("\n  Created watch \"{}\"", name);
    println!("  Initial hash: {}", &hash[..8]);
    println!("  Engine: {:?}", watch.engine);
    if watch.agent_config.is_some() {
        println!("  AI Agent: enabled");
    }
    if watch.use_profile {
        println!("  Profile: enabled");
    }
    if !watch.tags.is_empty() {
        println!("  Tags: {}", watch.tags.join(", "));
    }
    println!("  Checking every {}", format_interval(watch.interval_secs));

    // Prompt for notification setup if not configured and interactive (skip with --yes)
    let mut config = Config::load()?;
    if config.default_notify.is_none() && interactive && !yes {
        println!();
        if let Some(target) = prompt_notification_setup()? {
            config.default_notify = Some(target);
            config.save()?;
            println!("  Notification settings saved.");
        }
    }

    println!("\n  Run `kto daemon` to start monitoring.");

    Ok(())
}

/// List all watches
pub fn cmd_list(verbose: bool, tag_filter: Option<String>, json: bool) -> Result<()> {
    let db = Database::open()?;
    let mut watches = db.list_watches()?;

    // Filter by tag if specified
    if let Some(ref tag) = tag_filter {
        watches.retain(|w| w.tags.iter().any(|t| t.eq_ignore_ascii_case(tag)));
    }

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

    if watches.is_empty() {
        if tag_filter.is_some() {
            println!("No watches found with tag '{}'.", tag_filter.unwrap());
        } else {
            println!("No watches configured. Run `kto new` to create one.");
        }
        return Ok(());
    }

    // Check if terminal supports colors
    let use_color = atty::is(atty::Stream::Stdout);

    println!("\nWatches:\n");

    if verbose {
        for watch in watches {
            let status = if watch.enabled {
                if use_color { "active".green().to_string() } else { "active".to_string() }
            } else {
                if use_color { "paused".yellow().to_string() } else { "paused".to_string() }
            };

            println!("  {} ({})", watch.name.bold(), &watch.id.to_string()[..8]);
            println!("    URL:      {}", watch.url);
            println!("    Status:   {}, every {}", status, format_interval(watch.interval_secs));
            println!("    Engine:   {:?}", watch.engine);
            if watch.agent_config.is_some() {
                println!("    AI Agent: enabled");
            }
            if !watch.tags.is_empty() {
                println!("    Tags:     {}", watch.tags.join(", "));
            }
            println!();
        }
    } else {
        // Calculate max widths for alignment
        let max_name_len = watches.iter().map(|w| w.name.len()).max().unwrap_or(20).min(30);

        for watch in watches {
            // Status indicator with color
            let status_indicator = if watch.enabled {
                if use_color { "".green().to_string() } else { "[active]".to_string() }
            } else {
                if use_color { "".yellow().to_string() } else { "[paused]".to_string() }
            };

            // Engine badge (RSS)
            let engine_badge = if watch.engine == Engine::Rss {
                if use_color { " RSS".magenta().to_string() } else { " [RSS]".to_string() }
            } else {
                "".to_string()
            };

            // AI badge
            let ai_badge = if watch.agent_config.is_some() {
                if use_color { " AI".cyan().to_string() } else { " [AI]".to_string() }
            } else {
                "".to_string()
            };

            // Truncate name if too long
            let name = truncate_str(&watch.name, max_name_len);
            let padded_name = format!("{:width$}", name, width = max_name_len);

            // Truncate URL if too long
            let url = truncate_str(&watch.url, 50);

            let interval = format_interval(watch.interval_secs);

            println!("  {} {}{}{} {} ({})",
                     status_indicator,
                     if use_color { padded_name.bold().to_string() } else { padded_name },
                     engine_badge,
                     ai_badge,
                     url.dimmed(),
                     interval);
        }
    }

    println!();
    Ok(())
}

/// Show details of a specific watch
pub fn cmd_show(id_or_name: &str, json: 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()))?;

    // Show recent changes
    let changes = db.get_recent_changes(&watch.id, 5)?;

    if json {
        let output = serde_json::json!({
            "watch": watch,
            "recent_changes": changes
        });
        println!("{}", serde_json::to_string_pretty(&output)?);
        return Ok(());
    }

    println!("\nWatch: {}\n", watch.name);
    println!("  ID:        {}", watch.id);
    println!("  URL:       {}", watch.url);
    println!("  Status:    {}", if watch.enabled { "active" } else { "paused" });
    println!("  Interval:  {}", format_interval(watch.interval_secs));
    println!("  Engine:    {:?}", watch.engine);
    if let Some(ref agent_config) = watch.agent_config {
        println!("  AI Agent:  {}", if agent_config.enabled { "enabled" } else { "disabled" });
        if let Some(ref instructions) = agent_config.instructions {
            println!("  Instructions: {}", instructions);
        }
    }
    if watch.use_profile {
        println!("  Profile:   enabled");
    }
    println!("  Created:   {}", watch.created_at.format("%Y-%m-%d %H:%M"));

    if !changes.is_empty() {
        println!("\n  Recent changes:");
        for change in changes {
            let notified = if change.notified { "notified" } else { "not notified" };
            println!("    {} - {}", change.detected_at.format("%Y-%m-%d %H:%M"), notified);
        }
    }

    Ok(())
}

/// Edit a watch
pub fn cmd_edit(
    id_or_name: &str,
    new_name: Option<String>,
    new_interval: Option<String>,
    new_enabled: Option<bool>,
    new_agent: Option<bool>,
    new_agent_instructions: Option<String>,
    new_selector: Option<String>,
    new_notify: Option<String>,
    new_use_profile: Option<bool>,
) -> Result<()> {
    use inquire::Select;

    let db = Database::open()?;
    let mut watch = db.get_watch(id_or_name)?
        .ok_or_else(|| kto::KtoError::WatchNotFound(id_or_name.to_string()))?;

    let has_flags = new_name.is_some() || new_interval.is_some() || new_enabled.is_some()
        || new_agent.is_some() || new_agent_instructions.is_some() || new_selector.is_some()
        || new_notify.is_some() || new_use_profile.is_some();

    if has_flags {
        // Flag-based editing (non-interactive)
        let mut changes = Vec::new();

        if let Some(name) = new_name {
            watch.name = name.clone();
            changes.push(format!("name -> {}", name));
        }

        if let Some(ref interval_str) = new_interval {
            let interval = parse_interval_str(interval_str)?;
            watch.interval_secs = interval;
            changes.push(format!("interval -> {}", format_interval(interval)));
        }

        if let Some(enabled) = new_enabled {
            watch.enabled = enabled;
            changes.push(format!("enabled -> {}", enabled));
        }

        if let Some(agent) = new_agent {
            if agent {
                if watch.agent_config.is_none() {
                    watch.agent_config = Some(AgentConfig {
                        enabled: true,
                        prompt_template: None,
                        instructions: None,
                    });
                } else if let Some(ref mut config) = watch.agent_config {
                    config.enabled = true;
                }
                changes.push("agent -> enabled".to_string());
            } else {
                if let Some(ref mut config) = watch.agent_config {
                    config.enabled = false;
                }
                changes.push("agent -> disabled".to_string());
            }
        }

        if let Some(instructions) = new_agent_instructions {
            if watch.agent_config.is_none() {
                watch.agent_config = Some(AgentConfig {
                    enabled: true,
                    prompt_template: None,
                    instructions: Some(instructions.clone()),
                });
            } else if let Some(ref mut config) = watch.agent_config {
                config.instructions = Some(instructions.clone());
            }
            changes.push(format!("agent_instructions -> {}", instructions));
        }

        if let Some(selector) = new_selector {
            watch.extraction = Extraction::Selector { selector: selector.clone() };
            changes.push(format!("selector -> {}", selector));
        }

        if let Some(notify_str) = new_notify {
            if notify_str.to_lowercase() == "none" || notify_str.to_lowercase() == "clear" {
                watch.notify_target = None;
                changes.push("notify -> cleared (will use global default)".to_string());
            } else {
                // Parse the notify string (format: "type:value" or "type:value:value2")
                let target = super::parse_notify_string(&notify_str)?;
                let description = super::describe_notify_target(&target);
                watch.notify_target = Some(target);
                changes.push(format!("notify -> {}", description));
            }
        }

        if let Some(profile) = new_use_profile {
            watch.use_profile = profile;
            changes.push(format!("use_profile -> {}", profile));
        }

        db.update_watch(&watch)?;

        println!("\nUpdated watch '{}':", watch.name);
        for change in changes {
            println!("  {}", change);
        }
    } else if atty::is(atty::Stream::Stdin) {
        // Interactive editing
        println!("\nEditing watch: {}\n", watch.name);
        println!("  Current settings:");
        println!("    Name:     {}", watch.name);
        println!("    URL:      {}", watch.url);
        println!("    Interval: {}", format_interval(watch.interval_secs));
        println!("    Status:   {}", if watch.enabled { "active" } else { "paused" });
        println!("    Engine:   {:?}", watch.engine);
        if let Some(ref config) = watch.agent_config {
            println!("    AI Agent: {}", if config.enabled { "enabled" } else { "disabled" });
            if let Some(ref inst) = config.instructions {
                println!("    Instructions: {}", inst);
            }
        } else {
            println!("    AI Agent: not configured");
        }
        println!();

        loop {
            let options = vec![
                "Change name",
                "Change interval",
                "Toggle pause/resume",
                "Toggle AI agent",
                "Set agent instructions",
                "Done",
            ];

            let choice = Select::new("What would you like to change?", options)
                .prompt()
                .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;

            match choice {
                "Change name" => {
                    let new = Text::new("New name:")
                        .with_default(&watch.name)
                        .prompt()
                        .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;
                    watch.name = new;
                    println!("  Name updated.");
                }
                "Change interval" => {
                    let current = format_interval(watch.interval_secs);
                    let new = Text::new("New interval (e.g., 5m, 1h, 30s):")
                        .with_default(&current)
                        .prompt()
                        .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;

                    if let Ok(secs) = parse_interval_str(&new) {
                        watch.interval_secs = secs;
                        println!("  Interval updated to {}.", format_interval(secs));
                    } else {
                        println!("  Invalid interval format. Use 30s, 5m, 1h, etc.");
                    }
                }
                "Toggle pause/resume" => {
                    watch.enabled = !watch.enabled;
                    println!("  Watch {}.", if watch.enabled { "resumed" } else { "paused" });
                }
                "Toggle AI agent" => {
                    if let Some(ref mut config) = watch.agent_config {
                        config.enabled = !config.enabled;
                        println!("  AI agent {}.", if config.enabled { "enabled" } else { "disabled" });
                    } else {
                        watch.agent_config = Some(AgentConfig {
                            enabled: true,
                            prompt_template: None,
                            instructions: None,
                        });
                        println!("  AI agent enabled.");
                    }
                }
                "Set agent instructions" => {
                    let current = watch.agent_config.as_ref()
                        .and_then(|c| c.instructions.as_deref())
                        .unwrap_or("");
                    let new = Text::new("Agent instructions:")
                        .with_default(current)
                        .with_help_message("What should the AI focus on when analyzing changes?")
                        .prompt()
                        .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;

                    if watch.agent_config.is_none() {
                        watch.agent_config = Some(AgentConfig {
                            enabled: true,
                            prompt_template: None,
                            instructions: if new.is_empty() { None } else { Some(new) },
                        });
                    } else if let Some(ref mut config) = watch.agent_config {
                        config.instructions = if new.is_empty() { None } else { Some(new) };
                    }
                    println!("  Instructions updated.");
                }
                "Done" => break,
                _ => {}
            }
        }

        db.update_watch(&watch)?;
        println!("\nWatch '{}' updated.", watch.name);
    } else {
        println!("No flags provided and not running interactively.");
        println!("Use flags like --interval 300 or run in a terminal for interactive mode.");
    }

    Ok(())
}

/// Pause a watch
pub fn cmd_pause(id_or_name: &str) -> Result<()> {
    let db = Database::open()?;
    let mut watch = db.get_watch(id_or_name)?
        .ok_or_else(|| kto::KtoError::WatchNotFound(id_or_name.to_string()))?;

    watch.enabled = false;
    db.update_watch(&watch)?;

    println!("Paused watch: {}", watch.name);
    Ok(())
}

/// Resume a paused watch
pub fn cmd_resume(id_or_name: &str) -> Result<()> {
    let db = Database::open()?;
    let mut watch = db.get_watch(id_or_name)?
        .ok_or_else(|| kto::KtoError::WatchNotFound(id_or_name.to_string()))?;

    watch.enabled = true;
    db.update_watch(&watch)?;

    println!("Resumed watch: {}", watch.name);
    Ok(())
}

/// Delete a watch
pub fn cmd_delete(id_or_name: &str, skip_confirm: 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 !skip_confirm {
        let confirm = Confirm::new(&format!("Delete watch '{}'?", watch.name))
            .with_default(false)
            .prompt()
            .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;

        if !confirm {
            println!("Cancelled.");
            return Ok(());
        }
    }

    db.delete_watch(&watch.id)?;
    println!("Deleted watch: {}", watch.name);
    Ok(())
}

// ============================================================================
// Enhanced Wizard Helper Functions
// ============================================================================

/// Perform parallel HTTP and Playwright fetches for dual content analysis
fn dual_fetch(url: &str) -> Result<(Option<PageContent>, Option<PageContent>)> {
    let url_owned = url.to_string();

    // Start HTTP fetch in a thread
    let url_http = url_owned.clone();
    let http_handle = thread::spawn(move || {
        fetch::fetch(&url_http, Engine::Http, &std::collections::HashMap::new())
    });

    // Start Playwright fetch if available
    let playwright_available = check_playwright().is_ready();
    let js_handle = if playwright_available {
        let url_js = url_owned.clone();
        Some(thread::spawn(move || {
            fetch::fetch(&url_js, Engine::Playwright, &std::collections::HashMap::new())
        }))
    } else {
        None
    };

    // Wait for HTTP result
    let http_result = http_handle
        .join()
        .map_err(|_| kto::KtoError::ConfigError("HTTP fetch thread panicked".into()))?;
    let http_content = http_result.ok();

    // Wait for Playwright result if started
    let js_content = if let Some(handle) = js_handle {
        handle
            .join()
            .map_err(|_| kto::KtoError::ConfigError("Playwright fetch thread panicked".into()))?
            .ok()
    } else {
        None
    };

    // Report what we got
    let http_status = if http_content.is_some() { "" } else { "" };
    let js_status = if js_content.is_some() {
        ""
    } else if playwright_available {
        ""
    } else {
        ""
    };
    println!("  Fetched: HTTP {} | JS {}", http_status, js_status);

    Ok((http_content, js_content))
}

/// Display enhanced confirmation UI with variants and current status
fn display_enhanced_confirmation(
    url: &str,
    suggestion: &EnhancedSetupSuggestion,
    default_extraction: &Extraction,
    default_engine: Engine,
    name_override: &Option<String>,
    _default_interval: u64,
    yes: bool,
) -> Result<(String, String, u64, bool, Option<String>, Extraction, Engine)> {
    // Check if we need to show low-confidence UI
    let low_confidence = suggestion.confidence < CONFIDENCE_THRESHOLD;

    if !yes {
        // Display analysis results
        println!();
        println!("  {}", "Analysis Results".bold().underline());
        println!();

        // Current status
        if let Some(ref status) = suggestion.current_status {
            println!("  Status:  {}", status.cyan());
        }

        // Engine recommendation
        let engine_text = if suggestion.needs_js {
            let reason = suggestion.js_reason.as_ref().map(|r| format!(" ({})", r)).unwrap_or_default();
            format!("{}{}", "JavaScript required".yellow(), reason)
        } else {
            "HTTP".to_string()
        };
        println!("  Engine:  {}", engine_text);

        // Detected variants (limit to 5 for display)
        if !suggestion.variants.is_empty() {
            println!();
            let more = if suggestion.variants.len() > 5 {
                format!(" (+{} more)", suggestion.variants.len() - 5)
            } else {
                String::new()
            };
            println!("  Variants:{}", more);
            for (i, variant) in suggestion.variants.iter().take(5).enumerate() {
                let status_str = variant.status.as_deref().unwrap_or("?");
                let is_match = suggestion.intent_match.as_ref().map(|m| m.variant_index == i).unwrap_or(false);
                let marker = if is_match { " ← intent".yellow().to_string() } else { "".to_string() };
                println!("    {}. {} - {}{}", i + 1, variant.name, status_str, marker);
            }
        }

        // Recommended setup
        println!();
        println!("  Suggested:");
        println!("    Name:     {}", suggestion.name);
        println!("    Interval: {}", format_interval(suggestion.interval_secs));
        if let Some(ref instructions) = suggestion.agent_instructions {
            let display_instructions = truncate_str(instructions, 60);
            println!("    AI:       \"{}\"", display_instructions);
        }

        // Show uncertainty reasons if low confidence
        if low_confidence && !suggestion.uncertainty_reasons.is_empty() {
            println!();
            println!("  {} Low confidence ({:.0}%):", "".yellow(), suggestion.confidence * 100.0);
            for reason in &suggestion.uncertainty_reasons {
                println!("{}", reason);
            }
        }
        println!();
    }

    // Determine final URL (with variant if matched)
    let final_url = if let Some(ref intent_match) = suggestion.intent_match {
        if let Some(variant) = suggestion.variants.get(intent_match.variant_index) {
            if let Some(ref url_hint) = variant.url_hint {
                construct_variant_url(url, url_hint)
            } else {
                url.to_string()
            }
        } else {
            url.to_string()
        }
    } else {
        url.to_string()
    };

    // Show variant URL if different
    if final_url != url && !yes {
        println!("  Using variant URL: {}", final_url.cyan());
        println!();
    }

    // User confirmation or customization
    if yes {
        // Auto-accept with --yes
        let name = name_override.clone().unwrap_or_else(|| suggestion.name.clone());
        let engine = if suggestion.needs_js { Engine::Playwright } else { default_engine };
        let extraction = suggestion.selector_hint.as_ref()
            .map(|sel| Extraction::Selector { selector: sel.clone() })
            .unwrap_or_else(|| default_extraction.clone());

        return Ok((
            name,
            final_url,
            suggestion.interval_secs,
            suggestion.agent_enabled,
            suggestion.agent_instructions.clone(),
            extraction,
            engine,
        ));
    }

    // Offer choices: Create, Customize, Cancel
    let choices = if !suggestion.variants.is_empty() && suggestion.variants.len() > 1 {
        vec!["Create Watch", "Select Different Variant", "Customize", "Cancel"]
    } else {
        vec!["Create Watch", "Customize", "Cancel"]
    };

    let choice = Select::new("What would you like to do?", choices)
        .prompt()
        .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;

    match choice {
        "Create Watch" => {
            let name = name_override.clone().unwrap_or_else(|| suggestion.name.clone());
            let engine = if suggestion.needs_js { Engine::Playwright } else { default_engine };
            let extraction = suggestion.selector_hint.as_ref()
                .map(|sel| Extraction::Selector { selector: sel.clone() })
                .unwrap_or_else(|| default_extraction.clone());

            Ok((
                name,
                final_url,
                suggestion.interval_secs,
                suggestion.agent_enabled,
                suggestion.agent_instructions.clone(),
                extraction,
                engine,
            ))
        }
        "Select Different Variant" => {
            // Let user select which variant to monitor
            let variant_names: Vec<String> = suggestion.variants.iter()
                .enumerate()
                .map(|(i, v)| {
                    let status = v.status.as_deref().unwrap_or("unknown");
                    format!("{}. {} - {}", i + 1, v.name, status)
                })
                .collect();

            let selected = Select::new("Which variant do you want to monitor?", variant_names)
                .prompt()
                .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;

            // Parse the selection to get index
            let selected_idx = selected.split('.').next()
                .and_then(|s| s.trim().parse::<usize>().ok())
                .map(|n| n - 1)
                .unwrap_or(0);

            let selected_variant = &suggestion.variants[selected_idx];

            // Construct URL with variant
            let variant_url = if let Some(ref hint) = selected_variant.url_hint {
                construct_variant_url(url, hint)
            } else {
                url.to_string()
            };

            // Update name to include variant
            let name = name_override.clone().unwrap_or_else(|| {
                format!("{} {}", suggestion.name, selected_variant.name)
            });

            // Update instructions to be variant-specific
            let instructions = Some(format!(
                "Monitor {} variant. Alert when status changes from '{}'",
                selected_variant.name,
                selected_variant.status.as_deref().unwrap_or("current")
            ));

            let engine = if suggestion.needs_js { Engine::Playwright } else { default_engine };
            let extraction = suggestion.selector_hint.as_ref()
                .map(|sel| Extraction::Selector { selector: sel.clone() })
                .unwrap_or_else(|| default_extraction.clone());

            println!("  Selected variant: {}", selected_variant.name);
            if variant_url != url {
                println!("  Using URL: {}", variant_url.cyan());
            }

            Ok((
                name,
                variant_url,
                suggestion.interval_secs,
                true,
                instructions,
                extraction,
                engine,
            ))
        }
        "Customize" => {
            // Manual customization flow
            let name = Text::new("Name for this watch?")
                .with_default(&name_override.clone().unwrap_or_else(|| suggestion.name.clone()))
                .prompt()
                .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;

            let interval_str = Text::new("Check interval (e.g., 5m, 1h)?")
                .with_default(&format_interval(suggestion.interval_secs))
                .prompt()
                .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;

            let custom_interval = crate::utils::parse_interval_str(&interval_str)
                .unwrap_or(suggestion.interval_secs);

            let use_ai = Confirm::new("Enable AI analysis?")
                .with_default(suggestion.agent_enabled)
                .prompt()
                .unwrap_or(suggestion.agent_enabled);

            let instructions = if use_ai {
                let inst = Text::new("What should AI watch for?")
                    .with_default(suggestion.agent_instructions.as_deref().unwrap_or(""))
                    .prompt()
                    .map_err(|e| kto::KtoError::ConfigError(e.to_string()))?;
                if inst.is_empty() { None } else { Some(inst) }
            } else {
                None
            };

            let use_js = if suggestion.needs_js {
                Confirm::new("Use JavaScript rendering (recommended)?")
                    .with_default(true)
                    .prompt()
                    .unwrap_or(true)
            } else {
                Confirm::new("Use JavaScript rendering?")
                    .with_default(false)
                    .prompt()
                    .unwrap_or(false)
            };

            let engine = if use_js { Engine::Playwright } else { Engine::Http };
            let extraction = suggestion.selector_hint.as_ref()
                .map(|sel| Extraction::Selector { selector: sel.clone() })
                .unwrap_or_else(|| default_extraction.clone());

            Ok((
                name,
                final_url,
                custom_interval,
                use_ai,
                instructions,
                extraction,
                engine,
            ))
        }
        "Cancel" | _ => {
            Err(kto::KtoError::ConfigError("Watch creation cancelled".into()))
        }
    }
}

/// Construct a URL with variant parameters
fn construct_variant_url(base_url: &str, url_hint: &str) -> String {
    // Parse the base URL
    if let Ok(mut parsed) = url::Url::parse(base_url) {
        // Check if url_hint is a full query param (contains =)
        if url_hint.contains('=') {
            // Split the hint into key=value pairs
            for param in url_hint.split('&') {
                if let Some((key, value)) = param.split_once('=') {
                    // Remove existing param with same key, add new one
                    let pairs: Vec<(String, String)> = parsed.query_pairs()
                        .filter(|(k, _)| k != key)
                        .map(|(k, v)| (k.to_string(), v.to_string()))
                        .collect();

                    parsed.set_query(None);
                    for (k, v) in pairs {
                        parsed.query_pairs_mut().append_pair(&k, &v);
                    }
                    parsed.query_pairs_mut().append_pair(key, value);
                }
            }
        } else {
            // Just append as-is (might be a path segment or raw param)
            let query = parsed.query().map(|q| format!("{}&{}", q, url_hint))
                .unwrap_or_else(|| url_hint.to_string());
            parsed.set_query(Some(&query));
        }
        parsed.to_string()
    } else {
        // Fallback: just append
        if base_url.contains('?') {
            format!("{}&{}", base_url, url_hint)
        } else {
            format!("{}?{}", base_url, url_hint)
        }
    }
}