soma-core 2.0.0

World's first production-ready self-aware development system with meta-cognitive capabilities and cognitive reasoning engine for intelligent development platforms
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
// Issue #27: Time Travel CLI Interface
// Interactive interface for navigating edit history with timeline branching and merging

use crate::edit_control::edit_history_time_travel::*;
use crate::edit_control::ModifiableEdit;
use crate::classification::ClassifiedEdit;
use crossterm::{
    cursor, execute, terminal,
    event::{self, Event, KeyCode},
    terminal::{ClearType},
    style::{Color, Print, ResetColor, SetForegroundColor},
};
use std::io::{self, Write};
use std::time::SystemTime;

/// Interactive Time Travel CLI Interface
/// Provides comprehensive timeline navigation and management
pub struct TimeTravelCLI {
    /// Time travel system
    pub time_system: EditHistoryTimeTravelSystem,
    /// Current interface state
    pub interface_state: TimeTravelInterfaceState,
    /// Current view mode
    pub view_mode: TimeTravelViewMode,
    /// Selected timeline
    pub selected_timeline: Option<String>,
    /// Selected event position
    pub selected_position: Option<usize>,
    /// Status message for user feedback
    pub status_message: String,
    /// Show help panel
    pub show_help: bool,
    /// Navigation history for undo operations
    pub navigation_history: Vec<NavigationState>,
}

/// Interface states for time travel CLI
#[derive(Debug, Clone, PartialEq)]
pub enum TimeTravelInterfaceState {
    /// Timeline overview with all timelines
    TimelineOverview,
    /// Detailed view of specific timeline
    TimelineDetails,
    /// Event navigation within timeline
    EventNavigation,
    /// Timeline branching interface
    BranchingInterface,
    /// Timeline merging interface
    MergingInterface,
    /// History search interface
    SearchInterface,
    /// Checkpoint management
    CheckpointManagement,
    /// Configuration panel
    Configuration,
}

/// View modes for time travel interface
#[derive(Debug, Clone, PartialEq)]
pub enum TimeTravelViewMode {
    /// List view of timelines/events
    List,
    /// Timeline graph visualization
    Graph,
    /// Chronological view
    Chronological,
    /// File-centric view
    FileCentric,
    /// Split view (timelines + events)
    Split,
}

/// Navigation state for undo operations
#[derive(Debug, Clone)]
pub struct NavigationState {
    /// Timeline at this navigation point
    pub timeline_id: String,
    /// Event position at this navigation point
    pub event_position: usize,
    /// Timestamp of navigation
    pub timestamp: SystemTime,
}

impl TimeTravelCLI {
    /// Create new time travel CLI interface
    pub fn new() -> Result<Self, String> {
        let time_system = EditHistoryTimeTravelSystem::new(None);
        
        Ok(Self {
            time_system,
            interface_state: TimeTravelInterfaceState::TimelineOverview,
            view_mode: TimeTravelViewMode::List,
            selected_timeline: None,
            selected_position: None,
            status_message: "Time Travel initialized".to_string(),
            show_help: false,
            navigation_history: Vec::new(),
        })
    }

    /// Create with existing time travel system
    pub fn with_time_system(time_system: EditHistoryTimeTravelSystem) -> Self {
        Self {
            time_system,
            interface_state: TimeTravelInterfaceState::TimelineOverview,
            view_mode: TimeTravelViewMode::List,
            selected_timeline: None,
            selected_position: None,
            status_message: "Time Travel loaded".to_string(),
            show_help: false,
            navigation_history: Vec::new(),
        }
    }

    /// Start interactive time travel session
    pub fn start_interactive_session(&mut self) -> Result<(), String> {
        // Initialize terminal
        terminal::enable_raw_mode().map_err(|e| format!("Failed to enable raw mode: {}", e))?;
        execute!(io::stdout(), terminal::Clear(ClearType::All))
            .map_err(|e| format!("Failed to clear terminal: {}", e))?;

        let mut running = true;
        while running {
            self.render_interface()?;
            
            if let Ok(event) = event::read() {
                match self.handle_event(event) {
                    Ok(should_continue) => {
                        if !should_continue {
                            running = false;
                        }
                    }
                    Err(e) => {
                        self.status_message = format!("Error: {}", e);
                    }
                }
            }
        }

        // Cleanup terminal
        terminal::disable_raw_mode().map_err(|e| format!("Failed to disable raw mode: {}", e))?;
        execute!(io::stdout(), terminal::Clear(ClearType::All))
            .map_err(|e| format!("Failed to clear terminal: {}", e))?;

        Ok(())
    }

    /// Render the current interface
    fn render_interface(&self) -> Result<(), String> {
        execute!(io::stdout(), cursor::MoveTo(0, 0))
            .map_err(|e| format!("Failed to move cursor: {}", e))?;

        self.render_header()?;
        
        match self.interface_state {
            TimeTravelInterfaceState::TimelineOverview => self.render_timeline_overview()?,
            TimeTravelInterfaceState::TimelineDetails => self.render_timeline_details()?,
            TimeTravelInterfaceState::EventNavigation => self.render_event_navigation()?,
            TimeTravelInterfaceState::BranchingInterface => self.render_branching_interface()?,
            TimeTravelInterfaceState::MergingInterface => self.render_merging_interface()?,
            TimeTravelInterfaceState::SearchInterface => self.render_search_interface()?,
            TimeTravelInterfaceState::CheckpointManagement => self.render_checkpoint_management()?,
            TimeTravelInterfaceState::Configuration => self.render_configuration()?,
        }

        self.render_controls()?;
        self.render_status()?;

        if self.show_help {
            self.render_help_panel()?;
        }

        io::stdout().flush().map_err(|e| format!("Failed to flush stdout: {}", e))?;
        Ok(())
    }

    /// Render interface header
    fn render_header(&self) -> Result<(), String> {
        execute!(io::stdout(),
            SetForegroundColor(Color::Cyan),
            Print("🕰️  SOMA-CORE Time Travel System\n"),
            Print("═══════════════════════════════════════════════════════════\n"),
            ResetColor,
            Print(&format!("State: {:?} | View: {:?} | Timelines: {}\n",
                          self.interface_state, self.view_mode, self.time_system.timelines.len())),
            Print("\n")
        ).map_err(|e| format!("Failed to render header: {}", e))?;

        Ok(())
    }

    /// Render timeline overview
    fn render_timeline_overview(&self) -> Result<(), String> {
        execute!(io::stdout(),
            SetForegroundColor(Color::Green),
            Print("📋 Timeline Overview\n"),
            Print("═══════════════════\n"),
            ResetColor
        ).map_err(|e| format!("Failed to render overview header: {}", e))?;

        if self.time_system.timelines.is_empty() {
            execute!(io::stdout(),
                Print("No timelines found. Press 'N' to create a new timeline.\n")
            ).map_err(|e| format!("Failed to render empty message: {}", e))?;
        } else {
            for (timeline_id, timeline) in &self.time_system.timelines {
                let marker = if self.selected_timeline.as_ref() == Some(timeline_id) { ">" } else { " " };
                let status_color = match timeline.state {
                    TimelineState::Active => Color::Green,
                    TimelineState::Inactive => Color::Yellow,
                    TimelineState::Archived => Color::Blue,
                    TimelineState::Merged => Color::Magenta,
                    TimelineState::Deleted => Color::Red,
                };

                execute!(io::stdout(),
                    Print(&format!("{} ", marker)),
                    SetForegroundColor(status_color),
                    Print(&format!("{:<20}", timeline.name)),
                    ResetColor,
                    Print(&format!(" | Events: {:3} | State: {:?}\n",
                                 timeline.events.len(), timeline.state))
                ).map_err(|e| format!("Failed to render timeline: {}", e))?;

                // Show branch relationships
                if let Some(parent) = &timeline.parent_timeline {
                    execute!(io::stdout(),
                        Print(&format!("    ↳ Branched from: {}\n", 
                               self.time_system.timelines.get(parent)
                                   .map(|t| t.name.as_str())
                                   .unwrap_or("Unknown")))
                    ).map_err(|e| format!("Failed to render branch info: {}", e))?;
                }

                if !timeline.child_timelines.is_empty() {
                    execute!(io::stdout(),
                        Print(&format!("    ↳ Children: {}\n", 
                               timeline.child_timelines.len()))
                    ).map_err(|e| format!("Failed to render children info: {}", e))?;
                }
            }
        }

        Ok(())
    }

    /// Render timeline details
    fn render_timeline_details(&self) -> Result<(), String> {
        let timeline_id = self.selected_timeline.as_ref()
            .ok_or("No timeline selected")?;

        let timeline = self.time_system.timelines.get(timeline_id)
            .ok_or("Selected timeline not found")?;

        execute!(io::stdout(),
            SetForegroundColor(Color::Green),
            Print(&format!("📊 Timeline Details: {}\n", timeline.name)),
            Print("═══════════════════════════════\n"),
            ResetColor,
            Print(&format!("Description: {}\n", timeline.description)),
            Print(&format!("State: {:?}\n", timeline.state)),
            Print(&format!("Events: {}\n", timeline.events.len())),
            Print(&format!("Current Position: {}\n", timeline.current_position)),
            Print(&format!("Created: {:?}\n", timeline.metadata.created_at)),
            Print(&format!("Modified: {:?}\n", timeline.metadata.modified_at)),
            Print("\n")
        ).map_err(|e| format!("Failed to render timeline details: {}", e))?;

        // Show recent events
        execute!(io::stdout(),
            SetForegroundColor(Color::Cyan),
            Print("📝 Recent Events:\n"),
            ResetColor
        ).map_err(|e| format!("Failed to render events header: {}", e))?;

        let events_to_show = timeline.events.iter()
            .enumerate()
            .rev()
            .take(10)
            .collect::<Vec<_>>();

        for (pos, event) in events_to_show.iter().rev() {
            let marker = if *pos == timeline.current_position { ">" } else { " " };
            let event_type_color = match event.event_type {
                EditEventType::Creation => Color::Green,
                EditEventType::Modification => Color::Yellow,
                EditEventType::Checkpoint => Color::Blue,
                EditEventType::Rollback => Color::Red,
                _ => Color::White,
            };

            execute!(io::stdout(),
                Print(&format!("{} {:3}: ", marker, pos)),
                SetForegroundColor(event_type_color),
                Print(&format!("{:12}", format!("{:?}", event.event_type))),
                ResetColor,
                Print(&format!(" | {}\n", event.metadata.description))
            ).map_err(|e| format!("Failed to render event: {}", e))?;
        }

        Ok(())
    }

    /// Render event navigation
    fn render_event_navigation(&self) -> Result<(), String> {
        let timeline_id = self.selected_timeline.as_ref()
            .ok_or("No timeline selected")?;

        let timeline = self.time_system.timelines.get(timeline_id)
            .ok_or("Selected timeline not found")?;

        execute!(io::stdout(),
            SetForegroundColor(Color::Green),
            Print("🧭 Event Navigation\n"),
            Print("═══════════════════\n"),
            ResetColor,
            Print(&format!("Timeline: {} | Position: {} / {}\n",
                          timeline.name, timeline.current_position + 1, timeline.events.len())),
            Print("\n")
        ).map_err(|e| format!("Failed to render navigation header: {}", e))?;

        if let Some(current_event) = timeline.events.get(timeline.current_position) {
            execute!(io::stdout(),
                SetForegroundColor(Color::Cyan),
                Print("📍 Current Event:\n"),
                ResetColor,
                Print(&format!("ID: {}\n", current_event.id)),
                Print(&format!("Type: {:?}\n", current_event.event_type)),
                Print(&format!("Description: {}\n", current_event.metadata.description)),
                Print(&format!("File: {}\n", current_event.edit.base_edit.file)),
                Print(&format!("Confidence: {:.1}%\n", current_event.edit.get_effective_confidence() * 100.0)),
                Print(&format!("Timestamp: {:?}\n", current_event.timestamp)),
                Print("\n")
            ).map_err(|e| format!("Failed to render current event: {}", e))?;

            // Show code preview
            execute!(io::stdout(),
                SetForegroundColor(Color::Yellow),
                Print("💻 Code Preview:\n"),
                ResetColor
            ).map_err(|e| format!("Failed to render code header: {}", e))?;

            let final_code = current_event.edit.compute_final_code();
            let code_lines: Vec<&str> = final_code.lines().collect();
            for (i, line) in code_lines.iter().take(10).enumerate() {
                execute!(io::stdout(),
                    Print(&format!("{:3}: {}\n", i + 1, line))
                ).map_err(|e| format!("Failed to render code line: {}", e))?;
            }

            if code_lines.len() > 10 {
                execute!(io::stdout(),
                    Print(&format!("... and {} more lines\n", code_lines.len() - 10))
                ).map_err(|e| format!("Failed to render code continuation: {}", e))?;
            }
        }

        Ok(())
    }

    /// Render branching interface
    fn render_branching_interface(&self) -> Result<(), String> {
        execute!(io::stdout(),
            SetForegroundColor(Color::Green),
            Print("🌿 Timeline Branching\n"),
            Print("════════════════════\n"),
            ResetColor,
            Print("Create a new timeline branch from current position.\n"),
            Print("\n"),
            Print("Current Timeline: "),
            SetForegroundColor(Color::Cyan),
            Print(&format!("{}\n", 
                  self.selected_timeline.as_ref()
                      .and_then(|id| self.time_system.timelines.get(id))
                      .map(|t| t.name.as_str())
                      .unwrap_or("None"))),
            ResetColor,
            Print("\n"),
            Print("Enter branch name (press Enter to confirm, Escape to cancel):\n"),
            Print("> ")
        ).map_err(|e| format!("Failed to render branching interface: {}", e))?;

        Ok(())
    }

    /// Render merging interface
    fn render_merging_interface(&self) -> Result<(), String> {
        execute!(io::stdout(),
            SetForegroundColor(Color::Green),
            Print("🔗 Timeline Merging\n"),
            Print("═══════════════════\n"),
            ResetColor,
            Print("Merge timelines together.\n"),
            Print("\n"),
            Print("Available merge strategies:\n"),
            Print("• fast-forward: Simple linear merge\n"),
            Print("• three-way: Standard merge with conflict resolution\n"),
            Print("• squash: Compress source timeline into single event\n"),
            Print("\n")
        ).map_err(|e| format!("Failed to render merging interface: {}", e))?;

        // Show available timelines for merging
        for (timeline_id, timeline) in &self.time_system.timelines {
            if timeline.state == TimelineState::Active && 
               Some(timeline_id) != self.selected_timeline.as_ref() {
                execute!(io::stdout(),
                    Print(&format!("  {} ({})\n", timeline.name, timeline.events.len()))
                ).map_err(|e| format!("Failed to render merge candidate: {}", e))?;
            }
        }

        Ok(())
    }

    /// Render search interface
    fn render_search_interface(&self) -> Result<(), String> {
        execute!(io::stdout(),
            SetForegroundColor(Color::Green),
            Print("🔍 History Search\n"),
            Print("════════════════\n"),
            ResetColor,
            Print("Search through edit history.\n"),
            Print("\n"),
            Print("Search options:\n"),
            Print("• Text search in descriptions\n"),
            Print("• Filter by file patterns\n"),
            Print("• Filter by time range\n"),
            Print("• Filter by confidence level\n"),
            Print("• Filter by event types\n"),
            Print("\n"),
            Print("Enter search query: ")
        ).map_err(|e| format!("Failed to render search interface: {}", e))?;

        Ok(())
    }

    /// Render checkpoint management
    fn render_checkpoint_management(&self) -> Result<(), String> {
        execute!(io::stdout(),
            SetForegroundColor(Color::Green),
            Print("📍 Checkpoint Management\n"),
            Print("═══════════════════════\n"),
            ResetColor,
            Print("Manage timeline checkpoints.\n"),
            Print("\n")
        ).map_err(|e| format!("Failed to render checkpoint header: {}", e))?;

        // Show existing checkpoints across timelines
        let mut checkpoint_count = 0;
        for timeline in self.time_system.timelines.values() {
            for event in &timeline.events {
                if event.event_type == EditEventType::Checkpoint {
                    checkpoint_count += 1;
                    execute!(io::stdout(),
                        Print(&format!("📍 {} | {} | {}\n",
                                     event.metadata.description,
                                     timeline.name,
                                     event.timestamp.duration_since(SystemTime::UNIX_EPOCH)
                                         .unwrap_or_default().as_secs()))
                    ).map_err(|e| format!("Failed to render checkpoint: {}", e))?;
                }
            }
        }

        if checkpoint_count == 0 {
            execute!(io::stdout(),
                Print("No checkpoints found. Press 'C' to create a checkpoint.\n")
            ).map_err(|e| format!("Failed to render no checkpoints message: {}", e))?;
        }

        Ok(())
    }

    /// Render configuration panel
    fn render_configuration(&self) -> Result<(), String> {
        execute!(io::stdout(),
            SetForegroundColor(Color::Green),
            Print("⚙️  Configuration\n"),
            Print("════════════════\n"),
            ResetColor,
            Print(&format!("Max Memory Events: {}\n", self.time_system.config.max_memory_events)),
            Print(&format!("Auto Checkpoint Frequency: {}\n", self.time_system.config.auto_checkpoint_frequency)),
            Print(&format!("Max Timeline Depth: {}\n", self.time_system.config.max_timeline_depth)),
            Print(&format!("Git Integration: {}\n", self.time_system.config.git_integration_enabled)),
            Print(&format!("Auto Merge: {}\n", self.time_system.config.auto_merge_timelines)),
            Print("\n"),
            Print("Performance Settings:\n"),
            Print(&format!("  Lazy Loading: {}\n", self.time_system.config.performance_settings.lazy_loading)),
            Print(&format!("  Event Caching: {}\n", self.time_system.config.performance_settings.event_caching)),
            Print(&format!("  Background Indexing: {}\n", self.time_system.config.performance_settings.background_indexing)),
            Print(&format!("  Max Memory: {} MB\n", self.time_system.config.performance_settings.max_memory_mb)),
        ).map_err(|e| format!("Failed to render configuration: {}", e))?;

        Ok(())
    }

    /// Render control help
    fn render_controls(&self) -> Result<(), String> {
        execute!(io::stdout(),
            Print("\n"),
            SetForegroundColor(Color::Yellow),
            Print("🎮 Controls: "),
            ResetColor
        ).map_err(|e| format!("Failed to render controls header: {}", e))?;

        match self.interface_state {
            TimeTravelInterfaceState::TimelineOverview => {
                execute!(io::stdout(),
                    Print("↑↓ Select | Enter View | N New | D Delete | B Branch | M Merge | S Search | Q Quit")
                ).map_err(|e| format!("Failed to render overview controls: {}", e))?;
            }
            TimeTravelInterfaceState::EventNavigation => {
                execute!(io::stdout(),
                    Print("←→ Navigate | J Jump | C Checkpoint | R Rollback | Esc Back | ? Help")
                ).map_err(|e| format!("Failed to render navigation controls: {}", e))?;
            }
            _ => {
                execute!(io::stdout(),
                    Print("Esc Back | ? Help | Q Quit")
                ).map_err(|e| format!("Failed to render default controls: {}", e))?;
            }
        }

        Ok(())
    }

    /// Render status bar
    fn render_status(&self) -> Result<(), String> {
        execute!(io::stdout(),
            Print("\n"),
            SetForegroundColor(Color::Blue),
            Print("📊 Status: "),
            ResetColor,
            Print(&self.status_message),
            Print("\n")
        ).map_err(|e| format!("Failed to render status: {}", e))?;

        Ok(())
    }

    /// Render help panel
    fn render_help_panel(&self) -> Result<(), String> {
        execute!(io::stdout(),
            Print("\n"),
            SetForegroundColor(Color::Magenta),
            Print("❓ Help Panel\n"),
            Print("════════════\n"),
            ResetColor,
            Print("Time Travel System Commands:\n"),
            Print("• Timeline Management: Create, delete, branch, merge timelines\n"),
            Print("• Event Navigation: Move through edit history timeline\n"),
            Print("• Search & Filter: Find specific edits or patterns\n"),
            Print("• Checkpoints: Create and restore stable points\n"),
            Print("• Git Integration: Sync with Git commits and branches\n"),
            Print("\n"),
            Print("Navigation:\n"),
            Print("• Arrow keys: Navigate through timelines and events\n"),
            Print("• Enter: Select or confirm action\n"),
            Print("• Escape: Go back or cancel operation\n"),
            Print("• Q: Quit time travel system\n"),
            Print("• ?: Toggle this help panel\n"),
        ).map_err(|e| format!("Failed to render help panel: {}", e))?;

        Ok(())
    }

    /// Handle keyboard events
    fn handle_event(&mut self, event: Event) -> Result<bool, String> {
        match event {
            Event::Key(key_event) => {
                match key_event.code {
                    KeyCode::Char('q') | KeyCode::Char('Q') => {
                        return Ok(false); // Quit
                    }
                    KeyCode::Char('?') => {
                        self.show_help = !self.show_help;
                    }
                    KeyCode::Esc => {
                        self.handle_escape()?;
                    }
                    _ => {
                        self.handle_state_specific_key(key_event.code)?;
                    }
                }
            }
            _ => {}
        }
        Ok(true)
    }

    /// Handle escape key based on current state
    fn handle_escape(&mut self) -> Result<(), String> {
        match self.interface_state {
            TimeTravelInterfaceState::TimelineOverview => {
                // Do nothing - already at top level
            }
            _ => {
                self.interface_state = TimeTravelInterfaceState::TimelineOverview;
                self.status_message = "Returned to timeline overview".to_string();
            }
        }
        Ok(())
    }

    /// Handle keys specific to current interface state
    fn handle_state_specific_key(&mut self, key: KeyCode) -> Result<(), String> {
        match self.interface_state {
            TimeTravelInterfaceState::TimelineOverview => {
                self.handle_overview_keys(key)?;
            }
            TimeTravelInterfaceState::TimelineDetails => {
                self.handle_details_keys(key)?;
            }
            TimeTravelInterfaceState::EventNavigation => {
                self.handle_navigation_keys(key)?;
            }
            _ => {
                // Default handling for other states
            }
        }
        Ok(())
    }

    /// Handle keys in timeline overview
    fn handle_overview_keys(&mut self, key: KeyCode) -> Result<(), String> {
        match key {
            KeyCode::Char('n') | KeyCode::Char('N') => {
                self.create_new_timeline()?;
            }
            KeyCode::Enter => {
                if self.selected_timeline.is_some() {
                    self.interface_state = TimeTravelInterfaceState::TimelineDetails;
                    self.status_message = "Viewing timeline details".to_string();
                }
            }
            KeyCode::Char('b') | KeyCode::Char('B') => {
                if self.selected_timeline.is_some() {
                    self.interface_state = TimeTravelInterfaceState::BranchingInterface;
                    self.status_message = "Timeline branching mode".to_string();
                }
            }
            KeyCode::Char('s') | KeyCode::Char('S') => {
                self.interface_state = TimeTravelInterfaceState::SearchInterface;
                self.status_message = "History search mode".to_string();
            }
            KeyCode::Up => {
                self.select_previous_timeline();
            }
            KeyCode::Down => {
                self.select_next_timeline();
            }
            _ => {}
        }
        Ok(())
    }

    /// Handle keys in timeline details
    fn handle_details_keys(&mut self, key: KeyCode) -> Result<(), String> {
        match key {
            KeyCode::Char('e') | KeyCode::Char('E') => {
                self.interface_state = TimeTravelInterfaceState::EventNavigation;
                self.status_message = "Event navigation mode".to_string();
            }
            KeyCode::Char('c') | KeyCode::Char('C') => {
                self.interface_state = TimeTravelInterfaceState::CheckpointManagement;
                self.status_message = "Checkpoint management mode".to_string();
            }
            _ => {}
        }
        Ok(())
    }

    /// Handle keys in event navigation
    fn handle_navigation_keys(&mut self, key: KeyCode) -> Result<(), String> {
        match key {
            KeyCode::Left => {
                self.navigate_to_previous_event()?;
            }
            KeyCode::Right => {
                self.navigate_to_next_event()?;
            }
            KeyCode::Char('c') | KeyCode::Char('C') => {
                self.create_checkpoint_at_current_position()?;
            }
            KeyCode::Char('j') | KeyCode::Char('J') => {
                self.jump_to_event()?;
            }
            _ => {}
        }
        Ok(())
    }

    // Helper methods for UI operations
    fn create_new_timeline(&mut self) -> Result<(), String> {
        let timeline_id = self.time_system.create_timeline("new_timeline", "New timeline")?;
        self.selected_timeline = Some(timeline_id);
        self.status_message = "Created new timeline".to_string();
        Ok(())
    }

    fn select_previous_timeline(&mut self) {
        let timeline_ids: Vec<_> = self.time_system.timelines.keys().collect();
        if !timeline_ids.is_empty() {
            if let Some(current) = &self.selected_timeline {
                if let Some(pos) = timeline_ids.iter().position(|id| *id == current) {
                    let new_pos = if pos > 0 { pos - 1 } else { timeline_ids.len() - 1 };
                    self.selected_timeline = Some(timeline_ids[new_pos].clone());
                }
            } else {
                self.selected_timeline = Some(timeline_ids[0].clone());
            }
        }
    }

    fn select_next_timeline(&mut self) {
        let timeline_ids: Vec<_> = self.time_system.timelines.keys().collect();
        if !timeline_ids.is_empty() {
            if let Some(current) = &self.selected_timeline {
                if let Some(pos) = timeline_ids.iter().position(|id| *id == current) {
                    let new_pos = (pos + 1) % timeline_ids.len();
                    self.selected_timeline = Some(timeline_ids[new_pos].clone());
                }
            } else {
                self.selected_timeline = Some(timeline_ids[0].clone());
            }
        }
    }

    fn navigate_to_previous_event(&mut self) -> Result<(), String> {
        if let Some(timeline_id) = &self.selected_timeline {
            if let Some(timeline) = self.time_system.timelines.get_mut(timeline_id) {
                if timeline.current_position > 0 {
                    timeline.current_position -= 1;
                    self.status_message = format!("Moved to event {}", timeline.current_position);
                } else {
                    self.status_message = "Already at first event".to_string();
                }
            }
        }
        Ok(())
    }

    fn navigate_to_next_event(&mut self) -> Result<(), String> {
        if let Some(timeline_id) = &self.selected_timeline {
            if let Some(timeline) = self.time_system.timelines.get_mut(timeline_id) {
                if timeline.current_position < timeline.events.len().saturating_sub(1) {
                    timeline.current_position += 1;
                    self.status_message = format!("Moved to event {}", timeline.current_position);
                } else {
                    self.status_message = "Already at last event".to_string();
                }
            }
        }
        Ok(())
    }

    fn create_checkpoint_at_current_position(&mut self) -> Result<(), String> {
        let checkpoint_id = self.time_system.create_checkpoint_at_current_position("manual_checkpoint")?;
        self.status_message = format!("Created checkpoint: {}", checkpoint_id);
        Ok(())
    }

    fn jump_to_event(&mut self) -> Result<(), String> {
        // This would show an input dialog for jumping to specific event
        self.status_message = "Jump to event (not implemented in demo)".to_string();
        Ok(())
    }

    /// Add edits to time travel system
    pub fn add_edits_to_timeline(&mut self, edits: Vec<ModifiableEdit>) -> Result<(), String> {
        if self.time_system.timelines.is_empty() {
            self.time_system.create_timeline("main", "Main timeline")?;
        }

        for edit in edits {
            self.time_system.add_edit_event(edit, "Added via CLI")?;
        }

        self.status_message = format!("Added {} edits to timeline", 
                                    self.time_system.timelines.values().next()
                                        .map(|t| t.events.len())
                                        .unwrap_or(0));
        Ok(())
    }
}

/// Interactive time travel workflow decision integration
pub fn interactive_time_travel_decision(
    edits: Vec<ModifiableEdit>,
    _classifications: Vec<ClassifiedEdit>
) -> Result<bool, String> {
    println!("🕰️  Time Travel System Available");
    println!("═══════════════════════════════════");
    println!("Would you like to use the advanced time travel system?");
    println!();
    println!("Time travel provides:");
    println!("• Complete edit history with timeline branching");
    println!("• Navigate through different edit paths");
    println!("• Create and restore checkpoints");
    println!("• Search through historical changes");
    println!("• Merge and compare different edit timelines");
    println!();
    print!("Use Time Travel system? [y/N]: ");
    io::stdout().flush().map_err(|e| format!("Failed to flush stdout: {}", e))?;

    let mut input = String::new();
    io::stdin().read_line(&mut input).map_err(|e| format!("Failed to read input: {}", e))?;

    if input.trim().to_lowercase().starts_with('y') {
        // Launch time travel CLI
        let mut time_travel_cli = TimeTravelCLI::new()?;
        time_travel_cli.add_edits_to_timeline(edits)?;
        
        println!("Launching Time Travel interface...");
        time_travel_cli.start_interactive_session()?;
        
        println!("Time travel session completed");
        Ok(true)
    } else {
        println!("Proceeding without time travel");
        Ok(false)
    }
}

// Tests
#[cfg(test)]
mod tests {
    use super::*;
    use crate::agents::gpt4_agent::ProposedEdit;

    fn create_test_edit() -> ModifiableEdit {
        let proposed = ProposedEdit {
            file: "test.rs".to_string(),
            line_range: (1, 5),
            new_code: "fn test() { println!(\"test\"); }".to_string(),
            reason: "Test function".to_string(),
            confidence: 0.8,
        };
        ModifiableEdit::from_proposed_edit(proposed)
    }

    #[test]
    fn test_time_travel_cli_creation() {
        let cli = TimeTravelCLI::new().unwrap();
        assert_eq!(cli.interface_state, TimeTravelInterfaceState::TimelineOverview);
        assert_eq!(cli.view_mode, TimeTravelViewMode::List);
        assert!(cli.selected_timeline.is_none());
    }

    #[test]
    fn test_add_edits_to_timeline() {
        let mut cli = TimeTravelCLI::new().unwrap();
        let edits = vec![create_test_edit()];
        
        cli.add_edits_to_timeline(edits).unwrap();
        
        assert!(!cli.time_system.timelines.is_empty());
        assert!(cli.time_system.current_timeline.is_some());
    }

    #[test]
    fn test_timeline_selection() {
        let mut cli = TimeTravelCLI::new().unwrap();
        cli.time_system.create_timeline("test1", "Test 1").unwrap();
        cli.time_system.create_timeline("test2", "Test 2").unwrap();
        
        cli.select_next_timeline();
        assert!(cli.selected_timeline.is_some());
        
        cli.select_previous_timeline();
        assert!(cli.selected_timeline.is_some());
    }
}