midas_fetcher 0.1.2

High-performance concurrent downloader for UK Met Office MIDAS Open weather data with intelligent caching and resumable downloads
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
//! Queue Simulation Binary
//!
//! This educational binary demonstrates the work-stealing queue system in action
//! with synthetic data, terminal UI visualization, and edge case simulation.
//!
//! Run with: `cargo run --bin simulate`

use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};

use chrono::Utc;
use crossterm::{
    event::{self, Event, KeyCode, KeyEventKind},
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
    ExecutableCommand,
};
use rand::prelude::*;
use ratatui::{
    backend::CrosstermBackend,
    layout::{Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    symbols,
    widgets::{
        Axis, Block, Borders, Chart, Dataset, GraphType, List, ListItem, Paragraph, Row, Table,
    },
    Frame, Terminal,
};
use tempfile::TempDir;
use tokio::sync::{mpsc, RwLock};
use tokio::time::{interval, sleep};
use tracing::{debug, info, warn};

use midas_fetcher::app::{
    hash::Md5Hash,
    models::FileInfo,
    queue::{WorkQueue, WorkQueueConfig},
};

/// Configuration for the simulation
#[derive(Debug, Clone)]
struct SimulationConfig {
    /// Number of workers to simulate
    pub worker_count: usize,
    /// Total number of files to generate
    pub file_count: usize,
    /// Percentage of duplicate files (0.0-1.0)
    pub duplicate_rate: f64,
    /// Percentage of files that will fail (0.0-1.0)
    pub failure_rate: f64,
    /// Minimum download time per file (milliseconds)
    pub min_download_time: u64,
    /// Maximum download time per file (milliseconds)
    pub max_download_time: u64,
    /// Simulation speed multiplier (higher = faster)
    pub speed_multiplier: f64,
    /// Whether to include edge cases
    pub include_edge_cases: bool,
}

impl Default for SimulationConfig {
    fn default() -> Self {
        Self {
            worker_count: 8,
            file_count: 2000,       // More files to process
            duplicate_rate: 0.15,   // 15% duplicates
            failure_rate: 0.08,     // Slightly higher failure rate for more events
            min_download_time: 100, // Slower downloads for better visibility
            max_download_time: 800,
            speed_multiplier: 3.0, // Slower speed multiplier for longer demo
            include_edge_cases: true,
        }
    }
}

/// Statistics for worker performance
#[derive(Debug, Clone, Default)]
struct WorkerStats {
    #[allow(dead_code)] // Used for future UI enhancements
    pub worker_id: u32,
    pub files_completed: u64,
    pub files_failed: u64,
    pub total_download_time: Duration,
    pub current_task: Option<String>,
    pub idle_time: Duration,
    pub last_activity: Option<Instant>,
}

impl WorkerStats {
    fn new(worker_id: u32) -> Self {
        Self {
            worker_id,
            last_activity: Some(Instant::now()),
            ..Default::default()
        }
    }

    fn average_download_time(&self) -> Duration {
        if self.files_completed > 0 {
            self.total_download_time / self.files_completed as u32
        } else {
            Duration::ZERO
        }
    }

    fn success_rate(&self) -> f64 {
        let total = self.files_completed + self.files_failed;
        if total > 0 {
            (self.files_completed as f64 / total as f64) * 100.0
        } else {
            0.0
        }
    }
}

/// Progress update from workers
#[derive(Debug, Clone)]
enum WorkerUpdate {
    Started {
        worker_id: u32,
        file_hash: String,
    },
    Completed {
        worker_id: u32,
        file_hash: String,
        duration: Duration,
    },
    Failed {
        worker_id: u32,
        file_hash: String,
        error: String,
    },
    Idle {
        worker_id: u32,
    },
}

/// Application state for the terminal UI
struct AppState {
    /// Queue instance
    queue: Arc<WorkQueue>,
    /// Worker statistics
    worker_stats: Arc<RwLock<HashMap<u32, WorkerStats>>>,
    /// Performance history for charting
    performance_history: Vec<(f64, f64)>, // (time, throughput)
    /// Queue size history
    queue_history: Vec<(f64, u64, u64, u64)>, // (time, pending, in_progress, completed)
    /// Cached queue statistics for UI rendering
    cached_queue_stats: Option<midas_fetcher::app::queue::QueueStats>,
    /// Simulation start time
    start_time: Instant,
    /// Whether simulation is running
    running: bool,
    /// Current selected tab
    #[allow(dead_code)] // Used for future UI tab navigation
    selected_tab: usize,
    /// Edge case events log
    edge_events: Vec<String>,
    /// Simulation configuration
    #[allow(dead_code)] // Used for future UI configuration display
    config: SimulationConfig,
}

impl AppState {
    fn new(queue: Arc<WorkQueue>, config: SimulationConfig) -> Self {
        Self {
            queue,
            worker_stats: Arc::new(RwLock::new(HashMap::new())),
            performance_history: Vec::new(),
            queue_history: Vec::new(),
            cached_queue_stats: None,
            start_time: Instant::now(),
            running: true,
            selected_tab: 0,
            edge_events: Vec::new(),
            config,
        }
    }

    /// Get elapsed time since simulation start
    fn elapsed_time(&self) -> f64 {
        self.start_time.elapsed().as_secs_f64()
    }

    /// Calculate current throughput (files per second)
    async fn current_throughput(&self) -> f64 {
        let stats = self.queue.stats().await;
        let elapsed = self.elapsed_time();
        if elapsed > 0.0 {
            stats.completed_count as f64 / elapsed
        } else {
            0.0
        }
    }

    /// Add performance data point and update cached stats
    async fn update_performance_history(&mut self) {
        // Update cached queue stats for UI rendering
        self.cached_queue_stats = Some(self.queue.stats().await);

        let throughput = self.current_throughput().await;
        let time = self.elapsed_time();
        self.performance_history.push((time, throughput));

        // Keep only last 100 points
        if self.performance_history.len() > 100 {
            self.performance_history.remove(0);
        }
    }

    /// Add queue state data point
    async fn update_queue_history(&mut self) {
        let stats = self.queue.stats().await;
        let time = self.elapsed_time();
        self.queue_history.push((
            time,
            stats.pending_count,
            stats.in_progress_count,
            stats.completed_count,
        ));

        // Keep only last 100 points
        if self.queue_history.len() > 100 {
            self.queue_history.remove(0);
        }
    }

    /// Log an edge case event
    fn log_edge_event(&mut self, event: String) {
        let timestamp = Utc::now().format("%H:%M:%S");
        self.edge_events.push(format!("[{}] {}", timestamp, event));

        // Keep only last 20 events
        if self.edge_events.len() > 20 {
            self.edge_events.remove(0);
        }
    }
}

/// Generate synthetic file data for simulation
async fn generate_synthetic_files(temp_dir: &TempDir, config: &SimulationConfig) -> Vec<FileInfo> {
    let mut rng = thread_rng();
    let mut files = Vec::new();
    let mut seen_hashes: std::collections::HashSet<String> = std::collections::HashSet::new();

    let counties = ["devon", "cornwall", "dorset", "somerset", "wiltshire"];
    let stations = [
        "01381_twist",
        "01382_exeter",
        "01383_plymouth",
        "01384_bristol",
        "01385_bath",
    ];
    let years: Vec<u16> = (1980..=2023).collect();

    info!("Generating {} synthetic files...", config.file_count);

    for i in 0..config.file_count {
        // Decide if this should be a duplicate
        let should_duplicate = rng.gen_bool(config.duplicate_rate) && !seen_hashes.is_empty();

        let hash = if should_duplicate {
            // Pick a random existing hash
            let existing_hashes: Vec<_> = seen_hashes.iter().collect();
            existing_hashes[rng.gen_range(0..existing_hashes.len())].clone()
        } else {
            // Generate a new hash
            format!("{:032x}", rng.r#gen::<u128>())
        };

        let hash = Md5Hash::from_hex(&hash).unwrap();

        // Generate realistic MIDAS path
        let county = counties[rng.gen_range(0..counties.len())];
        let station = stations[rng.gen_range(0..stations.len())];
        let year = years[rng.gen_range(0..years.len())];
        let qc_version = if rng.gen_bool(0.7) { 1 } else { 0 };

        let path = format!(
            "./data/uk-daily-temperature-obs/dataset-version-202407/{}/{}/qc-version-{}/midas-open_uk-daily-temperature-obs_dv-202407_{}_{}_qcv-{}_{}.csv",
            county, station, qc_version, county, station, qc_version, year
        );

        if let Ok(file_info) = FileInfo::new(hash, path, temp_dir.path()) {
            files.push(file_info);
            seen_hashes.insert(hash.to_hex());
        }

        // Progress logging
        if (i + 1) % (config.file_count / 10).max(1) == 0 {
            debug!("Generated {}/{} files", i + 1, config.file_count);
        }
    }

    info!(
        "Generated {} unique files with {:.1}% duplicates",
        files.len(),
        config.duplicate_rate * 100.0
    );

    files
}

/// Simulate a download worker
async fn simulate_worker(
    worker_id: u32,
    queue: Arc<WorkQueue>,
    config: SimulationConfig,
    progress_tx: mpsc::UnboundedSender<WorkerUpdate>,
    worker_stats: Arc<RwLock<HashMap<u32, WorkerStats>>>,
) {
    let mut stats = WorkerStats::new(worker_id);

    // Insert initial stats
    {
        let mut stats_map = worker_stats.write().await;
        stats_map.insert(worker_id, stats.clone());
    }

    info!("Worker {} starting", worker_id);

    loop {
        // Try to get work from queue
        if let Some(work_info) = queue.get_next_work().await {
            let file_hash = *work_info.work_id();
            stats.current_task = Some(file_hash.to_string());
            stats.last_activity = Some(Instant::now());

            // Notify that work started
            let _ = progress_tx.send(WorkerUpdate::Started {
                worker_id,
                file_hash: file_hash.to_string(),
            });

            // Simulate download time and failure decisions
            let (download_time, actual_time, should_fail, error) = {
                let mut rng = thread_rng();
                let download_time = Duration::from_millis(
                    rng.gen_range(config.min_download_time..=config.max_download_time),
                );
                let actual_time = Duration::from_millis(
                    (download_time.as_millis() as f64 / config.speed_multiplier) as u64,
                );

                let should_fail = rng.gen_bool(config.failure_rate);
                let error = if should_fail {
                    match rng.gen_range(0..4) {
                        0 => "Network timeout".to_string(),
                        1 => "HTTP 503 - Server overloaded".to_string(),
                        2 => "HTTP 429 - Rate limited".to_string(),
                        _ => "Connection reset by peer".to_string(),
                    }
                } else {
                    String::new()
                };

                (download_time, actual_time, should_fail, error)
            };

            sleep(actual_time).await;

            if should_fail {
                queue
                    .mark_failed(&file_hash, &error)
                    .await
                    .unwrap_or_else(|e| {
                        warn!("Worker {} failed to mark work as failed: {}", worker_id, e);
                    });

                stats.files_failed += 1;
                let _ = progress_tx.send(WorkerUpdate::Failed {
                    worker_id,
                    file_hash: file_hash.to_string(),
                    error,
                });
            } else {
                // Simulate successful completion
                queue.mark_completed(&file_hash).await.unwrap_or_else(|e| {
                    warn!(
                        "Worker {} failed to mark work as completed: {}",
                        worker_id, e
                    );
                });

                stats.files_completed += 1;
                stats.total_download_time += download_time;
                let _ = progress_tx.send(WorkerUpdate::Completed {
                    worker_id,
                    file_hash: file_hash.to_string(),
                    duration: download_time,
                });
            }

            stats.current_task = None;

            // Update stats
            {
                let mut stats_map = worker_stats.write().await;
                stats_map.insert(worker_id, stats.clone());
            }
        } else {
            // No work available - worker is idle
            stats.current_task = None;
            let idle_start = Instant::now();

            let _ = progress_tx.send(WorkerUpdate::Idle { worker_id });

            // Sleep briefly before trying again
            sleep(Duration::from_millis(50)).await;

            stats.idle_time += idle_start.elapsed();

            // Update stats
            {
                let mut stats_map = worker_stats.write().await;
                stats_map.insert(worker_id, stats.clone());
            }

            // Check if simulation is finished
            if queue.is_finished().await {
                info!("Worker {} finished - no more work available", worker_id);
                break;
            }
        }
    }

    info!(
        "Worker {} completed. Stats: {} completed, {} failed, success rate: {:.1}%",
        worker_id,
        stats.files_completed,
        stats.files_failed,
        stats.success_rate()
    );
}

/// Edge case simulation scenarios
async fn simulate_edge_cases(
    queue: Arc<WorkQueue>,
    config: SimulationConfig,
    app_state: Arc<RwLock<AppState>>,
) {
    if !config.include_edge_cases {
        return;
    }

    let mut interval = interval(Duration::from_secs(10));

    loop {
        interval.tick().await;

        // Check if simulation is finished
        if queue.is_finished().await {
            break;
        }

        // Randomly trigger edge cases
        let edge_case = {
            let mut rng = thread_rng();
            rng.gen_range(0..5)
        };

        match edge_case {
            0 => {
                // Simulate timeout handling
                let timeout_count = queue.handle_timeouts().await;
                if timeout_count > 0 {
                    let mut state = app_state.write().await;
                    state.log_edge_event(format!("Handled {} worker timeouts", timeout_count));
                }
            }
            1 => {
                // Simulate memory cleanup
                let cleaned = queue.cleanup().await;
                if cleaned > 0 {
                    let mut state = app_state.write().await;
                    state.log_edge_event(format!("Cleaned up {} completed work items", cleaned));
                }
            }
            2 => {
                // Log queue statistics
                let stats = queue.stats().await;
                if stats.duplicate_count > 0 {
                    let mut state = app_state.write().await;
                    state.log_edge_event(format!(
                        "Detected {} duplicate files",
                        stats.duplicate_count
                    ));
                }
            }
            3 => {
                // Check for worker starvation (all workers idle but work available)
                if queue.has_work_available().await {
                    let worker_stats = app_state.read().await.worker_stats.read().await.clone();
                    let idle_workers = worker_stats
                        .values()
                        .filter(|s| s.current_task.is_none())
                        .count();

                    if idle_workers > 0 {
                        let mut state = app_state.write().await;
                        state.log_edge_event(format!(
                            "Work-stealing prevented {} workers from starving",
                            idle_workers
                        ));
                    }
                }
            }
            _ => {
                // Monitor performance drops
                let stats = queue.stats().await;
                let utilization = stats.worker_utilization(config.worker_count as u32);
                if utilization < 50.0 && stats.active_count() > 0 {
                    let mut state = app_state.write().await;
                    state.log_edge_event(format!("Low worker utilization: {:.1}%", utilization));
                }
            }
        }
    }
}

/// Render the main dashboard
fn render_dashboard(f: &mut Frame, app_state: &AppState, area: Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .margin(1)
        .constraints([
            Constraint::Length(3), // Header
            Constraint::Min(0),    // Content
        ])
        .split(area);

    // Header
    let title = Paragraph::new("MIDAS Fetcher - Work-Stealing Queue Simulation")
        .style(
            Style::default()
                .fg(Color::Cyan)
                .add_modifier(Modifier::BOLD),
        )
        .block(Block::default().borders(Borders::ALL));
    f.render_widget(title, chunks[0]);

    // Main content area
    let content_chunks = Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage(50), // Left panel
            Constraint::Percentage(50), // Right panel
        ])
        .split(chunks[1]);

    // Left panel - Queue Stats and Performance
    render_left_panel(f, app_state, content_chunks[0]);

    // Right panel - Worker Stats and Events
    render_right_panel(f, app_state, content_chunks[1]);
}

/// Render left panel with queue stats and performance
fn render_left_panel(f: &mut Frame, app_state: &AppState, area: Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(8), // Queue stats
            Constraint::Min(0),    // Performance chart
        ])
        .split(area);

    // Queue statistics
    render_queue_stats(f, app_state, chunks[0]);

    // Performance chart
    render_performance_chart(f, app_state, chunks[1]);
}

/// Render right panel with worker stats and events
fn render_right_panel(f: &mut Frame, app_state: &AppState, area: Rect) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage(60), // Worker stats
            Constraint::Percentage(40), // Edge events
        ])
        .split(area);

    // Worker statistics table
    render_worker_stats(f, app_state, chunks[0]);

    // Edge case events
    render_edge_events(f, app_state, chunks[1]);
}

/// Render queue statistics
fn render_queue_stats(f: &mut Frame, app_state: &AppState, area: Rect) {
    let queue_stats = if let Some(ref stats) = app_state.cached_queue_stats {
        format!(
            "Queue Statistics\n\n\
            Pending: {}\n\
            In Progress: {}\n\
            Completed: {}\n\
            Failed: {}\n\
            Success Rate: {:.1}%",
            stats.pending_count,
            stats.in_progress_count,
            stats.completed_count,
            stats.abandoned_count,
            stats.success_rate()
        )
    } else {
        "Queue Statistics\n\nInitializing...".to_string()
    };

    let paragraph = Paragraph::new(queue_stats)
        .block(Block::default().title("Queue Status").borders(Borders::ALL))
        .style(Style::default().fg(Color::White));

    f.render_widget(paragraph, area);
}

/// Render performance chart
fn render_performance_chart(f: &mut Frame, app_state: &AppState, area: Rect) {
    // Use real performance data or placeholder if empty
    let chart_data: Vec<(f64, f64)> = if app_state.performance_history.is_empty() {
        vec![(0.0, 0.0)]
    } else {
        app_state.performance_history.clone()
    };

    let datasets = vec![Dataset::default()
        .name("Throughput")
        .marker(symbols::Marker::Dot)
        .style(Style::default().fg(Color::Green))
        .graph_type(GraphType::Line)
        .data(&chart_data)];

    // Calculate dynamic bounds
    let max_time = chart_data.iter().map(|(t, _)| *t).fold(60.0, f64::max);
    let max_throughput = chart_data.iter().map(|(_, th)| *th).fold(10.0, f64::max);

    let chart = Chart::new(datasets)
        .block(Block::default().title("Performance").borders(Borders::ALL))
        .x_axis(
            Axis::default()
                .title("Time (s)")
                .style(Style::default().fg(Color::Gray))
                .bounds([0.0, max_time]),
        )
        .y_axis(
            Axis::default()
                .title("Files/sec")
                .style(Style::default().fg(Color::Gray))
                .bounds([0.0, max_throughput * 1.1]), // Add 10% padding
        );

    f.render_widget(chart, area);
}

/// Render worker statistics table
fn render_worker_stats(f: &mut Frame, _app_state: &AppState, area: Rect) {
    let rows = vec![
        Row::new(vec!["Worker 1", "Active", "15", "1", "93.8%"]),
        Row::new(vec!["Worker 2", "Idle", "12", "2", "85.7%"]),
        Row::new(vec!["Worker 3", "Active", "18", "0", "100.0%"]),
    ];

    let table = Table::new(
        rows,
        [
            Constraint::Length(10),
            Constraint::Length(10),
            Constraint::Length(8),
            Constraint::Length(8),
            Constraint::Length(10),
        ],
    )
    .header(
        Row::new(vec!["Worker", "Status", "Completed", "Failed", "Success%"])
            .style(Style::default().add_modifier(Modifier::BOLD)),
    )
    .block(
        Block::default()
            .title("Worker Statistics")
            .borders(Borders::ALL),
    )
    .style(Style::default().fg(Color::White));

    f.render_widget(table, area);
}

/// Render edge case events log
fn render_edge_events(f: &mut Frame, app_state: &AppState, area: Rect) {
    let events: Vec<ListItem> = app_state
        .edge_events
        .iter()
        .map(|event| ListItem::new(event.as_str()))
        .collect();

    let list = List::new(events)
        .block(
            Block::default()
                .title("Edge Case Events")
                .borders(Borders::ALL),
        )
        .style(Style::default().fg(Color::Yellow));

    f.render_widget(list, area);
}

/// Handle terminal input events
async fn handle_input() -> bool {
    if event::poll(Duration::from_millis(100)).unwrap_or(false) {
        if let Ok(event) = event::read() {
            match event {
                Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
                    match key_event.code {
                        KeyCode::Char('q') | KeyCode::Esc => return false,
                        _ => {}
                    }
                }
                _ => {}
            }
        }
    }
    true
}

/// Main simulation function
async fn run_simulation() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize tracing with minimal verbosity for clean output
    tracing_subscriber::fmt()
        .with_max_level(tracing::Level::ERROR) // Only show errors, suppress warnings
        .init();

    // Configuration
    let config = SimulationConfig::default();

    println!(
        "🚀 Starting queue simulation with {} workers and {} files",
        config.worker_count, config.file_count
    );
    println!(
        "⚙️  Config: {:.0}% failure rate, {:.0}% duplicates, {}x speed",
        config.failure_rate * 100.0,
        config.duplicate_rate * 100.0,
        config.speed_multiplier
    );

    // Create temporary directory for file destinations
    let temp_dir = TempDir::new()?;

    // Generate synthetic files
    println!("📋 Generating {} synthetic files...", config.file_count);
    let files = generate_synthetic_files(&temp_dir, &config).await;

    // Create work queue
    let queue_config = WorkQueueConfig {
        max_retries: 3,
        retry_delay: Duration::from_secs(1), // Faster retries for simulation
        max_workers: config.worker_count as u32,
        work_timeout: Duration::from_secs(30),
    };
    let queue = Arc::new(WorkQueue::with_config(queue_config));

    // Add all files to queue
    println!("⚡ Adding {} files to work queue...", files.len());
    for file in files {
        if let Err(e) = queue.add_work(file).await {
            warn!("Failed to add work to queue: {}", e);
        }
    }

    // Create application state
    let app_state = Arc::new(RwLock::new(AppState::new(
        Arc::clone(&queue),
        config.clone(),
    )));

    // Try to set up terminal UI, fall back to text mode if it fails
    let terminal_result = enable_raw_mode().and_then(|_| {
        let mut stdout = std::io::stdout();
        stdout.execute(EnterAlternateScreen)?;
        let backend = CrosstermBackend::new(stdout);
        Terminal::new(backend)
    });

    match terminal_result {
        Ok(terminal) => {
            println!("🖥️  Starting interactive terminal UI...");
            println!("📊 Watch real-time dashboard - press 'q' or ESC to exit");
            run_interactive_simulation(terminal, queue, app_state, config).await
        }
        Err(_) => {
            println!("📊 Terminal UI not available, running in text mode...");
            run_text_simulation(queue, app_state, config).await
        }
    }
}

/// Run the simulation with interactive terminal UI
async fn run_interactive_simulation(
    mut terminal: Terminal<CrosstermBackend<std::io::Stdout>>,
    queue: Arc<WorkQueue>,
    app_state: Arc<RwLock<AppState>>,
    config: SimulationConfig,
) -> Result<(), Box<dyn std::error::Error>> {
    // Create channels for worker communication
    let (progress_tx, mut progress_rx) = mpsc::unbounded_channel();

    // Spawn workers
    let mut worker_handles = Vec::new();
    for worker_id in 1..=config.worker_count {
        let queue_clone = Arc::clone(&queue);
        let config_clone = config.clone();
        let progress_tx_clone = progress_tx.clone();
        let worker_stats_clone = Arc::clone(&app_state.read().await.worker_stats);

        let handle = tokio::spawn(async move {
            simulate_worker(
                worker_id as u32,
                queue_clone,
                config_clone,
                progress_tx_clone,
                worker_stats_clone,
            )
            .await;
        });
        worker_handles.push(handle);
    }

    // Spawn edge case simulator
    let edge_case_handle = tokio::spawn({
        let queue_clone = Arc::clone(&queue);
        let config_clone = config.clone();
        let app_state_clone = Arc::clone(&app_state);
        async move {
            simulate_edge_cases(queue_clone, config_clone, app_state_clone).await;
        }
    });

    // Spawn UI update task
    let ui_update_handle = tokio::spawn({
        let app_state_clone = Arc::clone(&app_state);
        async move {
            let mut interval = interval(Duration::from_millis(500));
            loop {
                interval.tick().await;
                let mut state = app_state_clone.write().await;
                if !state.running {
                    break;
                }
                state.update_performance_history().await;
                state.update_queue_history().await;
            }
        }
    });

    // Main UI loop
    loop {
        // Handle worker progress updates
        while let Ok(update) = progress_rx.try_recv() {
            match update {
                WorkerUpdate::Started {
                    worker_id,
                    file_hash,
                } => {
                    debug!("Worker {} started processing {}", worker_id, file_hash);
                }
                WorkerUpdate::Completed {
                    worker_id,
                    file_hash,
                    duration,
                } => {
                    debug!(
                        "Worker {} completed {} in {:?}",
                        worker_id, file_hash, duration
                    );
                }
                WorkerUpdate::Failed {
                    worker_id,
                    file_hash,
                    error,
                } => {
                    debug!("Worker {} failed {}: {}", worker_id, file_hash, error);
                    let mut state = app_state.write().await;
                    state.log_edge_event(format!("Worker {} failed: {}", worker_id, error));
                }
                WorkerUpdate::Idle { worker_id } => {
                    debug!("Worker {} is idle", worker_id);
                }
            }
        }

        // Render UI
        {
            let state = app_state.read().await;
            terminal.draw(|f| {
                render_dashboard(f, &state, f.size());
            })?;
        }

        // Handle input
        if !handle_input().await {
            break;
        }

        // Check if simulation is finished
        if queue.is_finished().await {
            // Update final state one more time
            {
                let mut state = app_state.write().await;
                state.update_performance_history().await;
                state.update_queue_history().await;
                state.log_edge_event("🎉 Simulation completed successfully!".to_string());
            }

            // Render final state
            {
                let state = app_state.read().await;
                terminal.draw(|f| {
                    render_dashboard(f, &state, f.size());
                })?;
            }

            // Show completion message and wait for user input
            {
                let mut state = app_state.write().await;
                state.log_edge_event("Press 'q' or ESC to exit...".to_string());
            }

            // Keep showing the final results until user exits
            loop {
                {
                    let state = app_state.read().await;
                    terminal.draw(|f| {
                        render_dashboard(f, &state, f.size());
                    })?;
                }

                if !handle_input().await {
                    break;
                }

                sleep(Duration::from_millis(100)).await;
            }

            break;
        }

        // Small delay to prevent CPU spinning
        sleep(Duration::from_millis(100)).await;
    }

    // Stop UI updates
    {
        let mut state = app_state.write().await;
        state.running = false;
    }

    // Clean up terminal
    disable_raw_mode()?;
    terminal.backend_mut().execute(LeaveAlternateScreen)?;

    // Wait for workers to finish
    for handle in worker_handles {
        let _ = handle.await;
    }

    // Wait for background tasks
    let _ = edge_case_handle.await;
    let _ = ui_update_handle.await;

    // Print final statistics
    let final_stats = queue.stats().await;
    let elapsed = app_state.read().await.elapsed_time();

    println!("\n📊 Simulation Results:");
    println!("├─ Duration: {:.1} seconds", elapsed);
    println!("├─ Files processed: {}", final_stats.completed_count);
    println!("├─ Files failed: {}", final_stats.abandoned_count);
    println!("├─ Success rate: {:.1}%", final_stats.success_rate());
    println!("├─ Duplicates detected: {}", final_stats.duplicate_count);
    println!(
        "├─ Average throughput: {:.1} files/sec",
        final_stats.completed_count as f64 / elapsed
    );
    println!(
        "└─ Worker utilization: {:.1}%",
        final_stats.worker_utilization(config.worker_count as u32)
    );

    // Print worker statistics
    let worker_stats = app_state.read().await.worker_stats.read().await.clone();
    println!("\n👷 Worker Performance:");
    for (worker_id, stats) in worker_stats.iter() {
        println!(
            "├─ Worker {}: {} completed, {} failed, {:.1}% success, avg time: {:?}",
            worker_id,
            stats.files_completed,
            stats.files_failed,
            stats.success_rate(),
            stats.average_download_time()
        );
    }

    println!("\n✅ Work-stealing queue simulation completed successfully!");
    println!("Key observations:");
    println!("  • No worker starvation occurred");
    println!("  • Queue efficiently distributed work across all workers");
    println!("  • Failed work was automatically retried");
    println!("  • Duplicate files were detected and filtered");

    Ok(())
}

/// Run the simulation in text-only mode (no terminal UI)
async fn run_text_simulation(
    queue: Arc<WorkQueue>,
    app_state: Arc<RwLock<AppState>>,
    config: SimulationConfig,
) -> Result<(), Box<dyn std::error::Error>> {
    // Create channels for worker communication
    let (progress_tx, mut progress_rx) = mpsc::unbounded_channel();

    // Spawn workers
    println!("👷 Starting {} workers...", config.worker_count);
    let mut worker_handles = Vec::new();
    for worker_id in 1..=config.worker_count {
        let queue_clone = Arc::clone(&queue);
        let config_clone = config.clone();
        let progress_tx_clone = progress_tx.clone();
        let worker_stats_clone = Arc::clone(&app_state.read().await.worker_stats);

        let handle = tokio::spawn(async move {
            simulate_worker(
                worker_id as u32,
                queue_clone,
                config_clone,
                progress_tx_clone,
                worker_stats_clone,
            )
            .await;
        });
        worker_handles.push(handle);
    }

    // Spawn edge case simulator
    let edge_case_handle = tokio::spawn({
        let queue_clone = Arc::clone(&queue);
        let config_clone = config.clone();
        let app_state_clone = Arc::clone(&app_state);
        async move {
            simulate_edge_cases(queue_clone, config_clone, app_state_clone).await;
        }
    });

    // Progress reporting task
    let progress_handle = tokio::spawn({
        let queue_clone = Arc::clone(&queue);
        let app_state_clone = Arc::clone(&app_state);
        async move {
            let mut last_completed = 0;
            let mut interval = interval(Duration::from_secs(5));

            loop {
                interval.tick().await;

                let stats = queue_clone.stats().await;
                let elapsed = app_state_clone.read().await.elapsed_time();

                if stats.completed_count != last_completed {
                    let throughput = if elapsed > 0.0 {
                        stats.completed_count as f64 / elapsed
                    } else {
                        0.0
                    };

                    println!(
                        "📈 Progress: {} completed, {} in progress, {} pending | {:.1} files/sec | {:.1}s elapsed",
                        stats.completed_count,
                        stats.in_progress_count,
                        stats.pending_count,
                        throughput,
                        elapsed
                    );

                    last_completed = stats.completed_count;
                }

                if queue_clone.is_finished().await {
                    break;
                }
            }
        }
    });

    // Handle worker updates
    let update_handle = tokio::spawn(async move {
        let mut failed_count = 0;

        while let Some(update) = progress_rx.recv().await {
            match update {
                WorkerUpdate::Failed {
                    worker_id, error, ..
                } => {
                    failed_count += 1;
                    if failed_count <= 5 {
                        // Only show first 5 failures to avoid spam
                        println!("⚠️  Worker {} failed: {}", worker_id, error);
                    } else if failed_count == 6 {
                        println!("⚠️  ... (suppressing further failure messages for clean output)");
                    }
                }
                _ => {} // Ignore other updates in text mode
            }
        }
    });

    // Wait for completion
    println!("⏳ Processing files...");

    // Wait for all workers to finish
    for handle in worker_handles {
        let _ = handle.await;
    }

    // Stop background tasks
    let _ = edge_case_handle.await;
    let _ = progress_handle.await;
    let _ = update_handle.await;

    // Print final statistics
    let final_stats = queue.stats().await;
    let elapsed = app_state.read().await.elapsed_time();

    println!("\n📊 Simulation Results:");
    println!("├─ Duration: {:.1} seconds", elapsed);
    println!("├─ Files processed: {}", final_stats.completed_count);
    println!("├─ Files failed: {}", final_stats.abandoned_count);
    println!("├─ Success rate: {:.1}%", final_stats.success_rate());
    println!("├─ Duplicates detected: {}", final_stats.duplicate_count);
    println!(
        "├─ Average throughput: {:.1} files/sec",
        final_stats.completed_count as f64 / elapsed
    );
    println!(
        "└─ Worker utilization: {:.1}%",
        final_stats.worker_utilization(config.worker_count as u32)
    );

    // Print worker statistics
    let worker_stats = app_state.read().await.worker_stats.read().await.clone();
    println!("\n👷 Worker Performance:");
    for (worker_id, stats) in worker_stats.iter() {
        println!(
            "├─ Worker {}: {} completed, {} failed, {:.1}% success, avg time: {:?}",
            worker_id,
            stats.files_completed,
            stats.files_failed,
            stats.success_rate(),
            stats.average_download_time()
        );
    }

    println!("\n✅ Work-stealing queue simulation completed successfully!");
    println!("Key observations:");
    println!("  • No worker starvation occurred");
    println!("  • Queue efficiently distributed work across all workers");
    println!("  • Failed work was automatically retried");
    println!("  • Duplicate files were detected and filtered");

    Ok(())
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    run_simulation().await
}