robosync 1.0.10

High-performance file synchronization with intelligent concurrent processing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
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
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
//! Mixed-mode strategy that uses different copy methods for different file types
//!
//! This module implements an intelligent mixed strategy that:
//! - Uses parallel batch operations for small files
//! - Uses delta transfer for large modified files
//! - Uses platform APIs for medium files
//! - Can batch small files to native tools for efficiency

use anyhow::Result;
use rayon::prelude::*;
use std::io::Write;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex};

use crate::error::RoboSyncError;
use crate::error_logger::ErrorLogger;
use crate::file_list::FileOperation;
use crate::formatted_display::{self, WorkerStats};
use crate::logging::SyncLogger;
use crate::operation_utils::{
    prepare_destination_path, update_progress, update_progress_with_file,
};
use crate::options::SyncOptions;
use crate::platform_api::PlatformCopier;
use crate::progress::SyncProgress;
use crate::sync_stats::SyncStats;

/// Default size thresholds for categorizing files - optimized for performance
const DEFAULT_SMALL_FILE_THRESHOLD: u64 = 256 * 1024; // 256KB - increased for better batching
const DEFAULT_MEDIUM_FILE_THRESHOLD: u64 = 10 * 1024 * 1024; // 10MB
const DEFAULT_LARGE_FILE_THRESHOLD: u64 = 100 * 1024 * 1024; // 100MB - files above this use delta

/// Delta transfer is most effective for very large files
const DELTA_BLOCK_SIZE: usize = 64 * 1024; // 64KB blocks for large files

/// Batch size for small file operations - larger for better performance
const SMALL_FILE_BATCH_SIZE: usize = 5000;

/// Number of parallel threads for small files
const SMALL_FILE_THREADS: usize = 32;

/// Format bytes into human readable string
fn humanize_bytes(bytes: u64) -> String {
    const UNITS: &[&str] = &["B", "KB", "MB", "GB", "TB"];
    if bytes == 0 {
        return "0 B".to_string();
    }
    let exponent = (bytes as f64).log(1024.0).floor() as usize;
    let exponent = exponent.min(UNITS.len() - 1);
    let value = bytes as f64 / 1024_f64.powi(exponent as i32);
    if exponent == 0 {
        format!("{} {}", bytes, UNITS[exponent])
    } else {
        format!("{:.1} {}", value, UNITS[exponent])
    }
}

/// Format number with thousands separators
fn format_number(n: u64) -> String {
    let s = n.to_string();
    let chars: Vec<char> = s.chars().collect();
    let mut result = String::new();

    for (i, &ch) in chars.iter().rev().enumerate() {
        if i > 0 && i % 3 == 0 {
            result.push(',');
        }
        result.push(ch);
    }

    result.chars().rev().collect()
}

/// Mixed strategy executor
#[derive(Clone)]
pub struct MixedStrategyExecutor {
    progress: Arc<SyncProgress>,
    logger: Option<Arc<Mutex<SyncLogger>>>,
}

impl MixedStrategyExecutor {
    pub fn new(total_files: u64, total_bytes: u64) -> Self {
        Self {
            progress: Arc::new(SyncProgress::new_silent(total_files, total_bytes)),
            logger: None,
        }
    }

    pub fn new_with_no_progress() -> Self {
        Self {
            progress: Arc::new(SyncProgress::new_noop()),
            logger: None,
        }
    }

    pub fn with_logger(mut self, logger: Arc<Mutex<SyncLogger>>) -> Self {
        self.logger = Some(logger);
        self
    }

    /// Execute mixed strategy synchronization
    pub fn execute(
        &self,
        operations: Vec<FileOperation>,
        source_root: &Path,
        dest_root: &Path,
        options: &SyncOptions,
    ) -> Result<SyncStats> {
        use indicatif::{ProgressBar, ProgressStyle};
        use std::collections::HashMap;
        use std::sync::mpsc;
        use std::thread;

        // Create logger with optional log file
        let logger = Arc::new(Mutex::new(SyncLogger::new(
            options.log_file.as_deref(),
            options.show_eta,
        )?));

        {
            let logger_guard = logger.lock().unwrap();
            logger_guard.log("Starting mixed strategy synchronization...");
            logger_guard.log(&format!("Source: {}", source_root.display()));
            logger_guard.log(&format!("Destination: {}", dest_root.display()));
        }

        // Update self with the logger for worker threads
        let executor_with_logger = self.clone().with_logger(Arc::clone(&logger));

        // Create error logger for automatic error reporting
        let error_logger = ErrorLogger::new(options.clone(), source_root, dest_root);
        let _error_handle = error_logger.get_handle();

        // Show spinner during categorization for user feedback
        let spinner = if options.show_progress {
            let s = ProgressBar::new_spinner();
            s.set_style(
                ProgressStyle::default_spinner()
                    .template("{spinner} {msg}")
                    .expect("Failed to set spinner template"),
            );
            s.set_message("Analyzing files for optimal strategy...");
            s.enable_steady_tick(std::time::Duration::from_millis(100));
            Some(s)
        } else {
            None
        };

        // Categorize files by size and type
        let categorized = self.categorize_operations(operations, options);
        let operation_count = categorized.total_operations();

        if let Some(spinner) = spinner {
            spinner.finish_and_clear();
        }

        if operation_count == 0 {
            if options.show_progress {
                println!("\n  ℹ️  No operations to perform - all files are up to date!");
            }
            return Ok(SyncStats::default());
        }

        // Calculate file and size statistics
        let (_file_stats, _size_stats) = self.calculate_stats(&categorized, source_root);

        // Strategy already printed by parent
        // File analysis already printed by parent

        // Print pending operations
        if options.show_progress {
            if options.verbose >= 1 {
                // Verbose mode: show detailed breakdown
                let detailed_stats = executor_with_logger
                    .calculate_detailed_pending_stats(&categorized, source_root);
                formatted_display::print_pending_operations_detailed(
                    &detailed_stats,
                    options.verbose,
                );
            } else {
                // Normal mode: show simple summary
                let pending_stats =
                    executor_with_logger.calculate_pending_stats(&categorized, source_root);
                formatted_display::print_pending_operations(
                    pending_stats.files_create,
                    pending_stats.files_update,
                    pending_stats.files_delete,
                    0, // files_skip - we don't skip in this implementation
                    pending_stats.dirs_create,
                    0, // dirs_update
                    0, // dirs_delete
                    0, // dirs_skip
                    pending_stats.size_create,
                    pending_stats.size_update,
                    pending_stats.size_delete,
                    0, // size_skip
                );
            }
        }

        // Process directories first (must be done before files)
        executor_with_logger.create_directories(
            &categorized.directories,
            source_root,
            dest_root,
        )?;

        // Clear any previous progress output before starting main progress bar
        if options.show_progress {
            println!(); // Add blank line for separation
        }

        // Create progress bar - always track progress internally
        let pb = if !options.show_progress {
            // Create a hidden progress bar that still tracks position
            let pb = indicatif::ProgressBar::new(operation_count);
            pb.set_draw_target(indicatif::ProgressDrawTarget::hidden());
            pb
        } else {
            let pb = formatted_display::create_progress_bar(operation_count);
            pb.set_message("Starting...");
            pb
        };

        // Setup for parallel execution
        let (tx, rx) = mpsc::channel();
        let mut handles = vec![];
        let mut worker_info = HashMap::new();
        let start_time = std::time::Instant::now();

        // Process small files in parallel
        if !categorized.small_files.is_empty() {
            let tx = tx.clone();
            let small_files = categorized.small_files.clone();
            let source_root = source_root.to_path_buf();
            let dest_root = dest_root.to_path_buf();
            let options = options.clone();
            let executor = executor_with_logger.clone();
            let pb_clone = pb.clone();
            let worker_start = std::time::Instant::now();

            worker_info.insert(
                "small",
                (
                    "Small".to_string(),
                    categorized.small_files.len() as u64,
                    worker_start,
                ),
            );

            let st = start_time;
            let handle = thread::spawn(move || {
                let stats = executor.process_small_files_batch(
                    &small_files,
                    &source_root,
                    &dest_root,
                    &options,
                    Some(&pb_clone),
                    st,
                );
                let _ = tx.send(("small", stats, worker_start.elapsed()));
            });
            handles.push(handle);
        }

        // Process medium files in parallel
        if !categorized.medium_files.is_empty() {
            let tx = tx.clone();
            let medium_files = categorized.medium_files.clone();
            let source_root = source_root.to_path_buf();
            let dest_root = dest_root.to_path_buf();
            let options = options.clone();
            let executor = executor_with_logger.clone();
            let pb_clone = pb.clone();
            let worker_start = std::time::Instant::now();

            worker_info.insert(
                "medium",
                (
                    "Medium".to_string(),
                    categorized.medium_files.len() as u64,
                    worker_start,
                ),
            );

            let st = start_time;
            let handle = thread::spawn(move || {
                let stats = executor.process_medium_files(
                    &medium_files,
                    &source_root,
                    &dest_root,
                    &options,
                    Some(&pb_clone),
                    st,
                );
                let _ = tx.send(("medium", stats, worker_start.elapsed()));
            });
            handles.push(handle);
        }

        // Process large files in parallel
        if !categorized.large_files.is_empty() {
            let tx = tx.clone();
            let large_files = categorized.large_files.clone();
            let source_root = source_root.to_path_buf();
            let dest_root = dest_root.to_path_buf();
            let options = options.clone();
            let executor = executor_with_logger.clone();
            let pb_clone = pb.clone();
            let worker_start = std::time::Instant::now();

            worker_info.insert(
                "large",
                (
                    "Large".to_string(),
                    categorized.large_files.len() as u64,
                    worker_start,
                ),
            );

            let st = start_time;
            let handle = thread::spawn(move || {
                let result = std::panic::catch_unwind(|| {
                    executor.process_large_files(
                        &large_files,
                        &source_root,
                        &dest_root,
                        &options,
                        Some(&pb_clone),
                        st,
                    )
                });

                let stats = match result {
                    Ok(stats_result) => stats_result,
                    Err(panic) => {
                        eprintln!("[ERROR] Large worker thread panicked: {:?}", panic);
                        Err(anyhow::anyhow!("Large worker thread panicked"))
                    }
                };

                if let Err(e) = tx.send(("large", stats, worker_start.elapsed())) {
                    eprintln!("[ERROR] Failed to send large worker results: {:?}", e);
                }
            });
            handles.push(handle);
        }

        // Process delta files (very large files)
        if !categorized.delta_files.is_empty() {
            let tx = tx.clone();
            let delta_files = categorized.delta_files.clone();
            let source_root = source_root.to_path_buf();
            let dest_root = dest_root.to_path_buf();
            let options = options.clone();
            let executor = executor_with_logger.clone();
            let pb_clone = pb.clone();
            let worker_start = std::time::Instant::now();

            worker_info.insert(
                "delta",
                (
                    "Delta transfer".to_string(),
                    categorized.delta_files.len() as u64,
                    worker_start,
                ),
            );

            let st = start_time;
            let handle = thread::spawn(move || {
                let stats = executor.process_delta_files(
                    &delta_files,
                    &source_root,
                    &dest_root,
                    &options,
                    Some(&pb_clone),
                    st,
                );
                let _ = tx.send(("delta", stats, worker_start.elapsed()));
            });
            handles.push(handle);
        }

        // Process deletes in parallel (after all creates/updates start)
        if !categorized.deletes.is_empty() {
            let tx = tx.clone();
            let deletes = categorized.deletes.clone();
            let options = options.clone();
            let executor = executor_with_logger.clone();
            let pb_clone = pb.clone();
            let worker_start = std::time::Instant::now();

            worker_info.insert(
                "delete",
                (
                    "Delete operations".to_string(),
                    categorized.deletes.len() as u64,
                    worker_start,
                ),
            );

            let st = start_time;
            let handle = thread::spawn(move || {
                let result = std::panic::catch_unwind(|| {
                    executor.process_deletes(&deletes, &options, Some(&pb_clone), st)
                });

                let stats = match result {
                    Ok(stats_result) => stats_result,
                    Err(panic) => {
                        eprintln!("[ERROR] Delete worker thread panicked: {:?}", panic);
                        Err(anyhow::anyhow!("Delete worker thread panicked"))
                    }
                };

                if let Err(e) = tx.send(("delete", stats, worker_start.elapsed())) {
                    eprintln!("[ERROR] Failed to send delete worker results: {:?}", e);
                }
            });
            handles.push(handle);
        }

        // Start a status logger thread that shows single-line status updates
        let logger_handle = if !options.show_progress {
            Some({
                let pb_status = pb.clone();
                let total_ops = operation_count;
                let start_time = std::time::Instant::now();
                let progress_tracker = Arc::clone(&self.progress);
                thread::spawn(move || {
                    let mut last_position = 0u64;
                    let mut stall_count = 0;
                    let mut last_rate = 0.0;
                    let spinner_chars = vec!['', '', '', '', '', '', '', '', '', ''];
                    let mut spinner_idx = 0;

                    loop {
                        thread::sleep(std::time::Duration::from_millis(100));
                        let current_position = pb_status.position();
                        let elapsed = start_time.elapsed();

                        // Calculate rate
                        let elapsed_secs = elapsed.as_secs_f64();
                        let rate = if elapsed_secs > 0.0 && current_position > 0 {
                            current_position as f64 / elapsed_secs
                        } else {
                            0.0
                        };

                        // Calculate throughput
                        let bytes_transferred = progress_tracker.get_bytes_transferred();
                        let throughput = if elapsed_secs > 0.0 {
                            (bytes_transferred as f64 / elapsed_secs) as u64
                        } else {
                            0
                        };

                        // Check for stalls
                        if current_position == last_position {
                            stall_count += 1;
                        } else {
                            stall_count = 0;
                            last_rate = rate;
                        }

                        // Update spinner
                        spinner_idx = (spinner_idx + 1) % spinner_chars.len();

                        // Print status line
                        let status = if stall_count > 50 {
                            // 5 seconds
                            format!(
                                " {} Syncing: {}/{} files | {}/s | ⚠️  Stalled for {}s",
                                spinner_chars[spinner_idx],
                                current_position,
                                total_ops,
                                indicatif::HumanBytes(throughput),
                                stall_count / 10
                            )
                        } else {
                            format!(
                                " {} Syncing: {}/{} files | {}/s",
                                spinner_chars[spinner_idx], 
                                current_position, 
                                total_ops, 
                                indicatif::HumanBytes(throughput)
                            )
                        };

                        // Clear line and print status
                        print!("\r{:80}\r{}", "", status);
                        let _ = std::io::stdout().flush();

                        last_position = current_position;

                        // Check if we're done
                        if current_position >= total_ops || pb_status.is_finished() {
                            print!("\r{:80}\r", ""); // Clear the line
                            let _ = std::io::stdout().flush();
                            break;
                        }
                    }
                })
            })
        } else {
            None
        };

        // Drop the original sender so rx.recv() will end when all threads finish
        drop(tx);

        // Collect results from all threads
        let mut total_stats = SyncStats::default();
        let mut worker_stats = Vec::new();

        for (worker_type, result, duration) in rx {
            match result {
                Ok(stats) => {
                    if let Some((name, _count, _)) = worker_info.get(worker_type) {
                        let bytes = stats.bytes_transferred();
                        let throughput = if duration.as_secs() > 0 {
                            bytes / duration.as_secs()
                        } else {
                            bytes
                        };

                        worker_stats.push(WorkerStats {
                            name: name.clone(),
                            files: if worker_type == "delete" {
                                stats.files_deleted()
                            } else {
                                stats.files_copied()
                            },
                            bytes,
                            duration_secs: duration.as_secs_f32(),
                            throughput,
                        });
                    }
                    total_stats = self.merge_stats(total_stats, stats);
                }
                Err(e) => {
                    // Don't print to stderr - it breaks the progress bar
                    // Record structured error for log file
                    let robosync_err = RoboSyncError::operation_failed(
                        format!("{}_worker", worker_type),
                        e.to_string(),
                    );
                    total_stats.add_structured_error(robosync_err, "worker_thread");
                }
            }
        }

        // Wait for all threads to complete
        for handle in handles {
            let _ = handle.join();
        }

        // Finish progress bar (only if progress is enabled)
        if options.show_progress {
            pb.finish_and_clear();
        } else {
            // Even for hidden progress bars, we need to finish them
            pb.finish();
        }

        // Wait for logger thread to finish
        if let Some(handle) = logger_handle {
            let _ = handle.join();
        }

        // Print worker performance (if progress enabled)
        if options.show_progress {
            formatted_display::print_worker_performance(worker_stats);
        }

        // Finish the internal progress tracker to prevent any final output
        self.progress.finish();

        // Final summary
        let elapsed = start_time.elapsed();
        let throughput = if elapsed.as_secs() > 0 {
            total_stats.bytes_transferred() / elapsed.as_secs()
        } else {
            total_stats.bytes_transferred()
        };

        // Always print summary statistics
        if options.show_progress {
            // With progress bar, show fancy completion message
            if total_stats.files_deleted() > 0 {
                println!(
                    "\n     ✅ Completed in {:.1}s: {} files copied, {} deleted, {} transferred ({}/s)",
                    elapsed.as_secs_f32(),
                    format_number(total_stats.files_copied()),
                    format_number(total_stats.files_deleted()),
                    humanize_bytes(total_stats.bytes_transferred()),
                    humanize_bytes(throughput)
                );
            } else {
                println!(
                    "\n     ✅ Completed in {:.1}s: {} files copied, {} transferred ({}/s)",
                    elapsed.as_secs_f32(),
                    format_number(total_stats.files_copied()),
                    humanize_bytes(total_stats.bytes_transferred()),
                    humanize_bytes(throughput)
                );
            }
        }

        // Log final statistics to file only (not console)
        {
            let logger = logger.lock().unwrap();
            logger.log_to_file_only(&format!("Files copied: {}", total_stats.files_copied()));
            logger.log_to_file_only(&format!("Files deleted: {}", total_stats.files_deleted()));
            logger.log_to_file_only(&format!(
                "Bytes transferred: {}",
                total_stats.bytes_transferred()
            ));
            logger.log_to_file_only(&format!("Errors: {}", total_stats.errors()));

            // Log error details
            let error_details = total_stats.get_error_details();
            logger.log_to_file_only(&format!("Error details count: {}", error_details.len()));
            for error_detail in error_details {
                logger.log_to_file_only(&format!(
                    "ERROR: {} - {} - {}",
                    error_detail.path.display(),
                    error_detail.operation,
                    error_detail.message
                ));
            }
        }

        // Finalize error reporting before printing summary
        let error_report_path =
            if let Ok(report_path) = error_logger.finalize_with_stats(&total_stats) {
                report_path
            } else {
                None
            };

        // Get metadata warning count
        let warning_count = crate::metadata::get_and_reset_metadata_warning_count();

        // Print error/warning summary if there were any
        if total_stats.errors() > 0 || warning_count > 0 {
            eprintln!();
            if total_stats.errors() > 0 {
                eprintln!(
                    "⚠️  {} errors occurred during synchronization",
                    total_stats.errors()
                );
                if let Some(ref path) = error_report_path {
                    eprintln!("📄 Error details saved to: {}", path.display());
                }
            }
            if warning_count > 0 {
                eprintln!(
                    "⚠️  {warning_count} metadata warnings (permissions/ownership/timestamps)"
                );
                eprintln!("These are non-fatal - files were copied successfully.");
                if warning_count > 10 {
                    eprintln!("Consider using --copy DAT to skip metadata preservation.");
                }
            }
        }

        // Close the logger
        logger.lock().unwrap().close();

        Ok(total_stats)
    }

    /// Categorize operations by file size and type
    fn categorize_operations(
        &self,
        operations: Vec<FileOperation>,
        options: &SyncOptions,
    ) -> CategorizedOps {
        let mut categorized = CategorizedOps::default();

        // Use configured thresholds or defaults
        let small_threshold = options
            .small_file_threshold
            .unwrap_or(DEFAULT_SMALL_FILE_THRESHOLD);
        let medium_threshold = options
            .medium_file_threshold
            .unwrap_or(DEFAULT_MEDIUM_FILE_THRESHOLD);
        let large_threshold = options
            .large_file_threshold
            .unwrap_or(DEFAULT_LARGE_FILE_THRESHOLD);

        for op in operations {
            match &op {
                FileOperation::Create { path } | FileOperation::Update { path, .. } => {
                    // Get file info from the operation
                    match std::fs::metadata(path) {
                        Ok(metadata) => {
                            let size = metadata.len();

                            if metadata.is_dir() {
                                categorized.directories.push(op);
                            } else if size <= small_threshold {
                                categorized.small_files.push(op);
                            } else if size <= medium_threshold {
                                categorized.medium_files.push(op);
                            } else if size <= large_threshold {
                                categorized.large_files.push(op);
                            } else {
                                // Very large files (>100MB) benefit from delta transfer
                                categorized.delta_files.push(op);
                            }
                        }
                        Err(_) => {
                            // If we can't read metadata, treat as small file
                            // The actual error will be captured when we try to copy it
                            categorized.small_files.push(op);
                        }
                    }
                }
                FileOperation::CreateDirectory { .. } => {
                    categorized.directories.push(op);
                }
                FileOperation::Delete { .. } => {
                    categorized.deletes.push(op);
                }
                FileOperation::CreateSymlink { .. } | FileOperation::UpdateSymlink { .. } => {
                    // Symlinks are small operations, add to small files
                    categorized.small_files.push(op);
                }
            }
        }

        categorized
    }

    /// Create directories
    fn create_directories(
        &self,
        dirs: &[FileOperation],
        source_root: &Path,
        dest_root: &Path,
    ) -> Result<()> {
        for op in dirs {
            if let FileOperation::CreateDirectory { path } = op {
                let relative = path.strip_prefix(source_root).unwrap_or(path);
                let dest = dest_root.join(relative);
                std::fs::create_dir_all(&dest)?;
            }
        }
        Ok(())
    }

    /// Process small files using parallel batch operations
    fn process_small_files_batch(
        &self,
        files: &[FileOperation],
        source_root: &Path,
        dest_root: &Path,
        options: &SyncOptions,
        progress_bar: Option<&indicatif::ProgressBar>,
        start_time: std::time::Instant,
    ) -> Result<SyncStats> {
        use crate::metadata::{copy_file_with_metadata, CopyFlags};

        let mut stats = SyncStats::default();
        let copy_flags = CopyFlags::from_string(&options.copy_flags);

        // Configure thread pool for optimal performance
        let pool = rayon::ThreadPoolBuilder::new()
            .num_threads(SMALL_FILE_THREADS)
            .build()
            .unwrap_or_else(|_| {
                // Fall back to global thread pool if custom build fails
                rayon::ThreadPoolBuilder::new()
                    .build()
                    .expect("Failed to create thread pool")
            });

        // Process in parallel using rayon with optimized settings
        let active_files = std::sync::Arc::new(std::sync::Mutex::new(std::collections::HashMap::<
            std::path::PathBuf,
            std::time::Instant,
        >::new()));
        let active_files_clone = active_files.clone();

        // Spawn a monitoring thread to detect stalled files
        let monitor_handle = std::thread::spawn(move || {
            loop {
                std::thread::sleep(std::time::Duration::from_secs(5));
                let active = active_files_clone.lock().unwrap();
                for (path, start_time) in active.iter() {
                    let elapsed = start_time.elapsed();
                    if elapsed > std::time::Duration::from_secs(10) {
                        eprintln!(
                            "[WARNING] File has been processing for {}s: {}",
                            elapsed.as_secs(),
                            path.display()
                        );
                    }
                }
                // Also report if we have active files
                if !active.is_empty() {}
                if active.is_empty() {
                    break;
                }
            }
        });

        let chunk_stats: Vec<_> = pool.install(|| {
            files
                .par_chunks(SMALL_FILE_BATCH_SIZE)
                .map(|chunk| {
                    let chunk_stats = SyncStats::default();

                    for op in chunk {
                        // Track active file
                        let file_path = match &op {
                            FileOperation::Create { path } | FileOperation::Update { path, .. } => {
                                Some(path.clone())
                            }
                            FileOperation::CreateSymlink { path, .. }
                            | FileOperation::UpdateSymlink { path, .. } => Some(path.clone()),
                            _ => None,
                        };

                        if let Some(path) = &file_path {
                            active_files
                                .lock()
                                .unwrap()
                                .insert(path.clone(), std::time::Instant::now());
                        }

                        match op {
                            FileOperation::Create { path } | FileOperation::Update { path, .. } => {
                                // Use utility function for path resolution and parent directory creation
                                let dest =
                                    match prepare_destination_path(path, source_root, dest_root) {
                                        Ok(dest) => dest,
                                        Err(e) => {
                                            let robosync_err = RoboSyncError::operation_failed(
                                                "prepare_path",
                                                e.to_string(),
                                            );
                                            chunk_stats.add_structured_error(
                                                robosync_err,
                                                "prepare_destination",
                                            );
                                            continue;
                                        }
                                    };

                                // Copy the file
                                match copy_file_with_metadata(path, &dest, &copy_flags) {
                                    Ok(bytes) => {
                                        chunk_stats.add_bytes_transferred(bytes);
                                        chunk_stats.increment_files_copied();

                                        // Use utility function for progress tracking with file info
                                        update_progress_with_file(
                                            &self.progress,
                                            progress_bar,
                                            bytes,
                                            start_time,
                                            path,
                                        );

                                        // Show on console only with -vv mode
                                        if options.verbose >= 2 {
                                            eprintln!(
                                                "  ✓ Copied: {}{} ({} bytes)",
                                                path.display(),
                                                dest.display(),
                                                bytes
                                            );
                                        }

                                        // Log to file with -v or higher
                                        if options.verbose >= 1 {
                                            if let Some(ref logger) = self.logger {
                                                if let Ok(logger) = logger.lock() {
                                                    logger.log_file_operation(
                                                        "Copied",
                                                        &format!(
                                                            "{}{} ({} bytes)",
                                                            path.display(),
                                                            dest.display(),
                                                            bytes
                                                        ),
                                                    );
                                                }
                                            }
                                        }
                                    }
                                    Err(e) => {
                                        let error_str = e.to_string();

                                        // Log error to file
                                        if let Some(ref logger) = self.logger {
                                            if let Ok(logger) = logger.lock() {
                                                logger.log_error(&format!(
                                                    "Failed to copy {}{}: {}",
                                                    path.display(),
                                                    dest.display(),
                                                    error_str
                                                ));
                                            }
                                        }

                                        // Convert anyhow::Error to RoboSyncError
                                        let robosync_err = match e.downcast::<std::io::Error>() {
                                            Ok(io_err) => {
                                                RoboSyncError::io_error(io_err, Some(path.clone()))
                                            }
                                            Err(e) => RoboSyncError::sync_failed(
                                                e.to_string(),
                                                Some(path.clone()),
                                                Some(dest.clone()),
                                            ),
                                        };
                                        chunk_stats.add_structured_error(robosync_err, "copy_file");

                                        // Still increment progress bar even for failed files
                                        if let Some(pb) = progress_bar {
                                            pb.inc(1);
                                        }
                                    }
                                }
                            }
                            FileOperation::CreateSymlink { path, target }
                            | FileOperation::UpdateSymlink { path, target } => {
                                // Use utility function for path resolution and parent directory creation
                                let dest =
                                    match prepare_destination_path(path, source_root, dest_root) {
                                        Ok(dest) => dest,
                                        Err(e) => {
                                            let robosync_err = RoboSyncError::operation_failed(
                                                "prepare_path",
                                                e.to_string(),
                                            );
                                            chunk_stats.add_structured_error(
                                                robosync_err,
                                                "prepare_destination",
                                            );
                                            continue;
                                        }
                                    };

                                // Remove existing symlink if updating
                                if matches!(op, FileOperation::UpdateSymlink { .. }) {
                                    let _ = std::fs::remove_file(&dest);
                                }

                                // Create symlink
                                #[cfg(unix)]
                                match std::os::unix::fs::symlink(target, &dest) {
                                    Ok(_) => {
                                        chunk_stats.increment_files_copied();
                                        // Use utility function for progress tracking (symlinks have 0 bytes)
                                        update_progress(
                                            &self.progress,
                                            progress_bar,
                                            0,
                                            start_time,
                                        );
                                    }
                                    Err(e) => {
                                        // Log error to file
                                        if let Some(ref logger) = self.logger {
                                            if let Ok(logger) = logger.lock() {
                                                logger.log_error(&format!(
                                                    "Failed to create symlink {}{}: {}",
                                                    dest.display(),
                                                    target.display(),
                                                    e
                                                ));
                                            }
                                        }

                                        // Convert to RoboSyncError
                                        let robosync_err =
                                            RoboSyncError::io_error(e, Some(dest.clone()));
                                        chunk_stats
                                            .add_structured_error(robosync_err, "create_symlink");
                                    }
                                }

                                #[cfg(windows)]
                                match crate::windows_symlinks::create_symlink(&dest, target) {
                                    Ok(_) => {
                                        chunk_stats.increment_files_copied();
                                        // Use utility function for progress tracking (symlinks have 0 bytes)
                                        update_progress(
                                            &self.progress,
                                            progress_bar,
                                            0,
                                            start_time,
                                        );
                                    }
                                    Err(e) => {
                                        // Log error to file
                                        if let Some(ref logger) = self.logger {
                                            if let Ok(logger) = logger.lock() {
                                                logger.log_error(&format!(
                                                    "Failed to create symlink {}{}: {}",
                                                    dest.display(),
                                                    target.display(),
                                                    e
                                                ));
                                            }
                                        }

                                        // Convert to RoboSyncError
                                        let robosync_err = RoboSyncError::operation_failed(
                                            "create_symlink",
                                            e.to_string(),
                                        );
                                        chunk_stats
                                            .add_structured_error(robosync_err, "create_symlink");
                                    }
                                }
                            }
                            _ => {}
                        }

                        // Remove from active tracking
                        if let Some(path) = file_path {
                            active_files.lock().unwrap().remove(&path);
                        }
                    }

                    chunk_stats
                })
                .collect()
        });

        // Merge all chunk statistics
        for chunk_stat in chunk_stats {
            stats = self.merge_stats(stats, chunk_stat);
        }

        // Clean up monitor thread
        drop(active_files);
        let _ = monitor_handle.join();

        Ok(stats)
    }

    /// Process medium files using platform APIs
    fn process_medium_files(
        &self,
        files: &[FileOperation],
        source_root: &Path,
        dest_root: &Path,
        _options: &SyncOptions,
        progress_bar: Option<&indicatif::ProgressBar>,
        _start_time: std::time::Instant,
    ) -> Result<SyncStats> {
        let copier = PlatformCopier::new();

        // Convert operations to file pairs
        let file_pairs: Vec<(PathBuf, PathBuf)> = files
            .iter()
            .filter_map(|op| match op {
                FileOperation::Create { path } | FileOperation::Update { path, .. } => {
                    // Use utility function for path resolution
                    let dest = crate::operation_utils::resolve_destination_path(
                        path,
                        source_root,
                        dest_root,
                    );
                    Some((path.clone(), dest))
                }
                _ => None,
            })
            .collect();

        // Copy files and update progress
        let stats = copier.copy_files(&file_pairs)?;

        // Update progress for all files attempted (not just successful copies)
        let total_files = file_pairs.len() as u64;
        for _ in 0..total_files {
            if let Some(pb) = progress_bar {
                pb.inc(1);
            }
        }

        // Update internal progress tracking
        for _ in 0..stats.files_copied() {
            self.progress.add_file();
        }
        self.progress.add_bytes(stats.bytes_transferred());

        Ok(stats)
    }

    /// Process large files with appropriate strategy
    fn process_large_files(
        &self,
        files: &[FileOperation],
        source_root: &Path,
        dest_root: &Path,
        options: &SyncOptions,
        progress_bar: Option<&indicatif::ProgressBar>,
        start_time: std::time::Instant,
    ) -> Result<SyncStats> {
        let stats = SyncStats::default();

        for op in files {
            match op {
                FileOperation::Update { path, .. } if options.checksum => {
                    // Use delta transfer for large file updates
                    let dest = crate::operation_utils::resolve_destination_path(
                        path,
                        source_root,
                        dest_root,
                    );

                    match self.delta_copy_file(path, &dest, options) {
                        Ok(bytes) => {
                            stats.add_bytes_transferred(bytes);
                            stats.increment_files_copied();

                            // Show on console only with -vv mode
                            if options.verbose >= 2 {
                                eprintln!(
                                    "  ✓ Delta transfer complete: {}{} ({} transferred)",
                                    path.display(),
                                    dest.display(),
                                    humanize_bytes(bytes)
                                );
                            }

                            // Log to file with -v or higher
                            if options.verbose >= 1 {
                                if let Some(ref logger) = self.logger {
                                    if let Ok(logger) = logger.lock() {
                                        logger.log_file_operation(
                                            "Delta transfer complete",
                                            &format!(
                                                "{}{} ({} bytes)",
                                                path.display(),
                                                dest.display(),
                                                bytes
                                            ),
                                        );
                                    }
                                }
                            }

                            // Use utility function for progress tracking
                            update_progress(&self.progress, progress_bar, bytes, start_time);
                        }
                        Err(e) => {
                            let error_str = e.to_string();

                            // Log error to file
                            if let Some(ref logger) = self.logger {
                                if let Ok(logger) = logger.lock() {
                                    logger.log_error(&format!(
                                        "Delta transfer failed {}{}: {}",
                                        path.display(),
                                        dest.display(),
                                        error_str
                                    ));
                                }
                            }

                            // Convert anyhow::Error to RoboSyncError
                            let robosync_err = match e.downcast::<std::io::Error>() {
                                Ok(io_err) => RoboSyncError::io_error(io_err, Some(path.clone())),
                                Err(e) => RoboSyncError::delta_failed(e.to_string(), path.clone()),
                            };
                            stats.add_structured_error(robosync_err, "delta_transfer");
                        }
                    }
                }
                FileOperation::Create { path } | FileOperation::Update { path, .. } => {
                    // Use platform API for new large files or updates without checksum
                    let relative = path.strip_prefix(source_root).unwrap_or(path);
                    let dest = dest_root.join(relative);

                    // Ensure parent directory exists
                    if let Some(parent) = dest.parent() {
                        std::fs::create_dir_all(parent)?;
                    }

                    let copier = PlatformCopier::new();
                    match copier.copy_file(path, &dest) {
                        Ok(bytes) => {
                            stats.add_bytes_transferred(bytes);
                            stats.increment_files_copied();

                            self.progress.add_file();
                            self.progress.add_bytes(bytes);

                            // Show on console only with -vv mode
                            if options.verbose >= 2 {
                                eprintln!(
                                    "  ✓ Copied: {}{} ({} bytes)",
                                    path.display(),
                                    dest.display(),
                                    bytes
                                );
                            }

                            // Log to file with -v or higher
                            if options.verbose >= 1 {
                                if let Some(ref logger) = self.logger {
                                    if let Ok(logger) = logger.lock() {
                                        logger.log_file_operation(
                                            "Copied",
                                            &format!(
                                                "{}{} ({} bytes)",
                                                path.display(),
                                                dest.display(),
                                                bytes
                                            ),
                                        );
                                    }
                                }
                            }

                            if let Some(pb) = progress_bar {
                                pb.inc(1);
                                let elapsed = start_time.elapsed().as_secs_f64();
                                if elapsed > 0.0 {
                                    let total_bytes = self.progress.get_bytes_transferred();
                                    let throughput = (total_bytes as f64 / elapsed) as u64;
                                    pb.set_message(format!("{}/s", humanize_bytes(throughput)));
                                }
                            }
                        }
                        Err(e) => {
                            let error_str = e.to_string();

                            // Log error to file
                            if let Some(ref logger) = self.logger {
                                if let Ok(logger) = logger.lock() {
                                    logger.log_error(&format!(
                                        "Delta transfer failed {}{}: {}",
                                        path.display(),
                                        dest.display(),
                                        error_str
                                    ));
                                }
                            }

                            // Convert anyhow::Error to RoboSyncError
                            let robosync_err = match e.downcast::<std::io::Error>() {
                                Ok(io_err) => RoboSyncError::io_error(io_err, Some(path.clone())),
                                Err(e) => RoboSyncError::delta_failed(e.to_string(), path.clone()),
                            };
                            stats.add_structured_error(robosync_err, "delta_transfer");
                        }
                    }
                }
                _ => {}
            }
        }

        Ok(stats)
    }

    /// Process very large files using delta transfer
    fn process_delta_files(
        &self,
        files: &[FileOperation],
        source_root: &Path,
        dest_root: &Path,
        options: &SyncOptions,
        progress_bar: Option<&indicatif::ProgressBar>,
        start_time: std::time::Instant,
    ) -> Result<SyncStats> {
        let stats = SyncStats::default();

        for op in files {
            match op {
                FileOperation::Create { path } => {
                    // New files can't use delta, use platform copy
                    let relative = path.strip_prefix(source_root).unwrap_or(path);
                    let dest = dest_root.join(relative);

                    // Ensure parent directory exists
                    if let Some(parent) = dest.parent() {
                        std::fs::create_dir_all(parent)?;
                    }

                    let copier = PlatformCopier::new();
                    match copier.copy_file(path, &dest) {
                        Ok(bytes) => {
                            stats.add_bytes_transferred(bytes);
                            stats.increment_files_copied();

                            self.progress.add_file();
                            self.progress.add_bytes(bytes);

                            if let Some(pb) = progress_bar {
                                pb.inc(1);
                                let elapsed = start_time.elapsed().as_secs_f64();
                                if elapsed > 0.0 {
                                    let total_bytes = self.progress.get_bytes_transferred();
                                    let throughput = (total_bytes as f64 / elapsed) as u64;
                                    pb.set_message(format!("{}/s", humanize_bytes(throughput)));
                                }
                            }
                        }
                        Err(e) => {
                            let error_str = e.to_string();

                            // Log error to file
                            if let Some(ref logger) = self.logger {
                                if let Ok(logger) = logger.lock() {
                                    logger.log_error(&format!(
                                        "Delta transfer failed {}{}: {}",
                                        path.display(),
                                        dest.display(),
                                        error_str
                                    ));
                                }
                            }

                            // Convert anyhow::Error to RoboSyncError
                            let robosync_err = match e.downcast::<std::io::Error>() {
                                Ok(io_err) => RoboSyncError::io_error(io_err, Some(path.clone())),
                                Err(e) => RoboSyncError::delta_failed(e.to_string(), path.clone()),
                            };
                            stats.add_structured_error(robosync_err, "delta_transfer");
                        }
                    }
                }
                FileOperation::Update { path, .. } => {
                    // Use delta transfer for updates
                    let relative = path.strip_prefix(source_root).unwrap_or(path);
                    let dest = dest_root.join(relative);

                    // Log start of delta transfer to console only with -vv
                    if options.verbose >= 2 {
                        eprintln!(
                            "  ⟳ Starting delta transfer: {}{} (file size: {})",
                            path.display(),
                            dest.display(),
                            humanize_bytes(std::fs::metadata(path).map(|m| m.len()).unwrap_or(0))
                        );
                    }

                    // Log to file with -v or higher
                    if options.verbose >= 1 {
                        if let Some(ref logger) = self.logger {
                            if let Ok(logger) = logger.lock() {
                                logger.log_file_operation(
                                    "Delta transfer starting",
                                    &format!("{}{}", path.display(), dest.display()),
                                );
                            }
                        }
                    }

                    match self.delta_copy_file(path, &dest, options) {
                        Ok(bytes) => {
                            stats.add_bytes_transferred(bytes);
                            stats.increment_files_copied();

                            // Log completion to console only with -vv
                            if options.verbose >= 2 {
                                eprintln!(
                                    "  ✓ Delta transfer complete: {}{} ({} transferred)",
                                    path.display(),
                                    dest.display(),
                                    humanize_bytes(bytes)
                                );
                            }

                            // Log to file with -v or higher
                            if options.verbose >= 1 {
                                if let Some(ref logger) = self.logger {
                                    if let Ok(logger) = logger.lock() {
                                        logger.log_file_operation(
                                            "Delta transfer complete",
                                            &format!(
                                                "{}{} ({} bytes)",
                                                path.display(),
                                                dest.display(),
                                                bytes
                                            ),
                                        );
                                    }
                                }
                            }

                            if let Some(pb) = progress_bar {
                                pb.inc(1);
                                pb.set_message(format!(
                                    "{} transferred (delta)",
                                    humanize_bytes(stats.bytes_transferred())
                                ));
                            }
                            self.progress.add_file();
                            self.progress.add_bytes(bytes);
                        }
                        Err(e) => {
                            let error_str = e.to_string();

                            // Log error to file
                            if let Some(ref logger) = self.logger {
                                if let Ok(logger) = logger.lock() {
                                    logger.log_error(&format!(
                                        "Delta transfer failed {}{}: {}",
                                        path.display(),
                                        dest.display(),
                                        error_str
                                    ));
                                }
                            }

                            // Convert anyhow::Error to RoboSyncError
                            let robosync_err = match e.downcast::<std::io::Error>() {
                                Ok(io_err) => RoboSyncError::io_error(io_err, Some(path.clone())),
                                Err(e) => RoboSyncError::delta_failed(e.to_string(), path.clone()),
                            };
                            stats.add_structured_error(robosync_err, "delta_transfer");
                        }
                    }
                }
                _ => {}
            }
        }

        Ok(stats)
    }

    /// Perform delta copy for a single file
    fn delta_copy_file(&self, source: &Path, dest: &Path, options: &SyncOptions) -> Result<u64> {
        use crate::metadata::{copy_file_with_metadata, CopyFlags};
        use crate::streaming_delta::StreamingDelta;
        use std::fs;

        // Create parent directory if needed
        if let Some(parent) = dest.parent() {
            fs::create_dir_all(parent)?;
        }

        // If destination doesn't exist, do a regular copy
        if !dest.exists() {
            let copy_flags = CopyFlags::from_string(&options.copy_flags);
            return copy_file_with_metadata(source, dest, &copy_flags);
        }

        // Use streaming delta for all file sizes
        let streaming_delta = StreamingDelta::new(DELTA_BLOCK_SIZE);

        // Log checksum generation
        if options.verbose >= 2 {
            eprintln!("    - Generating checksums for destination file...");
        }

        // Generate checksums for destination file
        let checksums = streaming_delta.generate_checksums_streaming(dest)?;

        if options.verbose >= 2 {
            eprintln!("    - Generated {} block checksums", checksums.len());
        }

        // Create temp file for reconstruction
        let temp_path = dest.with_extension("robosync_tmp");

        // Log delta application
        if options.verbose >= 2 {
            eprintln!("    - Applying delta transfer...");
        }

        // Apply delta transfer using streaming
        let transferred_bytes =
            streaming_delta.apply_delta_streaming(source, dest, &temp_path, &checksums)?;

        if options.verbose >= 2 {
            eprintln!(
                "    - Delta transfer complete, {} bytes transferred",
                transferred_bytes
            );
        }

        // Move temp file to destination
        fs::rename(&temp_path, dest)?;

        // Copy metadata - the file content is already updated, just need metadata
        let copy_flags = CopyFlags::from_string(&options.copy_flags);
        let _ = crate::metadata_utils::apply_metadata_after_delta(source, dest, &copy_flags);

        // Return the amount of data actually transferred
        Ok(transferred_bytes)
    }

    /// Merge two SyncStats
    fn merge_stats(&self, base: SyncStats, other: SyncStats) -> SyncStats {
        base.add_bytes_transferred(other.bytes_transferred());
        for _ in 0..other.files_copied() {
            base.increment_files_copied();
        }
        for _ in 0..other.errors() {
            base.increment_errors();
        }
        for _ in 0..other.files_deleted() {
            base.increment_files_deleted();
        }

        // Merge error details
        for error_detail in other.get_error_details() {
            base.add_error(
                error_detail.path,
                &error_detail.operation,
                &error_detail.message,
            );
        }

        // Merge structured errors
        for structured_error in other.get_structured_errors() {
            base.add_structured_error(structured_error.error, structured_error.context);
        }

        base
    }

    /// Process delete operations
    fn process_deletes(
        &self,
        deletes: &[FileOperation],
        _options: &SyncOptions,
        progress_bar: Option<&indicatif::ProgressBar>,
        _start_time: std::time::Instant,
    ) -> Result<SyncStats> {
        use crate::sync_stats::StructuredError;
        use std::sync::atomic::{AtomicU64, Ordering};
        use std::sync::{Arc, Mutex};

        // Thread-safe counters for parallel processing
        let files_deleted = Arc::new(AtomicU64::new(0));
        let structured_errors = Arc::new(Mutex::new(Vec::<StructuredError>::new()));

        // Process deletes in parallel using rayon
        deletes.par_iter().for_each(|op| {
            if let FileOperation::Delete { path } = op {
                // Use symlink_metadata to check type without following symlinks
                match std::fs::symlink_metadata(path) {
                    Ok(metadata) => {
                        if metadata.is_dir() {
                            match std::fs::remove_dir_all(path) {
                                Ok(_) => {
                                    files_deleted.fetch_add(1, Ordering::Relaxed);
                                    if let Some(pb) = progress_bar {
                                        pb.inc(1);
                                    }
                                    self.progress.add_file();
                                }
                                Err(e) => {
                                    // Don't print to stderr with progress bar active
                                    // Record structured error for log file
                                    let robosync_err =
                                        RoboSyncError::io_error(e, Some(path.clone()));
                                    if let Ok(mut errors) = structured_errors.lock() {
                                        errors.push(StructuredError {
                                            error: robosync_err,
                                            context: "remove_dir_all".to_string(),
                                        });
                                    }
                                }
                            }
                        } else {
                            match std::fs::remove_file(path) {
                                Ok(_) => {
                                    files_deleted.fetch_add(1, Ordering::Relaxed);
                                    if let Some(pb) = progress_bar {
                                        pb.inc(1);
                                    }
                                    self.progress.add_file();
                                }
                                Err(e) => {
                                    // Don't print to stderr with progress bar active
                                    // Record structured error for log file
                                    let robosync_err =
                                        RoboSyncError::io_error(e, Some(path.clone()));
                                    if let Ok(mut errors) = structured_errors.lock() {
                                        errors.push(StructuredError {
                                            error: robosync_err,
                                            context: "remove_file".to_string(),
                                        });
                                    }
                                }
                            }
                        }
                    }
                    Err(e) => {
                        // Don't print to stderr with progress bar active
                        // Record structured error for log file
                        let robosync_err = RoboSyncError::io_error(e, Some(path.clone()));
                        if let Ok(mut errors) = structured_errors.lock() {
                            errors.push(StructuredError {
                                error: robosync_err,
                                context: "symlink_metadata".to_string(),
                            });
                        }
                    }
                }
            }
        });

        // Create final stats from atomic counters
        let stats = SyncStats::default();

        // Add the counts using the proper methods
        let deleted_count = files_deleted.load(Ordering::Relaxed);

        // Use loop to add multiple counts since increment methods only add 1
        for _ in 0..deleted_count {
            stats.increment_files_deleted();
        }

        // Add structured errors to stats
        if let Ok(mut errors) = structured_errors.lock() {
            for error in errors.drain(..) {
                stats.add_structured_error(error.error, &error.context);
            }
        }

        Ok(stats)
    }
}

/// Categorized file operations
#[derive(Default, Clone)]
struct CategorizedOps {
    small_files: Vec<FileOperation>,
    medium_files: Vec<FileOperation>,
    large_files: Vec<FileOperation>,
    delta_files: Vec<FileOperation>, // Very large files that benefit from delta
    directories: Vec<FileOperation>,
    deletes: Vec<FileOperation>,
}

impl CategorizedOps {
    fn total_operations(&self) -> u64 {
        (self.small_files.len()
            + self.medium_files.len()
            + self.large_files.len()
            + self.delta_files.len()
            + self.deletes.len()) as u64
    }
}

/// File statistics by category
struct FileStats {
    total: u64,
    small: u64,
    medium: u64,
    large: u64,
    delta: u64,
}

/// Size statistics by category
struct SizeStats {
    total: u64,
    small: u64,
    medium: u64,
    large: u64,
    delta: u64,
}

/// Pending operation statistics
pub struct PendingStats {
    pub files_create: u64,
    pub files_update: u64,
    pub files_delete: u64,
    pub dirs_create: u64,
    pub size_create: u64,
    pub size_update: u64,
    pub size_delete: u64,
}

/// Detailed breakdown by size category
pub struct SizeBreakdown {
    pub small_count: u64,
    pub small_size: u64,
    pub medium_count: u64,
    pub medium_size: u64,
    pub large_count: u64,
    pub large_size: u64,
    pub delta_count: u64,
    pub delta_size: u64,
    pub delta_actual: u64, // Estimated actual transfer size for delta files
}

/// Detailed pending stats with size breakdowns
pub struct DetailedPendingStats {
    pub basic: PendingStats,
    pub create_breakdown: SizeBreakdown,
    pub update_breakdown: SizeBreakdown,
}

impl MixedStrategyExecutor {
    /// Calculate file and size statistics
    fn calculate_stats(
        &self,
        categorized: &CategorizedOps,
        _source_root: &Path,
    ) -> (FileStats, SizeStats) {
        let mut file_stats = FileStats {
            total: 0,
            small: 0,
            medium: 0,
            large: 0,
            delta: 0,
        };

        let mut size_stats = SizeStats {
            total: 0,
            small: 0,
            medium: 0,
            large: 0,
            delta: 0,
        };

        // Count and sum small files
        for op in &categorized.small_files {
            if let FileOperation::Create { path } | FileOperation::Update { path, .. } = op {
                if let Ok(metadata) = std::fs::metadata(path) {
                    if !metadata.is_dir() {
                        file_stats.small += 1;
                        file_stats.total += 1;
                        let size = metadata.len();
                        size_stats.small += size;
                        size_stats.total += size;
                    }
                }
            }
        }

        // Count and sum medium files
        for op in &categorized.medium_files {
            if let FileOperation::Create { path } | FileOperation::Update { path, .. } = op {
                if let Ok(metadata) = std::fs::metadata(path) {
                    if !metadata.is_dir() {
                        file_stats.medium += 1;
                        file_stats.total += 1;
                        let size = metadata.len();
                        size_stats.medium += size;
                        size_stats.total += size;
                    }
                }
            }
        }

        // Count and sum large files
        for op in &categorized.large_files {
            if let FileOperation::Create { path } | FileOperation::Update { path, .. } = op {
                if let Ok(metadata) = std::fs::metadata(path) {
                    if !metadata.is_dir() {
                        file_stats.large += 1;
                        file_stats.total += 1;
                        let size = metadata.len();
                        size_stats.large += size;
                        size_stats.total += size;
                    }
                }
            }
        }

        // Count and sum delta files (very large files)
        for op in &categorized.delta_files {
            if let FileOperation::Create { path } | FileOperation::Update { path, .. } = op {
                if let Ok(metadata) = std::fs::metadata(path) {
                    if !metadata.is_dir() {
                        file_stats.delta += 1;
                        file_stats.total += 1;
                        let size = metadata.len();
                        size_stats.delta += size;
                        size_stats.total += size;
                    }
                }
            }
        }

        (file_stats, size_stats)
    }

    /// Calculate pending operation statistics
    fn calculate_pending_stats(
        &self,
        categorized: &CategorizedOps,
        _source_root: &Path,
    ) -> PendingStats {
        let mut stats = PendingStats {
            files_create: 0,
            files_update: 0,
            files_delete: 0,
            dirs_create: 0,
            size_create: 0,
            size_update: 0,
            size_delete: 0,
        };

        // Process all file operations
        let all_file_ops = categorized
            .small_files
            .iter()
            .chain(categorized.medium_files.iter())
            .chain(categorized.large_files.iter())
            .chain(categorized.delta_files.iter());

        for op in all_file_ops {
            match op {
                FileOperation::Create { path } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            stats.files_create += 1;
                            stats.size_create += metadata.len();
                        }
                    }
                }
                FileOperation::Update { path, .. } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            stats.files_update += 1;
                            stats.size_update += metadata.len();
                        }
                    }
                }
                _ => {}
            }
        }

        // Count directory creates
        for op in &categorized.directories {
            if let FileOperation::CreateDirectory { .. } = op {
                stats.dirs_create += 1;
            }
        }

        // Count deletes
        stats.files_delete = categorized.deletes.len() as u64;
        // We don't track delete sizes in the current implementation

        stats
    }

    /// Calculate detailed pending stats with size breakdowns
    fn calculate_detailed_pending_stats(
        &self,
        categorized: &CategorizedOps,
        _source_root: &Path,
    ) -> DetailedPendingStats {
        let mut stats = DetailedPendingStats {
            basic: PendingStats {
                files_create: 0,
                files_update: 0,
                files_delete: 0,
                dirs_create: 0,
                size_create: 0,
                size_update: 0,
                size_delete: 0,
            },
            create_breakdown: SizeBreakdown {
                small_count: 0,
                small_size: 0,
                medium_count: 0,
                medium_size: 0,
                large_count: 0,
                large_size: 0,
                delta_count: 0,
                delta_size: 0,
                delta_actual: 0,
            },
            update_breakdown: SizeBreakdown {
                small_count: 0,
                small_size: 0,
                medium_count: 0,
                medium_size: 0,
                large_count: 0,
                large_size: 0,
                delta_count: 0,
                delta_size: 0,
                delta_actual: 0,
            },
        };

        // Process small files
        for op in &categorized.small_files {
            match op {
                FileOperation::Create { path } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            let size = metadata.len();
                            stats.basic.files_create += 1;
                            stats.basic.size_create += size;
                            stats.create_breakdown.small_count += 1;
                            stats.create_breakdown.small_size += size;
                        }
                    }
                }
                FileOperation::Update { path, .. } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            let size = metadata.len();
                            stats.basic.files_update += 1;
                            stats.basic.size_update += size;
                            stats.update_breakdown.small_count += 1;
                            stats.update_breakdown.small_size += size;
                        }
                    }
                }
                _ => {}
            }
        }

        // Process medium files
        for op in &categorized.medium_files {
            match op {
                FileOperation::Create { path } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            let size = metadata.len();
                            stats.basic.files_create += 1;
                            stats.basic.size_create += size;
                            stats.create_breakdown.medium_count += 1;
                            stats.create_breakdown.medium_size += size;
                        }
                    }
                }
                FileOperation::Update { path, .. } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            let size = metadata.len();
                            stats.basic.files_update += 1;
                            stats.basic.size_update += size;
                            stats.update_breakdown.medium_count += 1;
                            stats.update_breakdown.medium_size += size;
                        }
                    }
                }
                _ => {}
            }
        }

        // Process large files
        for op in &categorized.large_files {
            match op {
                FileOperation::Create { path } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            let size = metadata.len();
                            stats.basic.files_create += 1;
                            stats.basic.size_create += size;
                            stats.create_breakdown.large_count += 1;
                            stats.create_breakdown.large_size += size;
                        }
                    }
                }
                FileOperation::Update { path, .. } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            let size = metadata.len();
                            stats.basic.files_update += 1;
                            stats.basic.size_update += size;
                            stats.update_breakdown.large_count += 1;
                            stats.update_breakdown.large_size += size;
                        }
                    }
                }
                _ => {}
            }
        }

        // Process delta files (with estimated actual transfer)
        for op in &categorized.delta_files {
            match op {
                FileOperation::Create { path } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            let size = metadata.len();
                            stats.basic.files_create += 1;
                            stats.basic.size_create += size;
                            stats.create_breakdown.delta_count += 1;
                            stats.create_breakdown.delta_size += size;
                            stats.create_breakdown.delta_actual += size; // Full transfer for new files
                        }
                    }
                }
                FileOperation::Update { path, .. } => {
                    if let Ok(metadata) = std::fs::metadata(path) {
                        if !metadata.is_dir() {
                            let size = metadata.len();
                            stats.basic.files_update += 1;
                            stats.basic.size_update += size;
                            stats.update_breakdown.delta_count += 1;
                            stats.update_breakdown.delta_size += size;
                            // Estimate 10% change for delta files (conservative)
                            stats.update_breakdown.delta_actual += size / 10;
                        }
                    }
                }
                _ => {}
            }
        }

        // Count directory creates
        for op in &categorized.directories {
            if let FileOperation::CreateDirectory { .. } = op {
                stats.basic.dirs_create += 1;
            }
        }

        // Count deletes
        stats.basic.files_delete = categorized.deletes.len() as u64;

        stats
    }
}