oxigdal-cli 0.1.4

Command-line interface for OxiGDAL geospatial operations
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
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
//! Advanced progress bars and logging system for OxiGDAL CLI
//!
//! This module provides comprehensive progress tracking and structured logging
//! capabilities for long-running geospatial operations.
//!
//! # Features
//!
//! - Multi-progress bar support for parallel operations
//! - Tile processing progress with spatial context
//! - Byte transfer progress with throughput statistics
//! - Accurate time estimates based on operation history
//! - Structured logging with multiple outputs
//! - Configurable verbosity levels
//! - File-based logging
//! - Rich color support
//!
//! # Example
//!
//! ```no_run
//! use oxigdal_cli::progress::{ProgressManager, VerbosityLevel, LogConfig};
//!
//! let config = LogConfig::new()
//!     .with_verbosity(VerbosityLevel::Info)
//!     .with_colors(true);
//! let manager = ProgressManager::new(config);
//!
//! let tile_tracker = manager.create_tile_tracker(100, 100, "Processing tiles")?;
//! for y in 0..100 {
//!     for x in 0..100 {
//!         tile_tracker.advance_tile(x, y);
//!     }
//! }
//! tile_tracker.finish();
//! # Ok::<(), anyhow::Error>(())
//! ```

use console::{Style, Term};
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use std::collections::VecDeque;
use std::fmt;
use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant, SystemTime};
use thiserror::Error;
use tracing::{Level, Subscriber};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, Layer, fmt as tracing_fmt};

/// Progress system error types
#[derive(Debug, Error)]
pub enum ProgressError {
    #[error("Failed to create log file: {0}")]
    LogFileCreation(#[source] std::io::Error),

    #[error("Failed to write to log: {0}")]
    LogWrite(#[source] std::io::Error),

    #[error("Progress style template error: {0}")]
    StyleTemplate(String),

    #[error("Invalid configuration: {0}")]
    InvalidConfig(String),

    #[error("Lock acquisition failed")]
    LockFailed,
}

/// Result type for progress operations
pub type ProgressResult<T> = Result<T, ProgressError>;

/// Verbosity levels for logging output
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum VerbosityLevel {
    /// Suppress all output except errors
    Quiet,
    /// Show only error messages
    Error,
    /// Show errors and warnings
    Warn,
    /// Standard informational output
    #[default]
    Info,
    /// Detailed debug information
    Debug,
    /// Maximum verbosity with trace-level details
    Trace,
}

impl VerbosityLevel {
    /// Convert to tracing Level
    #[must_use]
    pub const fn to_tracing_level(self) -> Level {
        match self {
            Self::Quiet | Self::Error => Level::ERROR,
            Self::Warn => Level::WARN,
            Self::Info => Level::INFO,
            Self::Debug => Level::DEBUG,
            Self::Trace => Level::TRACE,
        }
    }

    /// Check if logging is enabled at this level
    #[must_use]
    pub const fn is_enabled(&self, target: Self) -> bool {
        *self as u8 >= target as u8
    }
}

impl fmt::Display for VerbosityLevel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Quiet => write!(f, "quiet"),
            Self::Error => write!(f, "error"),
            Self::Warn => write!(f, "warn"),
            Self::Info => write!(f, "info"),
            Self::Debug => write!(f, "debug"),
            Self::Trace => write!(f, "trace"),
        }
    }
}

impl std::str::FromStr for VerbosityLevel {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "quiet" | "q" | "0" => Ok(Self::Quiet),
            "error" | "e" | "1" => Ok(Self::Error),
            "warn" | "warning" | "w" | "2" => Ok(Self::Warn),
            "info" | "i" | "3" => Ok(Self::Info),
            "debug" | "d" | "4" => Ok(Self::Debug),
            "trace" | "t" | "5" => Ok(Self::Trace),
            _ => Err(format!("Unknown verbosity level: {}", s)),
        }
    }
}

/// Color theme configuration for progress output
#[derive(Debug, Clone)]
pub struct ColorTheme {
    /// Style for progress bar fill
    pub progress_fill: Style,
    /// Style for progress bar background
    pub progress_bg: Style,
    /// Style for success messages
    pub success: Style,
    /// Style for warning messages
    pub warning: Style,
    /// Style for error messages
    pub error: Style,
    /// Style for info messages
    pub info: Style,
    /// Style for debug messages
    pub debug: Style,
    /// Style for labels/headers
    pub label: Style,
    /// Style for values/data
    pub value: Style,
    /// Style for paths/filenames
    pub path: Style,
    /// Style for metrics/numbers
    pub metric: Style,
    /// Style for timestamps
    pub timestamp: Style,
}

impl Default for ColorTheme {
    fn default() -> Self {
        Self {
            progress_fill: Style::new().cyan(),
            progress_bg: Style::new().blue().dim(),
            success: Style::new().green().bold(),
            warning: Style::new().yellow().bold(),
            error: Style::new().red().bold(),
            info: Style::new().white(),
            debug: Style::new().dim(),
            label: Style::new().bold(),
            value: Style::new().cyan(),
            path: Style::new().green(),
            metric: Style::new().magenta(),
            timestamp: Style::new().dim(),
        }
    }
}

impl ColorTheme {
    /// Create a theme with no colors (for non-TTY output)
    #[must_use]
    pub fn plain() -> Self {
        Self {
            progress_fill: Style::new(),
            progress_bg: Style::new(),
            success: Style::new(),
            warning: Style::new(),
            error: Style::new(),
            info: Style::new(),
            debug: Style::new(),
            label: Style::new(),
            value: Style::new(),
            path: Style::new(),
            metric: Style::new(),
            timestamp: Style::new(),
        }
    }

    /// Create a dark theme optimized for dark terminals
    #[must_use]
    pub fn dark() -> Self {
        Self {
            progress_fill: Style::new().cyan().bright(),
            progress_bg: Style::new().blue().dim(),
            success: Style::new().green().bright().bold(),
            warning: Style::new().yellow().bright().bold(),
            error: Style::new().red().bright().bold(),
            info: Style::new().white().bright(),
            debug: Style::new().white().dim(),
            label: Style::new().white().bold(),
            value: Style::new().cyan().bright(),
            path: Style::new().green().bright(),
            metric: Style::new().magenta().bright(),
            timestamp: Style::new().white().dim(),
        }
    }

    /// Create a light theme optimized for light terminals
    #[must_use]
    pub fn light() -> Self {
        Self {
            progress_fill: Style::new().blue(),
            progress_bg: Style::new().black().dim(),
            success: Style::new().green(),
            warning: Style::new().yellow(),
            error: Style::new().red(),
            info: Style::new().black(),
            debug: Style::new().black().dim(),
            label: Style::new().black().bold(),
            value: Style::new().blue(),
            path: Style::new().green(),
            metric: Style::new().magenta(),
            timestamp: Style::new().black().dim(),
        }
    }
}

/// Configuration for the logging system
#[derive(Debug, Clone)]
pub struct LogConfig {
    /// Verbosity level
    pub verbosity: VerbosityLevel,
    /// Enable colored output
    pub colors_enabled: bool,
    /// Color theme
    pub theme: ColorTheme,
    /// Log file path (optional)
    pub log_file: Option<PathBuf>,
    /// Include timestamps in output
    pub show_timestamps: bool,
    /// Include target module in output
    pub show_targets: bool,
    /// Include thread IDs in output
    pub show_thread_ids: bool,
    /// Include source file/line info
    pub show_source_location: bool,
    /// Maximum log line width (0 = no limit)
    pub max_line_width: usize,
    /// Structured JSON logging
    pub json_output: bool,
}

impl Default for LogConfig {
    fn default() -> Self {
        Self {
            verbosity: VerbosityLevel::Info,
            colors_enabled: true,
            theme: ColorTheme::default(),
            log_file: None,
            show_timestamps: false,
            show_targets: false,
            show_thread_ids: false,
            show_source_location: false,
            max_line_width: 0,
            json_output: false,
        }
    }
}

impl LogConfig {
    /// Create a new log configuration
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set verbosity level
    #[must_use]
    pub fn with_verbosity(mut self, level: VerbosityLevel) -> Self {
        self.verbosity = level;
        self
    }

    /// Enable or disable colors
    #[must_use]
    pub fn with_colors(mut self, enabled: bool) -> Self {
        self.colors_enabled = enabled;
        if !enabled {
            self.theme = ColorTheme::plain();
        }
        self
    }

    /// Set color theme
    #[must_use]
    pub fn with_theme(mut self, theme: ColorTheme) -> Self {
        self.theme = theme;
        self
    }

    /// Set log file path
    #[must_use]
    pub fn with_log_file(mut self, path: PathBuf) -> Self {
        self.log_file = Some(path);
        self
    }

    /// Enable timestamps
    #[must_use]
    pub fn with_timestamps(mut self, enabled: bool) -> Self {
        self.show_timestamps = enabled;
        self
    }

    /// Enable target module display
    #[must_use]
    pub fn with_targets(mut self, enabled: bool) -> Self {
        self.show_targets = enabled;
        self
    }

    /// Enable thread ID display
    #[must_use]
    pub fn with_thread_ids(mut self, enabled: bool) -> Self {
        self.show_thread_ids = enabled;
        self
    }

    /// Enable source location display
    #[must_use]
    pub fn with_source_location(mut self, enabled: bool) -> Self {
        self.show_source_location = enabled;
        self
    }

    /// Enable JSON output
    #[must_use]
    pub fn with_json(mut self, enabled: bool) -> Self {
        self.json_output = enabled;
        self
    }

    /// Set maximum line width
    #[must_use]
    pub fn with_max_width(mut self, width: usize) -> Self {
        self.max_line_width = width;
        self
    }

    /// Check if TTY is available and configure colors accordingly
    #[must_use]
    pub fn auto_colors(mut self) -> Self {
        self.colors_enabled = Term::stdout().is_term();
        if !self.colors_enabled {
            self.theme = ColorTheme::plain();
        }
        self
    }
}

/// Time estimation tracker for accurate ETA calculations
#[derive(Debug)]
pub struct TimeEstimator {
    /// Start time of the operation
    start_time: Instant,
    /// History of completion rates (items per second)
    rate_history: VecDeque<f64>,
    /// Maximum history size
    history_size: usize,
    /// Total items to process
    total_items: u64,
    /// Items processed so far
    processed_items: AtomicU64,
    /// Last update timestamp
    last_update: RwLock<Instant>,
    /// Items at last update
    last_items: AtomicU64,
    /// Smoothing factor for exponential moving average
    smoothing_factor: f64,
    /// Current smoothed rate
    smoothed_rate: RwLock<f64>,
}

impl TimeEstimator {
    /// Create a new time estimator
    #[must_use]
    pub fn new(total_items: u64) -> Self {
        Self {
            start_time: Instant::now(),
            rate_history: VecDeque::with_capacity(20),
            history_size: 20,
            total_items,
            processed_items: AtomicU64::new(0),
            last_update: RwLock::new(Instant::now()),
            last_items: AtomicU64::new(0),
            smoothing_factor: 0.3,
            smoothed_rate: RwLock::new(0.0),
        }
    }

    /// Create with custom history size
    #[must_use]
    pub fn with_history_size(mut self, size: usize) -> Self {
        self.history_size = size;
        self.rate_history = VecDeque::with_capacity(size);
        self
    }

    /// Update the progress
    pub fn update(&self, processed: u64) {
        self.processed_items.store(processed, Ordering::Relaxed);

        let now = Instant::now();
        let last_update = self.last_update.read().map_or(now, |guard| *guard);
        let elapsed = now.duration_since(last_update);

        // Only update rate every 100ms to avoid noise
        if elapsed >= Duration::from_millis(100) {
            let last_items = self.last_items.load(Ordering::Relaxed);
            let items_delta = processed.saturating_sub(last_items);
            let rate = items_delta as f64 / elapsed.as_secs_f64();

            // Update smoothed rate using exponential moving average
            if let Ok(mut smoothed) = self.smoothed_rate.write() {
                if *smoothed == 0.0 {
                    *smoothed = rate;
                } else {
                    *smoothed = self.smoothing_factor * rate + (1.0 - self.smoothing_factor) * *smoothed;
                }
            }

            // Update last values
            if let Ok(mut guard) = self.last_update.write() {
                *guard = now;
            }
            self.last_items.store(processed, Ordering::Relaxed);
        }
    }

    /// Increment progress by one
    pub fn increment(&self) {
        let processed = self.processed_items.fetch_add(1, Ordering::Relaxed) + 1;
        self.update(processed);
    }

    /// Get elapsed time
    #[must_use]
    pub fn elapsed(&self) -> Duration {
        self.start_time.elapsed()
    }

    /// Get estimated time remaining
    #[must_use]
    pub fn eta(&self) -> Option<Duration> {
        let processed = self.processed_items.load(Ordering::Relaxed);
        if processed == 0 {
            return None;
        }

        let remaining = self.total_items.saturating_sub(processed);
        if remaining == 0 {
            return Some(Duration::ZERO);
        }

        let rate = self.smoothed_rate.read().ok().map(|r| *r)?;
        if rate <= 0.0 {
            return None;
        }

        let eta_secs = remaining as f64 / rate;
        Some(Duration::from_secs_f64(eta_secs))
    }

    /// Get current processing rate (items per second)
    #[must_use]
    pub fn rate(&self) -> f64 {
        self.smoothed_rate.read().map_or(0.0, |r| *r)
    }

    /// Get progress percentage
    #[must_use]
    pub fn percentage(&self) -> f64 {
        if self.total_items == 0 {
            return 100.0;
        }
        let processed = self.processed_items.load(Ordering::Relaxed);
        (processed as f64 / self.total_items as f64) * 100.0
    }

    /// Format ETA as human-readable string
    #[must_use]
    pub fn format_eta(&self) -> String {
        match self.eta() {
            Some(eta) if eta.as_secs() == 0 => "< 1s".to_string(),
            Some(eta) => format_duration(eta),
            None => "calculating...".to_string(),
        }
    }
}

/// Format duration as human-readable string
fn format_duration(duration: Duration) -> String {
    let total_secs = duration.as_secs();

    if total_secs < 60 {
        return format!("{}s", total_secs);
    }

    let hours = total_secs / 3600;
    let minutes = (total_secs % 3600) / 60;
    let seconds = total_secs % 60;

    if hours > 0 {
        format!("{}h {}m {}s", hours, minutes, seconds)
    } else {
        format!("{}m {}s", minutes, seconds)
    }
}

/// Tile-aware progress tracker for raster operations
#[derive(Debug)]
pub struct TileProgressTracker {
    /// Progress bar
    progress_bar: ProgressBar,
    /// Time estimator
    estimator: Arc<TimeEstimator>,
    /// Number of tiles in X direction
    tiles_x: u64,
    /// Number of tiles in Y direction
    tiles_y: u64,
    /// Current tile X
    current_x: AtomicU64,
    /// Current tile Y
    current_y: AtomicU64,
    /// Whether to show spatial position
    show_position: bool,
}

impl TileProgressTracker {
    /// Create a new tile progress tracker
    pub fn new(
        multi: &MultiProgress,
        tiles_x: u64,
        tiles_y: u64,
        message: &str,
    ) -> ProgressResult<Self> {
        let total = tiles_x * tiles_y;
        let estimator = Arc::new(TimeEstimator::new(total));

        let style = ProgressStyle::default_bar()
            .template("{msg} [{bar:40.cyan/blue}] {pos}/{len} tiles ({percent}%) | ETA: {eta} | {per_sec}")
            .map_err(|e| ProgressError::StyleTemplate(e.to_string()))?
            .progress_chars("#>-");

        let pb = multi.add(ProgressBar::new(total));
        pb.set_style(style);
        pb.set_message(message.to_string());
        pb.enable_steady_tick(Duration::from_millis(100));

        Ok(Self {
            progress_bar: pb,
            estimator,
            tiles_x,
            tiles_y,
            current_x: AtomicU64::new(0),
            current_y: AtomicU64::new(0),
            show_position: true,
        })
    }

    /// Enable or disable spatial position display
    #[must_use]
    pub fn with_position_display(self, enabled: bool) -> Self {
        Self {
            show_position: enabled,
            ..self
        }
    }

    /// Advance to the next tile
    pub fn advance(&self) {
        self.progress_bar.inc(1);
        self.estimator.increment();
    }

    /// Advance to a specific tile position
    pub fn advance_tile(&self, x: u64, y: u64) {
        self.current_x.store(x, Ordering::Relaxed);
        self.current_y.store(y, Ordering::Relaxed);

        let pos = y * self.tiles_x + x;
        self.progress_bar.set_position(pos);
        self.estimator.update(pos);

        if self.show_position {
            self.progress_bar.set_message(format!(
                "Processing tile ({}, {}) of ({}, {})",
                x, y, self.tiles_x, self.tiles_y
            ));
        }
    }

    /// Get the current tile position
    #[must_use]
    pub fn current_position(&self) -> (u64, u64) {
        (
            self.current_x.load(Ordering::Relaxed),
            self.current_y.load(Ordering::Relaxed),
        )
    }

    /// Get estimated time remaining
    #[must_use]
    pub fn eta(&self) -> Option<Duration> {
        self.estimator.eta()
    }

    /// Finish the progress bar
    pub fn finish(&self) {
        self.progress_bar.finish_with_message("Completed");
    }

    /// Finish with error
    pub fn finish_with_error(&self, error: &str) {
        self.progress_bar.abandon_with_message(format!("Error: {}", error));
    }
}

/// Byte transfer progress tracker with throughput statistics
#[derive(Debug)]
pub struct ByteTransferTracker {
    /// Progress bar
    progress_bar: ProgressBar,
    /// Time estimator
    estimator: Arc<TimeEstimator>,
    /// Total bytes to transfer
    total_bytes: u64,
    /// Bytes transferred so far
    transferred_bytes: AtomicU64,
    /// Peak transfer rate
    peak_rate: RwLock<f64>,
    /// Transfer start time
    start_time: Instant,
}

impl ByteTransferTracker {
    /// Create a new byte transfer tracker
    pub fn new(
        multi: &MultiProgress,
        total_bytes: u64,
        label: &str,
    ) -> ProgressResult<Self> {
        let estimator = Arc::new(TimeEstimator::new(total_bytes));

        let style = ProgressStyle::default_bar()
            .template("{msg} [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}) | ETA: {eta}")
            .map_err(|e| ProgressError::StyleTemplate(e.to_string()))?
            .progress_chars("#>-");

        let pb = multi.add(ProgressBar::new(total_bytes));
        pb.set_style(style);
        pb.set_message(label.to_string());
        pb.enable_steady_tick(Duration::from_millis(100));

        Ok(Self {
            progress_bar: pb,
            estimator,
            total_bytes,
            transferred_bytes: AtomicU64::new(0),
            peak_rate: RwLock::new(0.0),
            start_time: Instant::now(),
        })
    }

    /// Add transferred bytes
    pub fn add_bytes(&self, bytes: u64) {
        let total = self.transferred_bytes.fetch_add(bytes, Ordering::Relaxed) + bytes;
        self.progress_bar.set_position(total);
        self.estimator.update(total);

        // Track peak rate
        let current_rate = self.estimator.rate();
        if let Ok(mut peak) = self.peak_rate.write() {
            if current_rate > *peak {
                *peak = current_rate;
            }
        }
    }

    /// Set absolute position
    pub fn set_position(&self, bytes: u64) {
        self.transferred_bytes.store(bytes, Ordering::Relaxed);
        self.progress_bar.set_position(bytes);
        self.estimator.update(bytes);
    }

    /// Get current transfer rate (bytes per second)
    #[must_use]
    pub fn rate(&self) -> f64 {
        self.estimator.rate()
    }

    /// Get peak transfer rate
    #[must_use]
    pub fn peak_rate(&self) -> f64 {
        self.peak_rate.read().map_or(0.0, |r| *r)
    }

    /// Get average transfer rate
    #[must_use]
    pub fn average_rate(&self) -> f64 {
        let elapsed = self.start_time.elapsed().as_secs_f64();
        if elapsed == 0.0 {
            return 0.0;
        }
        self.transferred_bytes.load(Ordering::Relaxed) as f64 / elapsed
    }

    /// Format rate as human-readable string
    #[must_use]
    pub fn format_rate(&self) -> String {
        format_bytes_per_sec(self.rate())
    }

    /// Get statistics summary
    #[must_use]
    pub fn statistics(&self) -> TransferStatistics {
        TransferStatistics {
            total_bytes: self.total_bytes,
            transferred_bytes: self.transferred_bytes.load(Ordering::Relaxed),
            elapsed: self.start_time.elapsed(),
            current_rate: self.rate(),
            peak_rate: self.peak_rate(),
            average_rate: self.average_rate(),
        }
    }

    /// Finish the transfer
    pub fn finish(&self) {
        let stats = self.statistics();
        self.progress_bar.finish_with_message(format!(
            "Completed - Avg: {}/s, Peak: {}/s",
            format_bytes(stats.average_rate as u64),
            format_bytes(stats.peak_rate as u64)
        ));
    }

    /// Finish with error
    pub fn finish_with_error(&self, error: &str) {
        self.progress_bar.abandon_with_message(format!("Error: {}", error));
    }
}

/// Transfer statistics
#[derive(Debug, Clone)]
pub struct TransferStatistics {
    /// Total bytes to transfer
    pub total_bytes: u64,
    /// Bytes transferred
    pub transferred_bytes: u64,
    /// Time elapsed
    pub elapsed: Duration,
    /// Current transfer rate
    pub current_rate: f64,
    /// Peak transfer rate
    pub peak_rate: f64,
    /// Average transfer rate
    pub average_rate: f64,
}

impl TransferStatistics {
    /// Get percentage complete
    #[must_use]
    pub fn percentage(&self) -> f64 {
        if self.total_bytes == 0 {
            return 100.0;
        }
        (self.transferred_bytes as f64 / self.total_bytes as f64) * 100.0
    }
}

/// Format bytes as human-readable string
fn format_bytes(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB", "PB"];
    let mut size = bytes as f64;
    let mut unit_idx = 0;

    while size >= 1024.0 && unit_idx < UNITS.len() - 1 {
        size /= 1024.0;
        unit_idx += 1;
    }

    if unit_idx == 0 {
        format!("{} {}", bytes, UNITS[unit_idx])
    } else {
        format!("{:.2} {}", size, UNITS[unit_idx])
    }
}

/// Format bytes per second as human-readable string
fn format_bytes_per_sec(bps: f64) -> String {
    format!("{}/s", format_bytes(bps as u64))
}

/// Multi-operation progress manager
#[derive(Debug)]
pub struct ProgressManager {
    /// Multi-progress container
    multi: MultiProgress,
    /// Configuration
    config: LogConfig,
    /// Active trackers count
    active_trackers: AtomicU64,
    /// Whether the manager is finished
    finished: AtomicBool,
    /// Log file writer (if enabled)
    log_writer: Option<Arc<Mutex<BufWriter<File>>>>,
}

impl ProgressManager {
    /// Create a new progress manager
    pub fn new(config: LogConfig) -> ProgressResult<Self> {
        let log_writer = if let Some(ref path) = config.log_file {
            let file = OpenOptions::new()
                .create(true)
                .append(true)
                .open(path)
                .map_err(ProgressError::LogFileCreation)?;
            Some(Arc::new(Mutex::new(BufWriter::new(file))))
        } else {
            None
        };

        let multi = MultiProgress::new();

        // Configure draw target based on colors/TTY
        if !config.colors_enabled || !Term::stdout().is_term() {
            multi.set_draw_target(ProgressDrawTarget::hidden());
        }

        Ok(Self {
            multi,
            config,
            active_trackers: AtomicU64::new(0),
            finished: AtomicBool::new(false),
            log_writer,
        })
    }

    /// Create a simple progress bar
    pub fn create_progress_bar(&self, total: u64, message: &str) -> ProgressResult<ProgressBar> {
        let style = ProgressStyle::default_bar()
            .template("{msg} [{bar:40.cyan/blue}] {pos}/{len} ({percent}%) | ETA: {eta}")
            .map_err(|e| ProgressError::StyleTemplate(e.to_string()))?
            .progress_chars("#>-");

        let pb = self.multi.add(ProgressBar::new(total));
        pb.set_style(style);
        pb.set_message(message.to_string());
        pb.enable_steady_tick(Duration::from_millis(100));

        self.active_trackers.fetch_add(1, Ordering::Relaxed);
        Ok(pb)
    }

    /// Create a spinner for indeterminate operations
    pub fn create_spinner(&self, message: &str) -> ProgressResult<ProgressBar> {
        let style = ProgressStyle::default_spinner()
            .template("{spinner:.green} {msg} [{elapsed_precise}]")
            .map_err(|e| ProgressError::StyleTemplate(e.to_string()))?
            .tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ ");

        let pb = self.multi.add(ProgressBar::new_spinner());
        pb.set_style(style);
        pb.set_message(message.to_string());
        pb.enable_steady_tick(Duration::from_millis(80));

        self.active_trackers.fetch_add(1, Ordering::Relaxed);
        Ok(pb)
    }

    /// Create a tile progress tracker
    pub fn create_tile_tracker(
        &self,
        tiles_x: u64,
        tiles_y: u64,
        message: &str,
    ) -> ProgressResult<TileProgressTracker> {
        self.active_trackers.fetch_add(1, Ordering::Relaxed);
        TileProgressTracker::new(&self.multi, tiles_x, tiles_y, message)
    }

    /// Create a byte transfer tracker
    pub fn create_transfer_tracker(
        &self,
        total_bytes: u64,
        label: &str,
    ) -> ProgressResult<ByteTransferTracker> {
        self.active_trackers.fetch_add(1, Ordering::Relaxed);
        ByteTransferTracker::new(&self.multi, total_bytes, label)
    }

    /// Log a message at the specified level
    pub fn log(&self, level: VerbosityLevel, message: &str) {
        if !self.config.verbosity.is_enabled(level) {
            return;
        }

        let styled_message = if self.config.colors_enabled {
            let style = match level {
                VerbosityLevel::Error => &self.config.theme.error,
                VerbosityLevel::Warn => &self.config.theme.warning,
                VerbosityLevel::Info => &self.config.theme.info,
                VerbosityLevel::Debug | VerbosityLevel::Trace => &self.config.theme.debug,
                VerbosityLevel::Quiet => return,
            };
            format!("{}", style.apply_to(message))
        } else {
            message.to_string()
        };

        // Print to console
        if !matches!(level, VerbosityLevel::Quiet) {
            self.multi.println(&styled_message).ok();
        }

        // Write to log file
        if let Some(ref writer) = self.log_writer {
            self.write_to_log(level, message);
        }
    }

    /// Write to log file
    fn write_to_log(&self, level: VerbosityLevel, message: &str) {
        if let Some(ref writer) = self.log_writer {
            if let Ok(mut guard) = writer.lock() {
                let timestamp = SystemTime::now()
                    .duration_since(SystemTime::UNIX_EPOCH)
                    .map(|d| d.as_secs())
                    .unwrap_or(0);

                let log_line = if self.config.json_output {
                    format!(
                        r#"{{"timestamp":{},"level":"{}","message":"{}"}}"#,
                        timestamp,
                        level,
                        message.replace('"', "\\\"")
                    )
                } else {
                    format!("[{}] [{}] {}", timestamp, level, message)
                };

                writeln!(guard, "{}", log_line).ok();
                guard.flush().ok();
            }
        }
    }

    /// Log an error message
    pub fn error(&self, message: &str) {
        self.log(VerbosityLevel::Error, message);
    }

    /// Log a warning message
    pub fn warn(&self, message: &str) {
        self.log(VerbosityLevel::Warn, message);
    }

    /// Log an info message
    pub fn info(&self, message: &str) {
        self.log(VerbosityLevel::Info, message);
    }

    /// Log a debug message
    pub fn debug(&self, message: &str) {
        self.log(VerbosityLevel::Debug, message);
    }

    /// Log a trace message
    pub fn trace(&self, message: &str) {
        self.log(VerbosityLevel::Trace, message);
    }

    /// Suspend progress bars for clean output
    pub fn suspend<F, R>(&self, f: F) -> R
    where
        F: FnOnce() -> R,
    {
        self.multi.suspend(f)
    }

    /// Get the multi-progress container
    #[must_use]
    pub fn multi(&self) -> &MultiProgress {
        &self.multi
    }

    /// Get current configuration
    #[must_use]
    pub fn config(&self) -> &LogConfig {
        &self.config
    }

    /// Clear all progress bars
    pub fn clear(&self) {
        self.multi.clear().ok();
    }

    /// Check if any trackers are active
    #[must_use]
    pub fn has_active_trackers(&self) -> bool {
        self.active_trackers.load(Ordering::Relaxed) > 0
    }

    /// Mark a tracker as complete (decrement counter)
    pub fn tracker_complete(&self) {
        self.active_trackers.fetch_sub(1, Ordering::Relaxed);
    }
}

/// Initialize the global logging system
pub fn init_logging(config: &LogConfig) -> ProgressResult<()> {
    let level = config.verbosity.to_tracing_level();

    let filter = EnvFilter::try_from_default_env()
        .unwrap_or_else(|_| EnvFilter::new(level.to_string()));

    let fmt_layer = tracing_fmt::layer()
        .with_ansi(config.colors_enabled)
        .with_target(config.show_targets)
        .with_thread_ids(config.show_thread_ids)
        .with_file(config.show_source_location)
        .with_line_number(config.show_source_location);

    if config.json_output {
        let json_layer = tracing_fmt::layer()
            .json()
            .with_target(config.show_targets)
            .with_thread_ids(config.show_thread_ids)
            .with_file(config.show_source_location)
            .with_line_number(config.show_source_location);

        tracing_subscriber::registry()
            .with(filter)
            .with(json_layer)
            .try_init()
            .map_err(|e| ProgressError::InvalidConfig(e.to_string()))?;
    } else {
        tracing_subscriber::registry()
            .with(filter)
            .with(fmt_layer)
            .try_init()
            .map_err(|e| ProgressError::InvalidConfig(e.to_string()))?;
    }

    Ok(())
}

/// Styled message builder for complex output
#[derive(Debug)]
pub struct StyledMessage {
    parts: Vec<String>,
    theme: ColorTheme,
}

impl StyledMessage {
    /// Create a new styled message builder
    #[must_use]
    pub fn new(theme: ColorTheme) -> Self {
        Self {
            parts: Vec::new(),
            theme,
        }
    }

    /// Add plain text
    #[must_use]
    pub fn text(mut self, text: &str) -> Self {
        self.parts.push(text.to_string());
        self
    }

    /// Add a label
    #[must_use]
    pub fn label(mut self, text: &str) -> Self {
        self.parts.push(format!("{}", self.theme.label.apply_to(text)));
        self
    }

    /// Add a value
    #[must_use]
    pub fn value(mut self, text: &str) -> Self {
        self.parts.push(format!("{}", self.theme.value.apply_to(text)));
        self
    }

    /// Add a path
    #[must_use]
    pub fn path(mut self, text: &str) -> Self {
        self.parts.push(format!("{}", self.theme.path.apply_to(text)));
        self
    }

    /// Add a metric
    #[must_use]
    pub fn metric(mut self, text: &str) -> Self {
        self.parts.push(format!("{}", self.theme.metric.apply_to(text)));
        self
    }

    /// Add success text
    #[must_use]
    pub fn success(mut self, text: &str) -> Self {
        self.parts.push(format!("{}", self.theme.success.apply_to(text)));
        self
    }

    /// Add warning text
    #[must_use]
    pub fn warning(mut self, text: &str) -> Self {
        self.parts.push(format!("{}", self.theme.warning.apply_to(text)));
        self
    }

    /// Add error text
    #[must_use]
    pub fn error(mut self, text: &str) -> Self {
        self.parts.push(format!("{}", self.theme.error.apply_to(text)));
        self
    }

    /// Build the final message
    #[must_use]
    pub fn build(self) -> String {
        self.parts.join("")
    }
}

impl fmt::Display for StyledMessage {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for part in &self.parts {
            write!(f, "{}", part)?;
        }
        Ok(())
    }
}

/// Progress bar presets for common operations
pub mod presets {
    use super::*;

    /// Create a raster processing progress bar
    pub fn raster_processing(multi: &MultiProgress, total: u64) -> ProgressResult<ProgressBar> {
        let style = ProgressStyle::default_bar()
            .template("{spinner:.green} {msg} [{bar:40.cyan/blue}] {pos}/{len} ({percent}%) | {per_sec} | ETA: {eta}")
            .map_err(|e| ProgressError::StyleTemplate(e.to_string()))?
            .progress_chars("=>-");

        let pb = multi.add(ProgressBar::new(total));
        pb.set_style(style);
        pb.enable_steady_tick(Duration::from_millis(100));
        Ok(pb)
    }

    /// Create a file I/O progress bar
    pub fn file_io(multi: &MultiProgress, total_bytes: u64) -> ProgressResult<ProgressBar> {
        let style = ProgressStyle::default_bar()
            .template("{msg} [{bar:40.green/dim}] {bytes}/{total_bytes} | {bytes_per_sec} | {eta}")
            .map_err(|e| ProgressError::StyleTemplate(e.to_string()))?
            .progress_chars("#>-");

        let pb = multi.add(ProgressBar::new(total_bytes));
        pb.set_style(style);
        pb.enable_steady_tick(Duration::from_millis(100));
        Ok(pb)
    }

    /// Create a validation progress bar
    pub fn validation(multi: &MultiProgress, total: u64) -> ProgressResult<ProgressBar> {
        let style = ProgressStyle::default_bar()
            .template("{spinner:.yellow} Validating... [{bar:40.yellow/dim}] {pos}/{len} checks")
            .map_err(|e| ProgressError::StyleTemplate(e.to_string()))?
            .progress_chars("*>-");

        let pb = multi.add(ProgressBar::new(total));
        pb.set_style(style);
        pb.enable_steady_tick(Duration::from_millis(80));
        Ok(pb)
    }

    /// Create a conversion progress bar
    pub fn conversion(multi: &MultiProgress, total: u64) -> ProgressResult<ProgressBar> {
        let style = ProgressStyle::default_bar()
            .template("{spinner:.magenta} Converting... [{bar:40.magenta/dim}] {pos}/{len} ({percent}%)")
            .map_err(|e| ProgressError::StyleTemplate(e.to_string()))?
            .progress_chars("#>-");

        let pb = multi.add(ProgressBar::new(total));
        pb.set_style(style);
        pb.enable_steady_tick(Duration::from_millis(100));
        Ok(pb)
    }

    /// Create a multi-band processing progress bar
    pub fn band_processing(multi: &MultiProgress, total_bands: u64) -> ProgressResult<ProgressBar> {
        let style = ProgressStyle::default_bar()
            .template("{msg} Band {pos}/{len} [{bar:30.cyan/blue}] {percent}%")
            .map_err(|e| ProgressError::StyleTemplate(e.to_string()))?
            .progress_chars("#>-");

        let pb = multi.add(ProgressBar::new(total_bands));
        pb.set_style(style);
        pb.enable_steady_tick(Duration::from_millis(100));
        Ok(pb)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::thread;

    #[test]
    fn test_verbosity_level_ordering() {
        assert!(VerbosityLevel::Quiet < VerbosityLevel::Error);
        assert!(VerbosityLevel::Error < VerbosityLevel::Warn);
        assert!(VerbosityLevel::Warn < VerbosityLevel::Info);
        assert!(VerbosityLevel::Info < VerbosityLevel::Debug);
        assert!(VerbosityLevel::Debug < VerbosityLevel::Trace);
    }

    #[test]
    fn test_verbosity_level_parsing() {
        assert_eq!(
            "info".parse::<VerbosityLevel>().ok(),
            Some(VerbosityLevel::Info)
        );
        assert_eq!(
            "debug".parse::<VerbosityLevel>().ok(),
            Some(VerbosityLevel::Debug)
        );
        assert_eq!(
            "quiet".parse::<VerbosityLevel>().ok(),
            Some(VerbosityLevel::Quiet)
        );
        assert!("invalid".parse::<VerbosityLevel>().is_err());
    }

    #[test]
    fn test_verbosity_is_enabled() {
        let level = VerbosityLevel::Info;
        assert!(level.is_enabled(VerbosityLevel::Error));
        assert!(level.is_enabled(VerbosityLevel::Warn));
        assert!(level.is_enabled(VerbosityLevel::Info));
        assert!(!level.is_enabled(VerbosityLevel::Debug));
        assert!(!level.is_enabled(VerbosityLevel::Trace));
    }

    #[test]
    fn test_log_config_builder() {
        let config = LogConfig::new()
            .with_verbosity(VerbosityLevel::Debug)
            .with_colors(true)
            .with_timestamps(true)
            .with_targets(true);

        assert_eq!(config.verbosity, VerbosityLevel::Debug);
        assert!(config.colors_enabled);
        assert!(config.show_timestamps);
        assert!(config.show_targets);
    }

    #[test]
    fn test_time_estimator() {
        let estimator = TimeEstimator::new(100);

        // Update progress
        estimator.update(25);
        thread::sleep(Duration::from_millis(10));
        estimator.update(50);

        assert!(estimator.elapsed() > Duration::ZERO);
        assert!(estimator.percentage() >= 50.0);
    }

    #[test]
    fn test_format_duration() {
        assert_eq!(format_duration(Duration::from_secs(30)), "30s");
        assert_eq!(format_duration(Duration::from_secs(90)), "1m 30s");
        assert_eq!(format_duration(Duration::from_secs(3661)), "1h 1m 1s");
    }

    #[test]
    fn test_format_bytes() {
        assert_eq!(format_bytes(512), "512 B");
        assert_eq!(format_bytes(1024), "1.00 KB");
        assert_eq!(format_bytes(1536), "1.50 KB");
        assert_eq!(format_bytes(1_048_576), "1.00 MB");
        assert_eq!(format_bytes(1_073_741_824), "1.00 GB");
    }

    #[test]
    fn test_color_themes() {
        let default = ColorTheme::default();
        let plain = ColorTheme::plain();
        let dark = ColorTheme::dark();
        let light = ColorTheme::light();

        // Just ensure they can be created without panicking
        let _ = default.success.apply_to("test");
        let _ = plain.success.apply_to("test");
        let _ = dark.success.apply_to("test");
        let _ = light.success.apply_to("test");
    }

    #[test]
    fn test_styled_message_builder() {
        let theme = ColorTheme::default();
        let msg = StyledMessage::new(theme)
            .label("File: ")
            .path("/path/to/file.tif")
            .text(" - ")
            .metric("1.5 MB")
            .build();

        assert!(!msg.is_empty());
    }

    #[test]
    fn test_progress_manager_creation() {
        let config = LogConfig::new().with_colors(false);
        let manager = ProgressManager::new(config);
        assert!(manager.is_ok());
    }

    #[test]
    fn test_progress_manager_spinner() {
        let config = LogConfig::new().with_colors(false);
        let manager = ProgressManager::new(config).expect("Failed to create manager");

        let spinner = manager.create_spinner("Processing...");
        assert!(spinner.is_ok());

        if let Ok(pb) = spinner {
            pb.finish();
        }
    }

    #[test]
    fn test_transfer_statistics() {
        let stats = TransferStatistics {
            total_bytes: 1000,
            transferred_bytes: 500,
            elapsed: Duration::from_secs(5),
            current_rate: 100.0,
            peak_rate: 150.0,
            average_rate: 100.0,
        };

        assert!((stats.percentage() - 50.0).abs() < 0.001);
    }

    #[test]
    fn test_presets_creation() {
        let multi = MultiProgress::new();
        multi.set_draw_target(ProgressDrawTarget::hidden());

        let raster = presets::raster_processing(&multi, 100);
        assert!(raster.is_ok());

        let file_io = presets::file_io(&multi, 1024);
        assert!(file_io.is_ok());

        let validation = presets::validation(&multi, 10);
        assert!(validation.is_ok());

        let conversion = presets::conversion(&multi, 50);
        assert!(conversion.is_ok());

        let band = presets::band_processing(&multi, 4);
        assert!(band.is_ok());
    }
}