ipfrs-interface 0.1.0

HTTP, gRPC, GraphQL and Python interfaces for IPFRS distributed storage
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
//! Streaming Support for IPFRS Gateway
//!
//! Provides:
//! - Memory-efficient streaming downloads
//! - Chunked uploads with progress tracking
//! - Server-Sent Events (SSE) for progress callbacks
//! - Batch block operations

use axum::{
    body::Body,
    extract::{Multipart, Path, Query, State},
    http::{header, StatusCode},
    response::{
        sse::{Event, KeepAlive, Sse},
        IntoResponse, Response,
    },
    Json,
};
use bytes::Bytes;
use futures::stream::{self, Stream, StreamExt};
use ipfrs_core::{Block, Cid};
use ipfrs_storage::BlockStoreTrait;
use serde::{Deserialize, Serialize};
use std::convert::Infallible;
use std::time::Duration;
use tokio::sync::broadcast;
use tracing::info;
use uuid::Uuid;

use crate::gateway::GatewayState;

// ============================================================================
// Flow Control & Concurrency
// ============================================================================

/// Concurrency control configuration for batch operations
#[derive(Debug, Clone)]
pub struct ConcurrencyConfig {
    /// Maximum number of concurrent tasks for batch operations (0 = unlimited)
    pub max_concurrent_tasks: usize,
    /// Enable parallel processing
    pub parallel_enabled: bool,
}

impl Default for ConcurrencyConfig {
    fn default() -> Self {
        Self {
            max_concurrent_tasks: 100, // Reasonable default
            parallel_enabled: true,
        }
    }
}

impl ConcurrencyConfig {
    /// Create a conservative config (lower concurrency)
    pub fn conservative() -> Self {
        Self {
            max_concurrent_tasks: 50,
            parallel_enabled: true,
        }
    }

    /// Create an aggressive config (higher concurrency)
    pub fn aggressive() -> Self {
        Self {
            max_concurrent_tasks: 200,
            parallel_enabled: true,
        }
    }

    /// Create a sequential config (no parallelism)
    pub fn sequential() -> Self {
        Self {
            max_concurrent_tasks: 1,
            parallel_enabled: false,
        }
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<(), String> {
        if self.max_concurrent_tasks == 0 && self.parallel_enabled {
            return Err(
                "max_concurrent_tasks cannot be 0 when parallel_enabled is true".to_string(),
            );
        }
        Ok(())
    }
}

/// Flow control configuration for streaming operations
#[derive(Debug, Clone)]
pub struct FlowControlConfig {
    /// Maximum bytes per second (0 = unlimited)
    pub max_bytes_per_second: u64,
    /// Initial window size in bytes
    pub initial_window_size: usize,
    /// Maximum window size in bytes
    pub max_window_size: usize,
    /// Minimum window size in bytes
    pub min_window_size: usize,
    /// Enable dynamic window adjustment
    pub dynamic_adjustment: bool,
}

impl Default for FlowControlConfig {
    fn default() -> Self {
        Self {
            max_bytes_per_second: 0,         // Unlimited
            initial_window_size: 256 * 1024, // 256KB
            max_window_size: 1024 * 1024,    // 1MB
            min_window_size: 64 * 1024,      // 64KB
            dynamic_adjustment: true,
        }
    }
}

impl FlowControlConfig {
    /// Create a flow control config with specific rate limit
    pub fn with_rate_limit(bytes_per_second: u64) -> Self {
        Self {
            max_bytes_per_second: bytes_per_second,
            ..Default::default()
        }
    }

    /// Create a conservative flow control config (smaller windows)
    pub fn conservative() -> Self {
        Self {
            initial_window_size: 64 * 1024,
            max_window_size: 256 * 1024,
            min_window_size: 32 * 1024,
            ..Default::default()
        }
    }

    /// Create an aggressive flow control config (larger windows)
    pub fn aggressive() -> Self {
        Self {
            initial_window_size: 512 * 1024,
            max_window_size: 2 * 1024 * 1024,
            min_window_size: 128 * 1024,
            ..Default::default()
        }
    }

    /// Validate configuration
    pub fn validate(&self) -> Result<(), String> {
        // Min window size must be less than or equal to initial window size
        if self.min_window_size > self.initial_window_size {
            return Err(format!(
                "Minimum window size ({}) cannot exceed initial window size ({})",
                self.min_window_size, self.initial_window_size
            ));
        }

        // Initial window size must be less than or equal to max window size
        if self.initial_window_size > self.max_window_size {
            return Err(format!(
                "Initial window size ({}) cannot exceed maximum window size ({})",
                self.initial_window_size, self.max_window_size
            ));
        }

        // Validate rate limit if set
        if self.max_bytes_per_second > 0 {
            validation::validate_rate_limit(self.max_bytes_per_second)?;
        }

        Ok(())
    }
}

/// Flow control state for a streaming operation
#[derive(Debug)]
pub struct FlowController {
    config: FlowControlConfig,
    current_window_size: usize,
    bytes_sent: u64,
    start_time: std::time::Instant,
    last_adjustment: std::time::Instant,
}

impl FlowController {
    /// Create a new flow controller
    pub fn new(config: FlowControlConfig) -> Self {
        Self {
            current_window_size: config.initial_window_size,
            config,
            bytes_sent: 0,
            start_time: std::time::Instant::now(),
            last_adjustment: std::time::Instant::now(),
        }
    }

    /// Get the current window size
    pub fn window_size(&self) -> usize {
        self.current_window_size
    }

    /// Calculate delay needed to respect rate limit
    pub fn calculate_delay(&self, bytes_to_send: usize) -> std::time::Duration {
        if self.config.max_bytes_per_second == 0 {
            return std::time::Duration::from_secs(0);
        }

        let elapsed = self.start_time.elapsed();
        let elapsed_secs = elapsed.as_secs_f64();

        if elapsed_secs == 0.0 {
            return std::time::Duration::from_secs(0);
        }

        let current_rate = self.bytes_sent as f64 / elapsed_secs;
        let target_rate = self.config.max_bytes_per_second as f64;

        if current_rate + (bytes_to_send as f64 / elapsed_secs) > target_rate {
            let delay_secs = (bytes_to_send as f64 / target_rate).max(0.0);
            std::time::Duration::from_secs_f64(delay_secs)
        } else {
            std::time::Duration::from_secs(0)
        }
    }

    /// Update flow control state after sending data
    pub fn on_data_sent(&mut self, bytes: usize) {
        self.bytes_sent += bytes as u64;

        // Adjust window size if dynamic adjustment is enabled
        if self.config.dynamic_adjustment {
            self.adjust_window();
        }
    }

    /// Dynamically adjust window size based on performance
    fn adjust_window(&mut self) {
        let elapsed = self.last_adjustment.elapsed();

        // Adjust every 100ms
        if elapsed < std::time::Duration::from_millis(100) {
            return;
        }

        self.last_adjustment = std::time::Instant::now();

        // Simple AIMD (Additive Increase Multiplicative Decrease) algorithm
        // Increase window size by 10% if no issues
        let new_size = (self.current_window_size as f64 * 1.1)
            .min(self.config.max_window_size as f64) as usize;

        self.current_window_size =
            new_size.clamp(self.config.min_window_size, self.config.max_window_size);
    }

    /// Decrease window size (on congestion)
    #[allow(dead_code)]
    pub fn on_congestion(&mut self) {
        // Multiplicative decrease by 50%
        let new_size = self.current_window_size / 2;
        self.current_window_size = new_size.max(self.config.min_window_size);
        self.last_adjustment = std::time::Instant::now();
    }

    /// Get current throughput in bytes per second
    pub fn current_throughput(&self) -> f64 {
        let elapsed = self.start_time.elapsed().as_secs_f64();
        if elapsed > 0.0 {
            self.bytes_sent as f64 / elapsed
        } else {
            0.0
        }
    }
}

// ============================================================================
// Resume/Cancel Support
// ============================================================================

/// Operation state for resume/cancel support
#[derive(Debug, Clone)]
pub struct OperationState {
    /// Unique operation ID
    pub operation_id: String,
    /// Current byte offset
    pub offset: u64,
    /// Total size (if known)
    pub total_size: Option<u64>,
    /// Operation type
    pub operation_type: OperationType,
    /// Status
    pub status: OperationStatus,
}

/// Operation type
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OperationType {
    Upload,
    Download,
}

/// Operation status
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum OperationStatus {
    InProgress,
    Paused,
    Cancelled,
    Completed,
    Failed,
}

/// Resume token for continuing operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResumeToken {
    /// Operation ID
    pub operation_id: String,
    /// Byte offset to resume from
    pub offset: u64,
    /// Optional CID for downloads
    pub cid: Option<String>,
}

impl ResumeToken {
    /// Create a new resume token
    pub fn new(operation_id: String, offset: u64, cid: Option<String>) -> Self {
        Self {
            operation_id,
            offset,
            cid,
        }
    }

    /// Encode resume token to base64
    pub fn encode(&self) -> Result<String, String> {
        let json = serde_json::to_string(self).map_err(|e| e.to_string())?;
        Ok(base64::Engine::encode(
            &base64::engine::general_purpose::URL_SAFE_NO_PAD,
            json.as_bytes(),
        ))
    }

    /// Decode resume token from base64
    pub fn decode(encoded: &str) -> Result<Self, String> {
        let bytes =
            base64::Engine::decode(&base64::engine::general_purpose::URL_SAFE_NO_PAD, encoded)
                .map_err(|e| e.to_string())?;

        let json = String::from_utf8(bytes).map_err(|e| e.to_string())?;
        serde_json::from_str(&json).map_err(|e| e.to_string())
    }
}

/// Cancel request
#[derive(Debug, Deserialize)]
pub struct CancelRequest {
    /// Operation ID to cancel
    pub operation_id: String,
}

/// Cancel response
#[derive(Debug, Serialize)]
pub struct CancelResponse {
    /// Operation ID that was cancelled
    pub operation_id: String,
    /// Whether cancellation was successful
    pub cancelled: bool,
    /// Optional resume token for later resumption
    pub resume_token: Option<String>,
}

// ============================================================================
// Progress Tracking
// ============================================================================

/// Progress event for uploads/downloads
#[derive(Debug, Clone, Serialize)]
pub struct ProgressEvent {
    /// Operation ID
    pub operation_id: String,
    /// Current bytes processed
    pub bytes_processed: u64,
    /// Total bytes (if known)
    pub total_bytes: Option<u64>,
    /// Progress percentage (0-100)
    pub progress_percent: Option<f32>,
    /// Status message
    pub status: ProgressStatus,
}

/// Progress status
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum ProgressStatus {
    Started,
    InProgress,
    Completed,
    Failed,
}

/// Progress tracker for streaming operations
#[derive(Clone)]
pub struct ProgressTracker {
    sender: broadcast::Sender<ProgressEvent>,
}

impl ProgressTracker {
    /// Create a new progress tracker
    pub fn new() -> Self {
        let (sender, _) = broadcast::channel(100);
        Self { sender }
    }

    /// Send a progress update
    pub fn send(&self, event: ProgressEvent) {
        let _ = self.sender.send(event);
    }

    /// Subscribe to progress updates
    pub fn subscribe(&self) -> broadcast::Receiver<ProgressEvent> {
        self.sender.subscribe()
    }
}

impl Default for ProgressTracker {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Streaming Downloads
// ============================================================================

/// Stream download query parameters
#[derive(Debug, Deserialize)]
pub struct StreamDownloadQuery {
    /// Chunk size in bytes (default: dynamic based on flow control)
    pub chunk_size: Option<usize>,
    /// Maximum bytes per second (0 = unlimited)
    pub max_bytes_per_second: Option<u64>,
    /// Enable flow control
    pub flow_control: Option<bool>,
    /// Resume token for continuing a previous download
    pub resume_token: Option<String>,
    /// Byte offset to start from (alternative to resume_token)
    pub offset: Option<u64>,
}

/// Stream content download endpoint
///
/// GET /v1/stream/download/{cid}
///
/// Streams content in chunks for memory-efficient downloads with optional flow control and resume support.
pub async fn stream_download(
    State(state): State<GatewayState>,
    Path(cid_str): Path<String>,
    Query(query): Query<StreamDownloadQuery>,
) -> Result<Response, StreamingError> {
    let cid: Cid = cid_str
        .parse()
        .map_err(|_| StreamingError::InvalidCid(cid_str.clone()))?;

    // Get the block
    let block = state
        .store
        .get(&cid)
        .await
        .map_err(|e| StreamingError::Storage(e.to_string()))?
        .ok_or_else(|| StreamingError::NotFound(cid_str.clone()))?;

    let data = block.data().to_vec();
    let total_size = data.len();

    // Determine start offset (from resume token or explicit offset)
    let start_offset = if let Some(resume_token) = &query.resume_token {
        let token = ResumeToken::decode(resume_token)
            .map_err(|e| StreamingError::Upload(format!("Invalid resume token: {}", e)))?;

        // Validate CID matches
        if let Some(token_cid) = &token.cid {
            if token_cid != &cid_str {
                return Err(StreamingError::Upload(
                    "Resume token CID mismatch".to_string(),
                ));
            }
        }

        token.offset as usize
    } else {
        query.offset.unwrap_or(0) as usize
    };

    // Validate offset
    if start_offset >= total_size {
        return Err(StreamingError::Upload(format!(
            "Invalid offset: {} (total size: {})",
            start_offset, total_size
        )));
    }

    // Initialize flow control if requested
    let enable_flow_control = query.flow_control.unwrap_or(false);
    let flow_controller = if enable_flow_control {
        let mut config = FlowControlConfig::default();
        if let Some(rate) = query.max_bytes_per_second {
            config.max_bytes_per_second = rate;
        }
        Some(FlowController::new(config))
    } else {
        None
    };

    // Determine chunk size (from query, flow control, or default)
    let chunk_size = query.chunk_size.unwrap_or_else(|| {
        flow_controller
            .as_ref()
            .map(|fc| fc.window_size())
            .unwrap_or(64 * 1024)
    });

    // Pre-collect chunks starting from offset to avoid lifetime issues
    let chunks: Vec<Vec<u8>> = data[start_offset..]
        .chunks(chunk_size)
        .map(|chunk| chunk.to_vec())
        .collect();

    let remaining_size = total_size - start_offset;

    // Create a stream that yields chunks with flow control
    let stream = if let Some(mut fc) = flow_controller {
        let stream = async_stream::stream! {
            for chunk in chunks {
                let chunk_len = chunk.len();

                // Apply flow control delay
                let delay = fc.calculate_delay(chunk_len);
                if !delay.is_zero() {
                    tokio::time::sleep(delay).await;
                }

                // Update flow control state
                fc.on_data_sent(chunk_len);

                yield Ok::<_, Infallible>(Bytes::from(chunk));
            }
        };
        Body::from_stream(stream)
    } else {
        let stream = stream::iter(chunks).map(|chunk| Ok::<_, Infallible>(Bytes::from(chunk)));
        Body::from_stream(stream)
    };

    // Build response with appropriate headers
    let mut response_builder = Response::builder();

    // If resuming, use 206 Partial Content
    if start_offset > 0 {
        response_builder = response_builder.status(StatusCode::PARTIAL_CONTENT);
        // Content-Range: bytes start-end/total
        let end_offset = total_size - 1;
        response_builder = response_builder.header(
            header::CONTENT_RANGE,
            format!("bytes {}-{}/{}", start_offset, end_offset, total_size),
        );
    } else {
        response_builder = response_builder.status(StatusCode::OK);
    }

    Ok(response_builder
        .header(header::CONTENT_TYPE, "application/octet-stream")
        .header(header::CONTENT_LENGTH, remaining_size.to_string())
        .header("X-Chunk-Size", chunk_size.to_string())
        .header("Accept-Ranges", "bytes")
        .body(stream)
        .unwrap())
}

// ============================================================================
// Streaming Uploads
// ============================================================================

/// Upload response
#[derive(Debug, Serialize)]
pub struct StreamUploadResponse {
    pub cid: String,
    pub size: u64,
    pub chunks_received: usize,
}

/// Stream upload endpoint with progress tracking
///
/// POST /v1/stream/upload
///
/// Accepts chunked uploads and provides progress updates via SSE.
pub async fn stream_upload(
    State(state): State<GatewayState>,
    mut multipart: Multipart,
) -> Result<Json<StreamUploadResponse>, StreamingError> {
    let mut total_data = Vec::new();
    let mut chunks_received = 0;

    // Process multipart data
    while let Some(field) = multipart
        .next_field()
        .await
        .map_err(|e| StreamingError::Upload(format!("Failed to read field: {}", e)))?
    {
        let data = field
            .bytes()
            .await
            .map_err(|e| StreamingError::Upload(format!("Failed to read data: {}", e)))?;

        total_data.extend_from_slice(&data);
        chunks_received += 1;
    }

    if total_data.is_empty() {
        return Err(StreamingError::Upload("No data received".to_string()));
    }

    // Create and store the block
    let block = Block::new(Bytes::from(total_data))
        .map_err(|e| StreamingError::Upload(format!("Failed to create block: {}", e)))?;

    let cid = *block.cid();
    let size = block.size();

    state
        .store
        .put(&block)
        .await
        .map_err(|e| StreamingError::Storage(e.to_string()))?;

    info!("Stream upload completed: {} ({} bytes)", cid, size);

    Ok(Json(StreamUploadResponse {
        cid: cid.to_string(),
        size,
        chunks_received,
    }))
}

// ============================================================================
// Batch Operations
// ============================================================================

/// Batch get request
#[derive(Debug, Deserialize)]
pub struct BatchGetRequest {
    /// List of CIDs to retrieve
    pub cids: Vec<String>,
}

/// Batch get response
#[derive(Debug, Serialize)]
pub struct BatchGetResponse {
    /// Successfully retrieved blocks
    pub blocks: Vec<BatchBlockResult>,
    /// Failed CIDs
    pub errors: Vec<BatchError>,
}

/// Individual block result in batch
#[derive(Debug, Serialize)]
pub struct BatchBlockResult {
    pub cid: String,
    pub data: String, // Base64 encoded
    pub size: u64,
}

/// Batch error for individual items
#[derive(Debug, Serialize)]
pub struct BatchError {
    pub cid: String,
    pub error: String,
}

/// Batch get endpoint (optimized with parallel processing)
///
/// POST /v1/block/batch/get
///
/// Retrieves multiple blocks in a single request.
/// Uses parallel processing for high throughput.
pub async fn batch_get(
    State(state): State<GatewayState>,
    Json(req): Json<BatchGetRequest>,
) -> Result<Json<BatchGetResponse>, StreamingError> {
    // Validate batch size
    validation::validate_batch_size(req.cids.len()).map_err(StreamingError::Validation)?;

    // Process all CIDs in parallel using concurrent tasks
    let tasks: Vec<_> = req
        .cids
        .into_iter()
        .map(|cid_str| {
            let state = state.clone();
            tokio::spawn(async move {
                match cid_str.parse::<Cid>() {
                    Ok(cid) => match state.store.get(&cid).await {
                        Ok(Some(block)) => {
                            let data_base64 = base64::Engine::encode(
                                &base64::engine::general_purpose::STANDARD,
                                block.data(),
                            );
                            Ok(BatchBlockResult {
                                cid: cid_str,
                                data: data_base64,
                                size: block.size(),
                            })
                        }
                        Ok(None) => Err(BatchError {
                            cid: cid_str,
                            error: "Block not found".to_string(),
                        }),
                        Err(e) => Err(BatchError {
                            cid: cid_str,
                            error: e.to_string(),
                        }),
                    },
                    Err(_) => Err(BatchError {
                        cid: cid_str,
                        error: "Invalid CID".to_string(),
                    }),
                }
            })
        })
        .collect();

    // Await all tasks and collect results
    let mut blocks = Vec::new();
    let mut errors = Vec::new();

    for task in tasks {
        match task.await {
            Ok(Ok(block)) => blocks.push(block),
            Ok(Err(error)) => errors.push(error),
            Err(e) => {
                // Task panicked or was cancelled
                errors.push(BatchError {
                    cid: "unknown".to_string(),
                    error: format!("Task execution error: {}", e),
                });
            }
        }
    }

    Ok(Json(BatchGetResponse { blocks, errors }))
}

/// Batch put request item
#[derive(Debug, Deserialize)]
pub struct BatchPutItem {
    /// Base64 encoded data
    pub data: String,
}

/// Transaction mode for batch operations
#[derive(Debug, Clone, Copy, Deserialize, Serialize, PartialEq, Eq, Default)]
#[serde(rename_all = "lowercase")]
pub enum TransactionMode {
    /// All-or-nothing: either all operations succeed or all fail
    Atomic,
    /// Best-effort: process each item independently
    #[default]
    BestEffort,
}

/// Batch put request
#[derive(Debug, Deserialize)]
pub struct BatchPutRequest {
    /// List of blocks to store
    pub blocks: Vec<BatchPutItem>,
    /// Transaction mode (default: best_effort)
    #[serde(default)]
    pub transaction_mode: TransactionMode,
}

/// Batch put response
#[derive(Debug, Serialize)]
pub struct BatchPutResponse {
    /// Successfully stored blocks
    pub stored: Vec<BatchStoredResult>,
    /// Failed items
    pub errors: Vec<BatchPutError>,
    /// Transaction ID (present in atomic mode)
    pub transaction_id: Option<String>,
    /// Transaction status
    pub transaction_status: TransactionStatus,
}

/// Transaction status
#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum TransactionStatus {
    /// All operations succeeded
    Committed,
    /// Some operations failed (best-effort mode)
    PartialSuccess,
    /// All operations rolled back (atomic mode)
    RolledBack,
}

/// Stored block result
#[derive(Debug, Serialize)]
pub struct BatchStoredResult {
    pub cid: String,
    pub size: u64,
    pub index: usize,
}

/// Batch put error
#[derive(Debug, Serialize)]
pub struct BatchPutError {
    pub index: usize,
    pub error: String,
}

/// Batch put endpoint
///
/// POST /v1/block/batch/put
///
/// Stores multiple blocks in a single request.
/// Supports atomic transactions (all-or-nothing) and best-effort mode.
pub async fn batch_put(
    State(state): State<GatewayState>,
    Json(req): Json<BatchPutRequest>,
) -> Result<Json<BatchPutResponse>, StreamingError> {
    let transaction_id = Uuid::new_v4().to_string();

    match req.transaction_mode {
        TransactionMode::Atomic => batch_put_atomic(state, req.blocks, transaction_id).await,
        TransactionMode::BestEffort => {
            batch_put_best_effort(state, req.blocks, transaction_id).await
        }
    }
}

/// Atomic batch put - all-or-nothing transaction
async fn batch_put_atomic(
    state: GatewayState,
    items: Vec<BatchPutItem>,
    transaction_id: String,
) -> Result<Json<BatchPutResponse>, StreamingError> {
    // Validate batch size
    validation::validate_batch_size(items.len()).map_err(StreamingError::Validation)?;

    // Phase 1: Validate all items and prepare blocks
    let mut prepared_blocks = Vec::new();
    let mut errors = Vec::new();

    for (index, item) in items.into_iter().enumerate() {
        match base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &item.data) {
            Ok(data) => match Block::new(Bytes::from(data)) {
                Ok(block) => {
                    prepared_blocks.push((index, block));
                }
                Err(e) => {
                    errors.push(BatchPutError {
                        index,
                        error: format!("Block creation error: {}", e),
                    });
                }
            },
            Err(e) => {
                errors.push(BatchPutError {
                    index,
                    error: format!("Base64 decode error: {}", e),
                });
            }
        }
    }

    // If any validation failed, rollback (don't store anything)
    if !errors.is_empty() {
        info!(
            "Atomic batch put [{}] rolled back: {} validation errors",
            transaction_id,
            errors.len()
        );
        return Ok(Json(BatchPutResponse {
            stored: vec![],
            errors,
            transaction_id: Some(transaction_id),
            transaction_status: TransactionStatus::RolledBack,
        }));
    }

    // Phase 2: Store all blocks
    let mut stored = Vec::new();
    let mut stored_cids = Vec::new(); // Track for potential rollback

    for (index, block) in prepared_blocks {
        let cid = *block.cid();
        let size = block.size();

        match state.store.put(&block).await {
            Ok(_) => {
                stored_cids.push(cid);
                stored.push(BatchStoredResult {
                    cid: cid.to_string(),
                    size,
                    index,
                });
            }
            Err(e) => {
                // Storage failure - rollback all stored blocks
                info!(
                    "Atomic batch put [{}] rolling back: storage error at index {}",
                    transaction_id, index
                );

                // Attempt to delete all previously stored blocks in this transaction
                for stored_cid in stored_cids {
                    let _ = state.store.delete(&stored_cid).await; // Best effort rollback
                }

                return Ok(Json(BatchPutResponse {
                    stored: vec![],
                    errors: vec![BatchPutError {
                        index,
                        error: format!("Storage error (transaction rolled back): {}", e),
                    }],
                    transaction_id: Some(transaction_id),
                    transaction_status: TransactionStatus::RolledBack,
                }));
            }
        }
    }

    info!(
        "Atomic batch put [{}] committed: {} blocks stored",
        transaction_id,
        stored.len()
    );

    Ok(Json(BatchPutResponse {
        stored,
        errors: vec![],
        transaction_id: Some(transaction_id),
        transaction_status: TransactionStatus::Committed,
    }))
}

/// Best-effort batch put - process each item independently
async fn batch_put_best_effort(
    state: GatewayState,
    items: Vec<BatchPutItem>,
    transaction_id: String,
) -> Result<Json<BatchPutResponse>, StreamingError> {
    // Validate batch size
    validation::validate_batch_size(items.len()).map_err(StreamingError::Validation)?;

    let mut stored = Vec::new();
    let mut errors = Vec::new();

    for (index, item) in items.into_iter().enumerate() {
        match base64::Engine::decode(&base64::engine::general_purpose::STANDARD, &item.data) {
            Ok(data) => match Block::new(Bytes::from(data)) {
                Ok(block) => {
                    let cid = *block.cid();
                    let size = block.size();

                    match state.store.put(&block).await {
                        Ok(_) => {
                            stored.push(BatchStoredResult {
                                cid: cid.to_string(),
                                size,
                                index,
                            });
                        }
                        Err(e) => {
                            errors.push(BatchPutError {
                                index,
                                error: format!("Storage error: {}", e),
                            });
                        }
                    }
                }
                Err(e) => {
                    errors.push(BatchPutError {
                        index,
                        error: format!("Block creation error: {}", e),
                    });
                }
            },
            Err(e) => {
                errors.push(BatchPutError {
                    index,
                    error: format!("Base64 decode error: {}", e),
                });
            }
        }
    }

    let status = if errors.is_empty() {
        TransactionStatus::Committed
    } else {
        TransactionStatus::PartialSuccess
    };

    info!(
        "Best-effort batch put [{}] completed: {} stored, {} errors",
        transaction_id,
        stored.len(),
        errors.len()
    );

    Ok(Json(BatchPutResponse {
        stored,
        errors,
        transaction_id: Some(transaction_id),
        transaction_status: status,
    }))
}

/// Batch has request
#[derive(Debug, Deserialize)]
pub struct BatchHasRequest {
    /// List of CIDs to check
    pub cids: Vec<String>,
}

/// Batch has response
#[derive(Debug, Serialize)]
pub struct BatchHasResponse {
    /// Results for each CID
    pub results: Vec<BatchHasResult>,
}

/// Individual has result
#[derive(Debug, Serialize)]
pub struct BatchHasResult {
    pub cid: String,
    pub exists: bool,
}

/// Batch has endpoint (optimized with parallel processing)
///
/// POST /v1/block/batch/has
///
/// Checks if multiple blocks exist in a single request.
/// Uses parallel processing for high throughput.
pub async fn batch_has(
    State(state): State<GatewayState>,
    Json(req): Json<BatchHasRequest>,
) -> Result<Json<BatchHasResponse>, StreamingError> {
    // Validate batch size
    validation::validate_batch_size(req.cids.len()).map_err(StreamingError::Validation)?;

    // Process all CIDs in parallel using concurrent tasks
    let tasks: Vec<_> = req
        .cids
        .into_iter()
        .map(|cid_str| {
            let state = state.clone();
            tokio::spawn(async move {
                let exists = if let Ok(cid) = cid_str.parse::<Cid>() {
                    state.store.has(&cid).await.unwrap_or(false)
                } else {
                    false
                };

                BatchHasResult {
                    cid: cid_str,
                    exists,
                }
            })
        })
        .collect();

    // Await all tasks and collect results
    let mut results = Vec::new();

    for task in tasks {
        match task.await {
            Ok(result) => results.push(result),
            Err(e) => {
                // Task panicked or was cancelled - mark as non-existent
                results.push(BatchHasResult {
                    cid: format!("task_error_{}", e),
                    exists: false,
                });
            }
        }
    }

    Ok(Json(BatchHasResponse { results }))
}

// ============================================================================
// Server-Sent Events (SSE) for Progress
// ============================================================================

/// SSE progress stream endpoint
///
/// GET /v1/progress/{operation_id}
///
/// Provides real-time progress updates via Server-Sent Events.
pub async fn progress_stream(
    State(_state): State<GatewayState>,
    Path(operation_id): Path<String>,
) -> Sse<impl Stream<Item = Result<Event, Infallible>>> {
    // Create a progress tracker for this operation
    let tracker = ProgressTracker::new();
    let mut receiver = tracker.subscribe();

    // Create the SSE stream
    let stream = async_stream::stream! {
        // Send initial event
        let initial = ProgressEvent {
            operation_id: operation_id.clone(),
            bytes_processed: 0,
            total_bytes: None,
            progress_percent: Some(0.0),
            status: ProgressStatus::Started,
        };
        yield Ok(Event::default()
            .event("progress")
            .json_data(initial)
            .unwrap());

        // Stream progress updates
        loop {
            match tokio::time::timeout(Duration::from_secs(30), receiver.recv()).await {
                Ok(Ok(event)) => {
                    let is_complete = matches!(event.status, ProgressStatus::Completed | ProgressStatus::Failed);
                    yield Ok(Event::default()
                        .event("progress")
                        .json_data(event)
                        .unwrap());

                    if is_complete {
                        break;
                    }
                }
                Ok(Err(_)) => {
                    // Channel closed
                    break;
                }
                Err(_) => {
                    // Timeout - send keepalive
                    yield Ok(Event::default().comment("keepalive"));
                }
            }
        }
    };

    Sse::new(stream).keep_alive(KeepAlive::default())
}

// ============================================================================
// Validation Utilities
// ============================================================================

/// Request validation utilities
pub mod validation {

    /// Validate CID string format
    pub fn validate_cid(cid: &str) -> Result<(), String> {
        if cid.is_empty() {
            return Err("CID cannot be empty".to_string());
        }

        // Basic CID validation (could be more comprehensive)
        if !cid.starts_with("Qm") && !cid.starts_with("bafy") && !cid.starts_with("baf") {
            return Err(format!("Invalid CID format: {}", cid));
        }

        if cid.len() < 10 {
            return Err(format!("CID too short: {}", cid));
        }

        Ok(())
    }

    /// Validate byte offset and size
    pub fn validate_offset(offset: u64, total_size: usize) -> Result<(), String> {
        if offset as usize >= total_size {
            return Err(format!(
                "Offset {} exceeds total size {}",
                offset, total_size
            ));
        }
        Ok(())
    }

    /// Validate chunk size (reasonable limits)
    pub fn validate_chunk_size(chunk_size: usize) -> Result<(), String> {
        const MIN_CHUNK_SIZE: usize = 1024; // 1KB
        const MAX_CHUNK_SIZE: usize = 10 * 1024 * 1024; // 10MB

        if chunk_size < MIN_CHUNK_SIZE {
            return Err(format!(
                "Chunk size {} is too small (minimum: {})",
                chunk_size, MIN_CHUNK_SIZE
            ));
        }

        if chunk_size > MAX_CHUNK_SIZE {
            return Err(format!(
                "Chunk size {} is too large (maximum: {})",
                chunk_size, MAX_CHUNK_SIZE
            ));
        }

        Ok(())
    }

    /// Validate rate limit
    pub fn validate_rate_limit(bytes_per_second: u64) -> Result<(), String> {
        const MAX_RATE: u64 = 10 * 1024 * 1024 * 1024; // 10 GB/s

        if bytes_per_second > MAX_RATE {
            return Err(format!(
                "Rate limit {} exceeds maximum {}",
                bytes_per_second, MAX_RATE
            ));
        }

        Ok(())
    }

    /// Validate batch size
    pub fn validate_batch_size(count: usize) -> Result<(), String> {
        const MAX_BATCH_SIZE: usize = 1000;

        if count == 0 {
            return Err("Batch cannot be empty".to_string());
        }

        if count > MAX_BATCH_SIZE {
            return Err(format!(
                "Batch size {} exceeds maximum {}",
                count, MAX_BATCH_SIZE
            ));
        }

        Ok(())
    }
}

// ============================================================================
// Error Types
// ============================================================================

/// Streaming operation errors
#[derive(Debug)]
pub enum StreamingError {
    InvalidCid(String),
    NotFound(String),
    Upload(String),
    Storage(String),
    Validation(String),
}

impl IntoResponse for StreamingError {
    fn into_response(self) -> Response {
        let (status, message) = match self {
            StreamingError::InvalidCid(cid) => {
                (StatusCode::BAD_REQUEST, format!("Invalid CID: {}", cid))
            }
            StreamingError::NotFound(cid) => {
                (StatusCode::NOT_FOUND, format!("Block not found: {}", cid))
            }
            StreamingError::Upload(msg) => {
                (StatusCode::BAD_REQUEST, format!("Upload error: {}", msg))
            }
            StreamingError::Storage(msg) => (
                StatusCode::INTERNAL_SERVER_ERROR,
                format!("Storage error: {}", msg),
            ),
            StreamingError::Validation(msg) => (
                StatusCode::BAD_REQUEST,
                format!("Validation error: {}", msg),
            ),
        };

        (status, message).into_response()
    }
}

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

    #[test]
    fn test_progress_event_serialization() {
        let event = ProgressEvent {
            operation_id: "test-123".to_string(),
            bytes_processed: 1024,
            total_bytes: Some(2048),
            progress_percent: Some(50.0),
            status: ProgressStatus::InProgress,
        };

        let json = serde_json::to_string(&event).unwrap();
        assert!(json.contains("test-123"));
        assert!(json.contains("1024"));
        assert!(json.contains("inprogress"));
    }

    #[test]
    fn test_progress_tracker() {
        let tracker = ProgressTracker::new();
        let _receiver = tracker.subscribe();

        let event = ProgressEvent {
            operation_id: "test".to_string(),
            bytes_processed: 100,
            total_bytes: Some(200),
            progress_percent: Some(50.0),
            status: ProgressStatus::InProgress,
        };

        tracker.send(event);

        // Note: In async context, we would await the receiver
        // This test just verifies the tracker can be created and used
    }

    #[test]
    fn test_batch_request_deserialization() {
        let json = r#"{"cids": ["QmTest1", "QmTest2"]}"#;
        let req: BatchGetRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.cids.len(), 2);
        assert_eq!(req.cids[0], "QmTest1");
    }

    #[test]
    fn test_batch_put_request_deserialization() {
        let json = r#"{"blocks": [{"data": "SGVsbG8="}]}"#;
        let req: BatchPutRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.blocks.len(), 1);
        assert_eq!(req.blocks[0].data, "SGVsbG8=");
        assert_eq!(req.transaction_mode, TransactionMode::BestEffort); // Default
    }

    #[test]
    fn test_batch_put_request_atomic_mode() {
        let json = r#"{"blocks": [{"data": "SGVsbG8="}], "transaction_mode": "atomic"}"#;
        let req: BatchPutRequest = serde_json::from_str(json).unwrap();
        assert_eq!(req.transaction_mode, TransactionMode::Atomic);
    }

    #[test]
    fn test_transaction_mode_default() {
        let mode = TransactionMode::default();
        assert_eq!(mode, TransactionMode::BestEffort);
    }

    #[test]
    fn test_transaction_status_serialization() {
        let status = TransactionStatus::Committed;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, r#""committed""#);

        let status = TransactionStatus::RolledBack;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, r#""rolledback""#);
    }

    #[test]
    fn test_batch_put_response_with_transaction() {
        let response = BatchPutResponse {
            stored: vec![],
            errors: vec![],
            transaction_id: Some("test-txn-123".to_string()),
            transaction_status: TransactionStatus::Committed,
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("test-txn-123"));
        assert!(json.contains("committed"));
    }

    #[test]
    fn test_flow_control_config_default() {
        let config = FlowControlConfig::default();
        assert_eq!(config.max_bytes_per_second, 0);
        assert_eq!(config.initial_window_size, 256 * 1024);
        assert_eq!(config.max_window_size, 1024 * 1024);
        assert_eq!(config.min_window_size, 64 * 1024);
        assert!(config.dynamic_adjustment);
    }

    #[test]
    fn test_flow_control_config_with_rate_limit() {
        let config = FlowControlConfig::with_rate_limit(1_000_000); // 1 MB/s
        assert_eq!(config.max_bytes_per_second, 1_000_000);
        assert!(config.dynamic_adjustment);
    }

    #[test]
    fn test_flow_control_config_conservative() {
        let config = FlowControlConfig::conservative();
        assert_eq!(config.initial_window_size, 64 * 1024);
        assert_eq!(config.max_window_size, 256 * 1024);
        assert_eq!(config.min_window_size, 32 * 1024);
    }

    #[test]
    fn test_flow_control_config_aggressive() {
        let config = FlowControlConfig::aggressive();
        assert_eq!(config.initial_window_size, 512 * 1024);
        assert_eq!(config.max_window_size, 2 * 1024 * 1024);
        assert_eq!(config.min_window_size, 128 * 1024);
    }

    #[test]
    fn test_flow_controller_window_size() {
        let config = FlowControlConfig::default();
        let controller = FlowController::new(config.clone());
        assert_eq!(controller.window_size(), config.initial_window_size);
    }

    #[test]
    fn test_flow_controller_no_rate_limit() {
        let config = FlowControlConfig::default(); // No rate limit (0)
        let controller = FlowController::new(config);

        // Should not delay when no rate limit
        let delay = controller.calculate_delay(1024);
        assert_eq!(delay, std::time::Duration::from_secs(0));
    }

    #[test]
    fn test_flow_controller_on_data_sent() {
        let config = FlowControlConfig::default();
        let mut controller = FlowController::new(config);

        controller.on_data_sent(1024);
        assert_eq!(controller.bytes_sent, 1024);

        controller.on_data_sent(2048);
        assert_eq!(controller.bytes_sent, 3072);
    }

    #[test]
    fn test_flow_controller_on_congestion() {
        let config = FlowControlConfig::default();
        let mut controller = FlowController::new(config.clone());

        let initial_window = controller.window_size();
        controller.on_congestion();

        // Window should decrease by 50%
        assert!(controller.window_size() < initial_window);
        assert!(controller.window_size() >= config.min_window_size);
    }

    #[test]
    fn test_flow_controller_throughput() {
        let config = FlowControlConfig::default();
        let mut controller = FlowController::new(config);

        // Send some data
        controller.on_data_sent(1024);

        // Throughput should be non-negative
        let throughput = controller.current_throughput();
        assert!(throughput >= 0.0);
    }

    #[test]
    fn test_resume_token_encode_decode() {
        let token = ResumeToken::new("op-123".to_string(), 4096, Some("QmTest123".to_string()));

        // Encode
        let encoded = token.encode().unwrap();
        assert!(!encoded.is_empty());

        // Decode
        let decoded = ResumeToken::decode(&encoded).unwrap();
        assert_eq!(decoded.operation_id, "op-123");
        assert_eq!(decoded.offset, 4096);
        assert_eq!(decoded.cid, Some("QmTest123".to_string()));
    }

    #[test]
    fn test_resume_token_invalid() {
        // Invalid base64
        let result = ResumeToken::decode("invalid!!!base64");
        assert!(result.is_err());

        // Valid base64 but invalid JSON
        let invalid_json = base64::Engine::encode(
            &base64::engine::general_purpose::URL_SAFE_NO_PAD,
            b"not json",
        );
        let result = ResumeToken::decode(&invalid_json);
        assert!(result.is_err());
    }

    #[test]
    fn test_operation_type_serialization() {
        let upload = OperationType::Upload;
        let json = serde_json::to_string(&upload).unwrap();
        assert_eq!(json, r#""upload""#);

        let download = OperationType::Download;
        let json = serde_json::to_string(&download).unwrap();
        assert_eq!(json, r#""download""#);
    }

    #[test]
    fn test_operation_status_serialization() {
        let status = OperationStatus::InProgress;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, r#""inprogress""#);

        let status = OperationStatus::Cancelled;
        let json = serde_json::to_string(&status).unwrap();
        assert_eq!(json, r#""cancelled""#);
    }

    #[test]
    fn test_cancel_response_serialization() {
        let response = CancelResponse {
            operation_id: "op-456".to_string(),
            cancelled: true,
            resume_token: Some("token123".to_string()),
        };

        let json = serde_json::to_string(&response).unwrap();
        assert!(json.contains("op-456"));
        assert!(json.contains("true"));
        assert!(json.contains("token123"));
    }

    // Validation tests
    #[test]
    fn test_validate_cid_valid() {
        assert!(validation::validate_cid("QmTest123456").is_ok());
        assert!(validation::validate_cid(
            "bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi"
        )
        .is_ok());
        assert!(validation::validate_cid(
            "bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy"
        )
        .is_ok());
    }

    #[test]
    fn test_validate_cid_invalid() {
        assert!(validation::validate_cid("").is_err());
        assert!(validation::validate_cid("invalid").is_err());
        assert!(validation::validate_cid("Qm").is_err());
    }

    #[test]
    fn test_validate_offset_valid() {
        assert!(validation::validate_offset(0, 1000).is_ok());
        assert!(validation::validate_offset(500, 1000).is_ok());
        assert!(validation::validate_offset(999, 1000).is_ok());
    }

    #[test]
    fn test_validate_offset_invalid() {
        assert!(validation::validate_offset(1000, 1000).is_err());
        assert!(validation::validate_offset(2000, 1000).is_err());
    }

    #[test]
    fn test_validate_chunk_size_valid() {
        assert!(validation::validate_chunk_size(1024).is_ok()); // Minimum
        assert!(validation::validate_chunk_size(64 * 1024).is_ok()); // 64KB
        assert!(validation::validate_chunk_size(10 * 1024 * 1024).is_ok()); // Maximum
    }

    #[test]
    fn test_validate_chunk_size_invalid() {
        assert!(validation::validate_chunk_size(512).is_err()); // Too small
        assert!(validation::validate_chunk_size(20 * 1024 * 1024).is_err()); // Too large
    }

    #[test]
    fn test_validate_rate_limit_valid() {
        assert!(validation::validate_rate_limit(0).is_ok()); // Unlimited
        assert!(validation::validate_rate_limit(1_000_000).is_ok()); // 1 MB/s
        assert!(validation::validate_rate_limit(10 * 1024 * 1024 * 1024).is_ok());
        // Maximum
    }

    #[test]
    fn test_validate_rate_limit_invalid() {
        assert!(validation::validate_rate_limit(20 * 1024 * 1024 * 1024).is_err());
        // Too large
    }

    #[test]
    fn test_validate_batch_size_valid() {
        assert!(validation::validate_batch_size(1).is_ok());
        assert!(validation::validate_batch_size(100).is_ok());
        assert!(validation::validate_batch_size(1000).is_ok()); // Maximum
    }

    #[test]
    fn test_validate_batch_size_invalid() {
        assert!(validation::validate_batch_size(0).is_err()); // Empty
        assert!(validation::validate_batch_size(1001).is_err()); // Too large
        assert!(validation::validate_batch_size(5000).is_err()); // Way too large
    }

    #[test]
    fn test_flow_control_config_validation_valid() {
        let config = FlowControlConfig::default();
        assert!(config.validate().is_ok());

        let config = FlowControlConfig::conservative();
        assert!(config.validate().is_ok());

        let config = FlowControlConfig::aggressive();
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_flow_control_config_validation_invalid() {
        // Min window size exceeds initial window size
        let config = FlowControlConfig {
            max_bytes_per_second: 0,
            initial_window_size: 64 * 1024,
            max_window_size: 1024 * 1024,
            min_window_size: 128 * 1024, // Exceeds initial
            dynamic_adjustment: true,
        };
        assert!(config.validate().is_err());

        // Initial window size exceeds max window size
        let config = FlowControlConfig {
            max_bytes_per_second: 0,
            initial_window_size: 2 * 1024 * 1024,
            max_window_size: 1024 * 1024, // Less than initial
            min_window_size: 64 * 1024,
            dynamic_adjustment: true,
        };
        assert!(config.validate().is_err());

        // Rate limit too high
        let config = FlowControlConfig {
            max_bytes_per_second: 20 * 1024 * 1024 * 1024, // Too high
            initial_window_size: 256 * 1024,
            max_window_size: 1024 * 1024,
            min_window_size: 64 * 1024,
            dynamic_adjustment: true,
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_concurrency_config_default() {
        let config = ConcurrencyConfig::default();
        assert_eq!(config.max_concurrent_tasks, 100);
        assert!(config.parallel_enabled);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_concurrency_config_conservative() {
        let config = ConcurrencyConfig::conservative();
        assert_eq!(config.max_concurrent_tasks, 50);
        assert!(config.parallel_enabled);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_concurrency_config_aggressive() {
        let config = ConcurrencyConfig::aggressive();
        assert_eq!(config.max_concurrent_tasks, 200);
        assert!(config.parallel_enabled);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_concurrency_config_sequential() {
        let config = ConcurrencyConfig::sequential();
        assert_eq!(config.max_concurrent_tasks, 1);
        assert!(!config.parallel_enabled);
        assert!(config.validate().is_ok());
    }

    #[test]
    fn test_concurrency_config_validation_invalid() {
        let config = ConcurrencyConfig {
            max_concurrent_tasks: 0,
            parallel_enabled: true,
        };
        assert!(config.validate().is_err());
    }

    #[test]
    fn test_concurrency_config_validation_valid() {
        let config = ConcurrencyConfig {
            max_concurrent_tasks: 0,
            parallel_enabled: false,
        };
        assert!(config.validate().is_ok());

        let config = ConcurrencyConfig {
            max_concurrent_tasks: 100,
            parallel_enabled: true,
        };
        assert!(config.validate().is_ok());
    }
}