digdigdig3 0.1.9

Multi-exchange connector library — unified async Rust API for 42 connectors: 19 CEX, 3 DEX, 5 forex/brokers, 14 stock providers, and 2 data feeds
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
//! # Coinbase Connector
//!
//! Implementation of all core traits for Coinbase Advanced Trade API.
//!
//! ## Core Traits
//! - `ExchangeIdentity` - exchange identification
//! - `MarketData` - market data (spot + LIMITED perpetuals)
//! - `Trading` - trading operations (spot + perpetuals)
//! - `Account` - account information
//!
//! ## Perpetual Futures Support
//!
//! Coinbase offers perpetual futures through the Advanced Trade API with significant limitations:
//!
//! ### What Works (Public REST API):
//! - ✅ `get_price()` - Get current perpetual price via best bid/ask
//! - ✅ `get_ticker()` - Get ticker data for perpetuals
//! - ✅ Product listing with `product_type=FUTURE&contract_expiry_type=PERPETUAL`
//!
//! ### What Does NOT Work (Public REST API):
//! - ❌ `get_orderbook()` - Orderbook endpoint is **SPOT ONLY**
//! - ❌ `get_klines()` - Candles endpoint is **SPOT ONLY**
//!
//! ### Alternatives for Full Perpetuals Data:
//! 1. **WebSocket Feeds** - Use Advanced Trade WebSocket with channels:
//!    - `level2` - Real-time orderbook updates
//!    - `candles` - Real-time candlestick updates
//!    - `ticker` - Price updates
//!    - `futures_balance_summary` - Perpetuals-specific data
//!
//! 2. **INTX API** - Coinbase International Exchange for institutional users:
//!    - REST: `/instruments/{instrument}/candles` - Historical candles
//!    - REST: `/instruments/{instrument}/quote` - Best bid/ask (L1)
//!    - WebSocket: `L2_DATA` channel - Full orderbook depth
//!    - WebSocket: `CANDLES` channel - Candlestick updates
//!    - **Note**: Requires authentication even for market data
//!
//! 3. **Authenticated Advanced Trade** - With API credentials:
//!    - May have access to additional perpetuals endpoints
//!    - Still limited compared to INTX
//!
//! ### Symbol Format:
//! - Spot: `BTC-USD` (base-quote)
//! - Perpetuals: `BTC-PERP` (base-PERP, quote ignored)
//!
//! ### Trading:
//! - Perpetual futures trading IS supported via Advanced Trade API
//! - Requires USDC margin and proper collateral
//! - Up to 10x leverage available
//! - Same order endpoints work for both spot and perpetuals
//!
//! ## References:
//! - Research: `coinbase_futures_data_api_report.md`
//! - Advanced Trade Docs: https://docs.cdp.coinbase.com/advanced-trade/docs/perpetuals
//! - INTX Docs: https://docs.cloud.coinbase.com/intx/docs/welcome

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

use async_trait::async_trait;
use reqwest::header::HeaderMap;
use serde_json::{json, Value};

use crate::core::{
    HttpClient, Credentials,
    ExchangeId, ExchangeType, AccountType, Symbol,
    ExchangeError, ExchangeResult,
    Price, Kline, Ticker, OrderBook,
    Order, OrderSide, OrderType,Balance, AccountInfo,
    Position, FundingRate,
    OrderRequest, CancelRequest, CancelScope,
    BalanceQuery, PositionQuery, PositionModification,
    OrderHistoryFilter, PlaceOrderResponse, FeeInfo,
    UserTrade, UserTradeFilter,
};
use crate::core::types::{
    WithdrawRequest, WithdrawResponse, DepositAddress,
    FundsHistoryFilter, FundsRecord, FundsRecordType,
};
use crate::core::types::SymbolInfo;
use crate::core::traits::{
    ExchangeIdentity, MarketData, Trading, Account, Positions, CancelAll, CustodialFunds,
};
use crate::core::types::{CancelAllResponse, OrderResult};
use crate::core::types::ConnectorStats;
use crate::core::utils::WeightRateLimiter;
use crate::core::utils::precision::PrecisionCache;

use super::endpoints::{CoinbaseUrls, CoinbaseEndpoint, format_symbol, map_kline_interval};
use super::auth::CoinbaseAuth;
use super::parser::CoinbaseParser;

// ═══════════════════════════════════════════════════════════════════════════════
// CONNECTOR
// ═══════════════════════════════════════════════════════════════════════════════

/// Coinbase connector
pub struct CoinbaseConnector {
    /// HTTP client
    http: HttpClient,
    /// Authentication (None for public methods)
    auth: Option<CoinbaseAuth>,
    /// Rate limiter (30 requests per second for private, 10 for public)
    rate_limiter: Arc<Mutex<WeightRateLimiter>>,
    /// Per-symbol precision cache (populated after get_exchange_info)
    precision: PrecisionCache,
}

impl CoinbaseConnector {
    /// Create new connector
    pub async fn new(credentials: Option<Credentials>) -> ExchangeResult<Self> {
        let http = HttpClient::new(30_000)?; // 30 sec timeout

        let auth = if let Some(creds) = credentials {
            Some(CoinbaseAuth::new(&creds)
                .map_err(ExchangeError::Auth)?)
        } else {
            None
        };

        // Initialize rate limiter: 30 requests per second (Coinbase private tier)
        let rate_limiter = Arc::new(Mutex::new(
            WeightRateLimiter::new(30, Duration::from_secs(1))
        ));

        Ok(Self {
            http,
            auth,
            rate_limiter,
            precision: PrecisionCache::new(),
        })
    }

    /// Create connector only for public methods
    pub async fn public() -> ExchangeResult<Self> {
        Self::new(None).await
    }

    // ═══════════════════════════════════════════════════════════════════════════
    // HTTP HELPERS
    // ═══════════════════════════════════════════════════════════════════════════

    /// Update rate limiter from Coinbase response headers
    ///
    /// Coinbase reports: CB-RATELIMIT-REMAINING = remaining, CB-RATELIMIT-LIMIT = total limit
    fn update_rate_from_headers(&self, headers: &HeaderMap) {
        let remaining = headers
            .get("CB-RATELIMIT-REMAINING")
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse::<u32>().ok());

        let limit = headers
            .get("CB-RATELIMIT-LIMIT")
            .and_then(|v| v.to_str().ok())
            .and_then(|s| s.parse::<u32>().ok())
            .or_else(|| {
                // Fall back to the limiter's max_weight if no limit header
                self.rate_limiter.lock().ok().map(|l| l.max_weight())
            });

        if let (Some(remaining), Some(limit)) = (remaining, limit) {
            let used = limit.saturating_sub(remaining);
            if let Ok(mut limiter) = self.rate_limiter.lock() {
                limiter.update_from_server(used);
            }
        }
    }

    /// Wait for rate limit if needed
    async fn rate_limit_wait(&self, weight: u32) {
        loop {
            let wait_time = {
                let mut limiter = self.rate_limiter.lock().expect("Mutex poisoned");
                if limiter.try_acquire(weight) {
                    return;
                }
                limiter.time_until_ready(weight)
            };

            if wait_time > Duration::ZERO {
                tokio::time::sleep(wait_time).await;
            }
        }
    }

    /// GET request
    async fn get(
        &self,
        endpoint: CoinbaseEndpoint,
        params: HashMap<String, String>,
    ) -> ExchangeResult<Value> {
        self.rate_limit_wait(1).await;

        let path = endpoint.path();

        // Build query string
        let query = if params.is_empty() {
            String::new()
        } else {
            let qs: Vec<String> = params.iter()
                .map(|(k, v)| format!("{}={}", k, v))
                .collect();
            format!("?{}", qs.join("&"))
        };

        // Decide whether to use public or private endpoint
        let (base_url, use_public) = if endpoint.is_private() && self.auth.is_some() {
            (CoinbaseUrls::base_url(), false)
        } else if endpoint.has_public_alternative() {
            (CoinbaseUrls::market_url(), true)
        } else if !endpoint.is_private() {
            (CoinbaseUrls::base_url(), false)
        } else {
            return Err(ExchangeError::Auth("Authentication required".to_string()));
        };

        // Use public market path if available
        let final_path = if use_public && endpoint.market_path().is_some() {
            endpoint.market_path().expect("market_path() is Some, checked above")
        } else {
            path
        };

        let full_path = format!("{}{}", final_path, query);
        let url = format!("{}{}", base_url, full_path);

        // Add auth headers if needed
        let headers = if !use_public && endpoint.is_private() {
            let auth = self.auth.as_ref()
                .ok_or_else(|| ExchangeError::Auth("Authentication required".to_string()))?;
            auth.sign_request("GET", &full_path)
                .map_err(ExchangeError::Auth)?
        } else {
            HashMap::new()
        };

        let (response, resp_headers) = self.http.get_with_response_headers(&url, &HashMap::new(), &headers).await?;
        self.update_rate_from_headers(&resp_headers);
        Ok(response)
    }

    /// POST request
    async fn post(
        &self,
        endpoint: CoinbaseEndpoint,
        body: Value,
    ) -> ExchangeResult<Value> {
        self.rate_limit_wait(1).await;

        let base_url = CoinbaseUrls::base_url();
        let path = endpoint.path();
        let url = format!("{}{}", base_url, path);

        // Auth headers (POST always requires auth)
        let auth = self.auth.as_ref()
            .ok_or_else(|| ExchangeError::Auth("Authentication required".to_string()))?;
        let headers = auth.sign_request("POST", path)
            .map_err(ExchangeError::Auth)?;

        let (response, resp_headers) = self.http.post_with_response_headers(&url, &body, &headers).await?;
        self.update_rate_from_headers(&resp_headers);
        Ok(response)
    }

    /// GET request against the v2 API with a dynamic path (account-specific endpoints).
    ///
    /// `path` must be a fully constructed path like `/accounts/{uuid}/deposits`.
    async fn get_v2(&self, path: &str, params: HashMap<String, String>) -> ExchangeResult<Value> {
        self.rate_limit_wait(1).await;

        let query = if params.is_empty() {
            String::new()
        } else {
            let qs: Vec<String> = params.iter()
                .map(|(k, v)| format!("{}={}", k, v))
                .collect();
            format!("?{}", qs.join("&"))
        };

        let full_path = format!("{}{}", path, query);
        let url = format!("{}{}", CoinbaseUrls::v2_url(), full_path);

        let auth = self.auth.as_ref()
            .ok_or_else(|| ExchangeError::Auth("Authentication required".to_string()))?;
        let headers = auth.sign_request("GET", &full_path)
            .map_err(ExchangeError::Auth)?;

        let (response, resp_headers) = self.http.get_with_response_headers(&url, &HashMap::new(), &headers).await?;
        self.update_rate_from_headers(&resp_headers);
        Ok(response)
    }

    /// POST request against the v2 API with a dynamic path.
    async fn post_v2(&self, path: &str, body: Value) -> ExchangeResult<Value> {
        self.rate_limit_wait(1).await;

        let url = format!("{}{}", CoinbaseUrls::v2_url(), path);

        let auth = self.auth.as_ref()
            .ok_or_else(|| ExchangeError::Auth("Authentication required".to_string()))?;
        let headers = auth.sign_request("POST", path)
            .map_err(ExchangeError::Auth)?;

        let (response, resp_headers) = self.http.post_with_response_headers(&url, &body, &headers).await?;
        self.update_rate_from_headers(&resp_headers);
        Ok(response)
    }

    /// Find the Coinbase account UUID for a given asset (e.g. "BTC", "ETH").
    ///
    /// Coinbase uses per-asset account UUIDs in the v2 API. This helper fetches
    /// the account list and returns the UUID for the requested asset.
    async fn find_account_id(&self, asset: &str) -> ExchangeResult<String> {
        let response = self.get(CoinbaseEndpoint::Accounts, HashMap::new()).await?;
        CoinbaseParser::find_account_id_for_asset(&response, asset)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// EXCHANGE IDENTITY
// ═══════════════════════════════════════════════════════════════════════════════

impl ExchangeIdentity for CoinbaseConnector {
    fn exchange_id(&self) -> ExchangeId {
        ExchangeId::Coinbase
    }

    fn metrics(&self) -> ConnectorStats {
        let (http_requests, http_errors, last_latency_ms) = self.http.stats();
        let (rate_used, rate_max) = if let Ok(mut limiter) = self.rate_limiter.lock() {
            (limiter.current_weight(), limiter.max_weight())
        } else {
            (0, 0)
        };
        ConnectorStats {
            http_requests,
            http_errors,
            last_latency_ms,
            rate_used,
            rate_max,
            rate_groups: Vec::new(),
            ws_ping_rtt_ms: 0,
        }
    }

    fn is_testnet(&self) -> bool {
        false // Coinbase doesn't have testnet for Advanced Trade
    }

    fn supported_account_types(&self) -> Vec<AccountType> {
        // Spot: Full support
        // FuturesCross: LIMITED - only ticker/price data available via public REST
        //   - Orderbook and candles are SPOT ONLY via REST API
        //   - Full futures data requires WebSocket or INTX API with auth
        vec![AccountType::Spot, AccountType::FuturesCross]
    }

    fn exchange_type(&self) -> ExchangeType {
        ExchangeType::Cex
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// MARKET DATA
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl MarketData for CoinbaseConnector {
    async fn get_price(
        &self,
        symbol: Symbol,
        account_type: AccountType,
    ) -> ExchangeResult<Price> {
        let product_id = format_symbol(&symbol, account_type);

        if self.auth.is_some() {
            // Authenticated: use BestBidAsk endpoint (private)
            let mut params = HashMap::new();
            params.insert("product_ids".to_string(), product_id);
            let response = self.get(CoinbaseEndpoint::BestBidAsk, params).await?;
            let ticker = CoinbaseParser::parse_ticker(&response)?;
            Ok(ticker.last_price)
        } else {
            // Public: use ProductBook endpoint (has public /market alternative)
            let mut params = HashMap::new();
            params.insert("product_id".to_string(), product_id);
            let response = self.get(CoinbaseEndpoint::ProductBook, params).await?;
            let orderbook = CoinbaseParser::parse_orderbook(&response)?;
            // Derive price from best bid/ask
            let bid = orderbook.bids.first().map(|(p, _)| *p);
            let ask = orderbook.asks.first().map(|(p, _)| *p);
            match (bid, ask) {
                (Some(b), Some(a)) => Ok((b + a) / 2.0),
                (Some(b), None) => Ok(b),
                (None, Some(a)) => Ok(a),
                (None, None) => Err(ExchangeError::Parse("No bid or ask in orderbook".into())),
            }
        }
    }

    async fn get_ticker(
        &self,
        symbol: Symbol,
        account_type: AccountType,
    ) -> ExchangeResult<Ticker> {
        let product_id = format_symbol(&symbol, account_type);

        if self.auth.is_some() {
            // Authenticated: use BestBidAsk endpoint (private)
            let mut params = HashMap::new();
            params.insert("product_ids".to_string(), product_id.clone());
            let response = self.get(CoinbaseEndpoint::BestBidAsk, params).await?;
            CoinbaseParser::parse_ticker(&response)
        } else {
            // Public: use ProductBook endpoint (has public /market alternative)
            let mut params = HashMap::new();
            params.insert("product_id".to_string(), product_id.clone());
            let response = self.get(CoinbaseEndpoint::ProductBook, params).await?;
            let orderbook = CoinbaseParser::parse_orderbook(&response)?;
            // Build ticker from orderbook data
            let bid_price = orderbook.bids.first().map(|(p, _)| *p);
            let ask_price = orderbook.asks.first().map(|(p, _)| *p);
            let last_price = match (bid_price, ask_price) {
                (Some(b), Some(a)) => (b + a) / 2.0,
                (Some(b), None) => b,
                (None, Some(a)) => a,
                (None, None) => return Err(ExchangeError::Parse("No bid or ask in orderbook".into())),
            };
            Ok(Ticker {
                symbol: product_id,
                last_price,
                bid_price,
                ask_price,
                high_24h: None,
                low_24h: None,
                volume_24h: None,
                quote_volume_24h: None,
                price_change_24h: None,
                price_change_percent_24h: None,
                timestamp: orderbook.timestamp,
            })
        }
    }

    async fn get_orderbook(
        &self,
        symbol: Symbol,
        depth: Option<u16>,
        account_type: AccountType,
    ) -> ExchangeResult<OrderBook> {
        // LIMITATION: Coinbase REST API orderbook endpoint is SPOT ONLY
        // For perpetuals, use WebSocket level2 channel or INTX API
        if matches!(account_type, AccountType::FuturesCross | AccountType::FuturesIsolated) {
            return Err(ExchangeError::NotSupported(
                "Coinbase REST API orderbook is SPOT ONLY. For perpetual futures orderbook, use WebSocket or INTX API".to_string()
            ));
        }

        let mut params = HashMap::new();
        params.insert("product_id".to_string(), format_symbol(&symbol, account_type));

        if let Some(d) = depth {
            params.insert("limit".to_string(), d.to_string());
        }

        let response = self.get(CoinbaseEndpoint::ProductBook, params).await?;
        CoinbaseParser::parse_orderbook(&response)
    }

    async fn get_klines(
        &self,
        symbol: Symbol,
        interval: &str,
        limit: Option<u16>,
        account_type: AccountType,
        end_time: Option<i64>,
    ) -> ExchangeResult<Vec<Kline>> {
        if matches!(account_type, AccountType::FuturesCross | AccountType::FuturesIsolated) {
            return Err(ExchangeError::NotSupported(
                "Coinbase REST API candles are SPOT ONLY".to_string()
            ));
        }

        let product_id = format_symbol(&symbol, account_type);
        let granularity = map_kline_interval(interval);

        let endpoint = CoinbaseEndpoint::Candles;
        let base_path = format!("{}/{}/candles", endpoint.path(), product_id);

        let mut params = HashMap::new();
        params.insert("granularity".to_string(), granularity.to_string());

        // Coinbase requires BOTH start + end, max 300 candles per window.
        // "end" alone is ignored.
        if let Some(et) = end_time {
            let end_s = et / 1000;
            let interval_s = interval_to_secs(interval) as i64;
            let count = limit.unwrap_or(350).min(350) as i64;
            let start_s = end_s - count * interval_s;
            params.insert("start".to_string(), start_s.to_string());
            params.insert("end".to_string(), end_s.to_string());
        }

        let query: Vec<String> = params.iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect();
        let query_str = if query.is_empty() {
            String::new()
        } else {
            format!("?{}", query.join("&"))
        };

        let base_url = if self.auth.is_some() {
            CoinbaseUrls::base_url()
        } else {
            CoinbaseUrls::market_url()
        };

        let url = format!("{}{}{}", base_url, base_path, query_str);

        let headers = if let Some(auth) = &self.auth {
            let full_path = format!("{}{}", base_path, query_str);
            auth.sign_request("GET", &full_path)
                .map_err(ExchangeError::Auth)?
        } else {
            HashMap::new()
        };

        self.rate_limit_wait(1).await;
        let (response, resp_headers) = self.http.get_with_response_headers(&url, &HashMap::new(), &headers).await?;
        self.update_rate_from_headers(&resp_headers);
        let mut klines = CoinbaseParser::parse_klines(&response)?;

        if let Some(l) = limit {
            klines.truncate(l.min(350) as usize);
        }

        Ok(klines)
    }

    async fn ping(&self) -> ExchangeResult<()> {
        // Coinbase doesn't have a dedicated ping endpoint
        // Use the server time endpoint as a health check
        // base_url() already includes /api/v3/brokerage, so just append /time
        let url = format!("{}/time", CoinbaseUrls::base_url());
        self.http.get(&url, &HashMap::new()).await?;
        Ok(())
    }

    async fn get_exchange_info(&self, account_type: AccountType) -> ExchangeResult<Vec<SymbolInfo>> {
        // GET /market/products (public) returns products list
        let params = HashMap::new();
        let response = self.get(CoinbaseEndpoint::Products, params).await?;
        let symbols = CoinbaseParser::parse_exchange_info(&response, account_type)?;
        self.precision.load_from_symbols(&symbols);
        Ok(symbols)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// TRADING
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl Trading for CoinbaseConnector {
    async fn place_order(&self, req: OrderRequest) -> ExchangeResult<PlaceOrderResponse> {
        let symbol = req.symbol.clone();
        let side = req.side;
        let quantity = req.quantity;
        let account_type = req.account_type;

        let product_id = format_symbol(&symbol, account_type);
        let side_str = match side { OrderSide::Buy => "BUY", OrderSide::Sell => "SELL" };
        let client_order_id = req.client_order_id.clone()
            .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
        let sym = &product_id;

        let order_config = match req.order_type {
            OrderType::Market => {
                // Coinbase market buy uses quote_size; market sell uses base_size
                let size_field = match side {
                    OrderSide::Buy => "quote_size",
                    OrderSide::Sell => "base_size",
                };
                json!({ "market_market_ioc": { size_field: self.precision.qty(sym, quantity) } })
            }
            OrderType::Limit { price } => {
                let post_only = matches!(req.time_in_force, crate::core::TimeInForce::PostOnly);
                let tif_key = match req.time_in_force {
                    crate::core::TimeInForce::Ioc => "limit_limit_ioc",
                    crate::core::TimeInForce::Fok => "limit_limit_fok",
                    crate::core::TimeInForce::PostOnly => "limit_limit_gtc",
                    _ => "limit_limit_gtc",
                };
                json!({
                    tif_key: {
                        "base_size": self.precision.qty(sym, quantity),
                        "limit_price": self.precision.price(sym, price),
                        "post_only": post_only,
                    }
                })
            }
            OrderType::PostOnly { price } => {
                json!({
                    "limit_limit_gtc": {
                        "base_size": self.precision.qty(sym, quantity),
                        "limit_price": self.precision.price(sym, price),
                        "post_only": true,
                    }
                })
            }
            OrderType::Ioc { price } => {
                let px_str = price.map(|p| self.precision.price(sym, p)).unwrap_or_else(|| "0".to_string());
                json!({
                    "limit_limit_ioc": {
                        "base_size": self.precision.qty(sym, quantity),
                        "limit_price": px_str,
                        "post_only": false,
                    }
                })
            }
            OrderType::Fok { price } => {
                json!({
                    "limit_limit_fok": {
                        "base_size": self.precision.qty(sym, quantity),
                        "limit_price": self.precision.price(sym, price),
                        "post_only": false,
                    }
                })
            }
            OrderType::StopMarket { stop_price } => {
                json!({
                    "stop_limit_stop_limit_gtc": {
                        "base_size": self.precision.qty(sym, quantity),
                        "limit_price": self.precision.price(sym, stop_price),
                        "stop_price": self.precision.price(sym, stop_price),
                        "stop_direction": match side {
                            OrderSide::Buy => "STOP_DIRECTION_STOP_UP",
                            OrderSide::Sell => "STOP_DIRECTION_STOP_DOWN",
                        },
                    }
                })
            }
            OrderType::StopLimit { stop_price, limit_price } => {
                json!({
                    "stop_limit_stop_limit_gtc": {
                        "base_size": self.precision.qty(sym, quantity),
                        "limit_price": self.precision.price(sym, limit_price),
                        "stop_price": self.precision.price(sym, stop_price),
                        "stop_direction": match side {
                            OrderSide::Buy => "STOP_DIRECTION_STOP_UP",
                            OrderSide::Sell => "STOP_DIRECTION_STOP_DOWN",
                        },
                    }
                })
            }
            OrderType::Gtd { price, expire_time } => {
                // Coinbase supports GTD via end_time parameter in limit_limit_gtd
                let end_time = chrono::DateTime::from_timestamp(expire_time / 1000, 0)
                    .map(|dt| dt.to_rfc3339())
                    .unwrap_or_default();
                json!({
                    "limit_limit_gtd": {
                        "base_size": self.precision.qty(sym, quantity),
                        "limit_price": self.precision.price(sym, price),
                        "end_time": end_time,
                        "post_only": false,
                    }
                })
            }
            OrderType::Oco { price, stop_price, stop_limit_price: _ } => {
                // Coinbase supports bracket orders: trigger_bracket_gtc
                json!({
                    "trigger_bracket_gtc": {
                        "base_size": self.precision.qty(sym, quantity),
                        "limit_price": self.precision.price(sym, price),
                        "stop_trigger_price": self.precision.price(sym, stop_price),
                    }
                })
            }
            OrderType::Bracket { price, take_profit, stop_loss } => {
                let px_str = price.map(|p| self.precision.price(sym, p)).unwrap_or_else(|| "0".to_string());
                let _ = take_profit;
                json!({
                    "trigger_bracket_gtc": {
                        "base_size": self.precision.qty(sym, quantity),
                        "limit_price": px_str,
                        "stop_trigger_price": self.precision.price(sym, stop_loss),
                    }
                })
            }
            OrderType::ReduceOnly { .. } | OrderType::TrailingStop { .. }
            | OrderType::Iceberg { .. } | OrderType::Twap { .. }
            | OrderType::Oto { .. } | OrderType::ConditionalPlan { .. }
            | OrderType::DcaRecurring { .. } => {
                return Err(ExchangeError::UnsupportedOperation(
                    format!("{:?} order type not supported on {:?}", req.order_type, self.exchange_id())
                ));
            }
        };

        let body = json!({
            "client_order_id": client_order_id,
            "product_id": product_id,
            "side": side_str,
            "order_configuration": order_config
        });

        let response = self.post(CoinbaseEndpoint::CreateOrder, body).await?;
        CoinbaseParser::parse_order(&response).map(PlaceOrderResponse::Simple)
    }

    async fn get_order_history(
        &self,
        filter: OrderHistoryFilter,
        account_type: AccountType,
    ) -> ExchangeResult<Vec<Order>> {
        // GET /orders/historical/batch with order_status=FILLED,CANCELLED
        let mut params = HashMap::new();
        params.insert("order_status".to_string(), "FILLED,CANCELLED,EXPIRED".to_string());

        if let Some(ref symbol) = filter.symbol {
            params.insert("product_id".to_string(), format_symbol(symbol, account_type));
        }

        if let Some(start) = filter.start_time {
            // Coinbase uses RFC3339 timestamps
            if let Some(dt) = chrono::DateTime::from_timestamp(start / 1000, 0) {
                params.insert("start_date".to_string(), dt.to_rfc3339());
            }
        }

        if let Some(end) = filter.end_time {
            if let Some(dt) = chrono::DateTime::from_timestamp(end / 1000, 0) {
                params.insert("end_date".to_string(), dt.to_rfc3339());
            }
        }

        if let Some(limit) = filter.limit {
            params.insert("limit".to_string(), limit.min(1000).to_string());
        }

        let query: Vec<String> = params.iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect();
        let query_str = format!("?{}", query.join("&"));

        let path = format!("{}{}", CoinbaseEndpoint::ListOrders.path(), query_str);
        let url = format!("{}{}", CoinbaseUrls::base_url(), path);

        let headers = self.auth.as_ref()
            .ok_or_else(|| ExchangeError::Auth("Authentication required".to_string()))?
            .sign_request("GET", &path)
            .map_err(ExchangeError::Auth)?;

        let response = self.http.get_with_headers(&url, &HashMap::new(), &headers).await?;

        let orders = response.get("orders")
            .and_then(|o| o.as_array())
            .ok_or_else(|| ExchangeError::Parse("Missing orders array".into()))?
            .iter()
            .filter_map(|order_json| {
                let order_obj = serde_json::json!({"order": order_json});
                CoinbaseParser::parse_order(&order_obj).ok()
            })
            .collect();

        Ok(orders)
    }

async fn cancel_order(&self, req: CancelRequest) -> ExchangeResult<Order> {
        match req.scope {
            CancelScope::Single { ref order_id } => {
                let symbol = req.symbol.as_ref()
                    .ok_or_else(|| ExchangeError::InvalidRequest("Symbol required for cancel".into()))?
                    .clone();
                let account_type = req.account_type;

                // Get order details before cancelling
                let order = self.get_order(&symbol.to_string(), order_id, account_type).await?;

                let body = json!({ "order_ids": [order_id] });
                let response = self.post(CoinbaseEndpoint::CancelOrders, body).await?;

                let results = response.get("results")
                    .and_then(|r| r.as_array())
                    .ok_or_else(|| ExchangeError::Parse("Missing results array".into()))?;

                let success = results.iter()
                    .any(|r| r.get("success").and_then(|s| s.as_bool()).unwrap_or(false));

                if success {
                    Ok(order)
                } else {
                    Err(ExchangeError::Api { code: 0, message: "Order cancellation failed".to_string() })
                }
            }
            CancelScope::All { ref symbol } => {
                let account_type = req.account_type;
                let sym_str = symbol.as_ref().map(|s| s.to_string()).unwrap_or_default();
                let open_orders = self.get_open_orders(
                    symbol.as_ref().map(|s| s.to_string()).as_deref(),
                    account_type,
                ).await?;

                if open_orders.is_empty() {
                    return Ok(Order {
                        id: format!("cancel_all_{}", crate::core::timestamp_millis()),
                        client_order_id: None,
                        symbol: sym_str,
                        side: OrderSide::Buy,
                        order_type: OrderType::Market,
                        status: crate::core::OrderStatus::Canceled,
                        price: None,
                        stop_price: None,
                        quantity: 0.0,
                        filled_quantity: 0.0,
                        average_price: None,
                        commission: None,
                        commission_asset: None,
                        created_at: 0,
                        updated_at: Some(crate::core::timestamp_millis() as i64),
                        time_in_force: crate::core::TimeInForce::Gtc,
                    });
                }

                let order_ids_vec: Vec<String> = open_orders.iter().map(|o| o.id.clone()).collect();
                let body = json!({ "order_ids": order_ids_vec });
                let response = self.post(CoinbaseEndpoint::CancelOrders, body).await?;
                let _ = response;

                Ok(Order {
                    id: format!("cancel_all_{}", crate::core::timestamp_millis()),
                    client_order_id: None,
                    symbol: symbol.as_ref().map(|s| s.to_string()).unwrap_or_default(),
                    side: OrderSide::Buy,
                    order_type: OrderType::Market,
                    status: crate::core::OrderStatus::Canceled,
                    price: None,
                    stop_price: None,
                    quantity: open_orders.len() as f64,
                    filled_quantity: 0.0,
                    average_price: None,
                    commission: None,
                    commission_asset: None,
                    created_at: 0,
                    updated_at: Some(crate::core::timestamp_millis() as i64),
                    time_in_force: crate::core::TimeInForce::Gtc,
                })
            }
            CancelScope::BySymbol { ref symbol } => {
                let account_type = req.account_type;
                let sym_str = symbol.to_string();
                let open_orders = self.get_open_orders(
                    Some(&sym_str),
                    account_type,
                ).await?;

                if open_orders.is_empty() {
                    return Ok(Order {
                        id: format!("cancel_all_{}", crate::core::timestamp_millis()),
                        client_order_id: None,
                        symbol: sym_str,
                        side: OrderSide::Buy,
                        order_type: OrderType::Market,
                        status: crate::core::OrderStatus::Canceled,
                        price: None,
                        stop_price: None,
                        quantity: 0.0,
                        filled_quantity: 0.0,
                        average_price: None,
                        commission: None,
                        commission_asset: None,
                        created_at: 0,
                        updated_at: Some(crate::core::timestamp_millis() as i64),
                        time_in_force: crate::core::TimeInForce::Gtc,
                    });
                }

                let order_ids_vec: Vec<String> = open_orders.iter().map(|o| o.id.clone()).collect();
                let body = json!({ "order_ids": order_ids_vec });
                let response = self.post(CoinbaseEndpoint::CancelOrders, body).await?;
                let _ = response;

                Ok(Order {
                    id: format!("cancel_all_{}", crate::core::timestamp_millis()),
                    client_order_id: None,
                    symbol: symbol.to_string(),
                    side: OrderSide::Buy,
                    order_type: OrderType::Market,
                    status: crate::core::OrderStatus::Canceled,
                    price: None,
                    stop_price: None,
                    quantity: open_orders.len() as f64,
                    filled_quantity: 0.0,
                    average_price: None,
                    commission: None,
                    commission_asset: None,
                    created_at: 0,
                    updated_at: Some(crate::core::timestamp_millis() as i64),
                    time_in_force: crate::core::TimeInForce::Gtc,
                })
            }
            CancelScope::Batch { ref order_ids } => {
                let symbol = req.symbol.as_ref()
                    .ok_or_else(|| ExchangeError::InvalidRequest("Symbol required for batch cancel".into()))?
                    .clone();
                let _account_type = req.account_type;

                // Coinbase supports batch cancel natively: POST /orders/batch_cancel
                let body = json!({ "order_ids": order_ids });
                let response = self.post(CoinbaseEndpoint::CancelOrders, body).await?;
                let _ = response;

                Ok(Order {
                    id: format!("batch_cancel_{}", crate::core::timestamp_millis()),
                    client_order_id: None,
                    symbol: symbol.to_string(),
                    side: OrderSide::Buy,
                    order_type: OrderType::Market,
                    status: crate::core::OrderStatus::Canceled,
                    price: None,
                    stop_price: None,
                    quantity: order_ids.len() as f64,
                    filled_quantity: 0.0,
                    average_price: None,
                    commission: None,
                    commission_asset: None,
                    created_at: 0,
                    updated_at: Some(crate::core::timestamp_millis() as i64),
                    time_in_force: crate::core::TimeInForce::Gtc,
                })
            }
            CancelScope::ByLabel(_)
            | CancelScope::ByCurrencyKind { .. }
            | CancelScope::ScheduledAt(_) => {
                return Err(ExchangeError::UnsupportedOperation(
                    "ByLabel/ByCurrencyKind/ScheduledAt cancel scopes not supported on Coinbase".into()
                ));
            }
        }
    }

    async fn get_order(
        &self,
        _symbol: &str,
        order_id: &str,
        _account_type: AccountType, // Not used, order_id is globally unique
    ) -> ExchangeResult<Order> {
        // Build path with order_id
        let endpoint = CoinbaseEndpoint::OrderDetails;
        let path = format!("{}/{}", endpoint.path(), order_id);

        let url = format!("{}{}", CoinbaseUrls::base_url(), path);

        let headers = self.auth.as_ref()
            .ok_or_else(|| ExchangeError::Auth("Authentication required".to_string()))?
            .sign_request("GET", &path)
            .map_err(ExchangeError::Auth)?;

        self.rate_limit_wait(1).await;
        let (response, resp_headers) = self.http.get_with_response_headers(&url, &HashMap::new(), &headers).await?;
        self.update_rate_from_headers(&resp_headers);
        CoinbaseParser::parse_order(&response)
    }

    async fn get_open_orders(
        &self,
        symbol: Option<&str>,
        account_type: AccountType,
    ) -> ExchangeResult<Vec<Order>> {
        // Convert Option<&str> to Option<Symbol>
        let symbol_str = symbol;
        let symbol: Option<crate::core::Symbol> = symbol_str.map(|s| {
            let parts: Vec<&str> = s.split('/').collect();
            if parts.len() == 2 {
                crate::core::Symbol::new(parts[0], parts[1])
            } else {
                crate::core::Symbol { base: s.to_string(), quote: String::new(), raw: Some(s.to_string()) }
            }
        });

        let mut params = HashMap::new();
        params.insert("order_status".to_string(), "OPEN".to_string());

        if let Some(s) = symbol {
            params.insert("product_id".to_string(), format_symbol(&s, account_type));
        }

        let query: Vec<String> = params.iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect();
        let query_str = format!("?{}", query.join("&"));

        let path = format!("{}{}", CoinbaseEndpoint::ListOrders.path(), query_str);
        let url = format!("{}{}", CoinbaseUrls::base_url(), path);

        let headers = self.auth.as_ref()
            .ok_or_else(|| ExchangeError::Auth("Authentication required".to_string()))?
            .sign_request("GET", &path)
            .map_err(ExchangeError::Auth)?;

        let response = self.http.get_with_headers(&url, &HashMap::new(), &headers).await?;

        let orders = response.get("orders")
            .and_then(|o| o.as_array())
            .ok_or_else(|| ExchangeError::Parse("Missing orders array".into()))?
            .iter()
            .filter_map(|order_json| {
                let order_obj = json!({"order": order_json});
                CoinbaseParser::parse_order(&order_obj).ok()
            })
            .collect();

        Ok(orders)
    }

    async fn get_user_trades(
        &self,
        filter: UserTradeFilter,
        _account_type: AccountType,
    ) -> ExchangeResult<Vec<UserTrade>> {
        // GET /orders/historical/fills — cursor-based pagination, fetch first page
        let mut params: Vec<String> = Vec::new();

        if let Some(ref product_id) = filter.symbol {
            params.push(format!("product_id={}", product_id));
        }

        if let Some(ref order_id) = filter.order_id {
            params.push(format!("order_id={}", order_id));
        }

        if let Some(start) = filter.start_time {
            if let Some(dt) = chrono::DateTime::from_timestamp((start / 1000) as i64, 0) {
                params.push(format!("start_sequence_timestamp={}", dt.to_rfc3339()));
            }
        }

        if let Some(end) = filter.end_time {
            if let Some(dt) = chrono::DateTime::from_timestamp((end / 1000) as i64, 0) {
                params.push(format!("end_sequence_timestamp={}", dt.to_rfc3339()));
            }
        }

        // Coinbase fills endpoint max is 100 per page
        let limit = filter.limit.unwrap_or(100).min(100);
        params.push(format!("limit={}", limit));

        let query_str = if params.is_empty() {
            String::new()
        } else {
            format!("?{}", params.join("&"))
        };

        let path = format!("{}{}", CoinbaseEndpoint::ListFills.path(), query_str);
        let url = format!("{}{}", CoinbaseUrls::base_url(), path);

        let headers = self.auth.as_ref()
            .ok_or_else(|| ExchangeError::Auth("Authentication required".to_string()))?
            .sign_request("GET", &path)
            .map_err(ExchangeError::Auth)?;

        let response = self.http.get_with_headers(&url, &HashMap::new(), &headers).await?;

        CoinbaseParser::parse_fills(&response)
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// ACCOUNT
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl Account for CoinbaseConnector {
    async fn get_balance(&self, query: BalanceQuery) -> ExchangeResult<Vec<Balance>> {
        let _asset = query.asset;
        let _account_type = query.account_type;
        let response = self.get(CoinbaseEndpoint::Accounts, HashMap::new()).await?;
        CoinbaseParser::parse_balance(&response)
    }

    async fn get_account_info(&self, account_type: AccountType) -> ExchangeResult<AccountInfo> {
        // Get transaction summary for fee tier info
        let response = self.get(CoinbaseEndpoint::TransactionSummary, HashMap::new()).await?;

        let maker_commission = response.get("fee_tier")
            .and_then(|ft| ft.get("maker_fee_rate"))
            .and_then(|mfr| mfr.as_str())
            .and_then(|s| s.parse::<f64>().ok())
            .unwrap_or(0.0);

        let taker_commission = response.get("fee_tier")
            .and_then(|ft| ft.get("taker_fee_rate"))
            .and_then(|tfr| tfr.as_str())
            .and_then(|s| s.parse::<f64>().ok())
            .unwrap_or(0.0);

        // Get balances
        let balances = self.get_balance(BalanceQuery { asset: None, account_type }).await?;

        Ok(AccountInfo {
            account_type,
            can_trade: true,
            can_withdraw: true,
            can_deposit: true,
            maker_commission,
            taker_commission,
            balances,
        })
    }

    async fn get_fees(&self, symbol: Option<&str>) -> ExchangeResult<FeeInfo> {
        // GET /transaction_summary returns fee tier info
        let response = self.get(CoinbaseEndpoint::TransactionSummary, HashMap::new()).await?;

        let maker_rate = response.get("fee_tier")
            .and_then(|ft| ft.get("maker_fee_rate"))
            .and_then(|v| v.as_str())
            .and_then(|s| s.parse::<f64>().ok())
            .unwrap_or(0.006);

        let taker_rate = response.get("fee_tier")
            .and_then(|ft| ft.get("taker_fee_rate"))
            .and_then(|v| v.as_str())
            .and_then(|s| s.parse::<f64>().ok())
            .unwrap_or(0.008);

        let tier = response.get("fee_tier")
            .and_then(|ft| ft.get("pricing_tier"))
            .and_then(|v| v.as_str())
            .map(String::from);

        Ok(FeeInfo {
            maker_rate,
            taker_rate,
            symbol: symbol.map(String::from),
            tier,
        })
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// POSITIONS (Not supported by Coinbase)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl Positions for CoinbaseConnector {
    async fn get_positions(&self, query: PositionQuery) -> ExchangeResult<Vec<Position>> {
        let _symbol = query.symbol.clone();
        let _account_type = query.account_type;

        Err(ExchangeError::NotSupported("Coinbase does not support futures/positions".to_string()))
    
    }

    async fn get_funding_rate(
        &self,
        _symbol: &str,
        _account_type: AccountType,
    ) -> ExchangeResult<FundingRate> {
        // Parse symbol string into Symbol struct
        let _symbol_str = _symbol;
        let _symbol = {
            let parts: Vec<&str> = _symbol_str.split('/').collect();
            if parts.len() == 2 {
                crate::core::Symbol::new(parts[0], parts[1])
            } else {
                crate::core::Symbol { base: _symbol_str.to_string(), quote: String::new(), raw: Some(_symbol_str.to_string()) }
            }
        };

        Err(ExchangeError::NotSupported("Coinbase does not support funding rates".to_string()))
    
    }

    async fn modify_position(&self, req: PositionModification) -> ExchangeResult<()> {
        match req {
            PositionModification::SetLeverage { symbol: ref _symbol, leverage: _leverage, account_type: _account_type } => {
                let _symbol = _symbol.clone();

                Err(ExchangeError::NotSupported("Coinbase does not support leverage".to_string()))
    
            }
            _ => Err(ExchangeError::UnsupportedOperation(
                format!("{:?} not supported on {:?}", req, self.exchange_id())
            )),
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// CANCEL ALL (optional trait)
// ═══════════════════════════════════════════════════════════════════════════════

#[async_trait]
impl CancelAll for CoinbaseConnector {
    /// Cancel all open orders, optionally filtered to a single symbol.
    ///
    /// Coinbase has no single "cancel all" endpoint — implementation is 2-step:
    /// 1. Fetch all open orders (optionally filtered by symbol).
    /// 2. Call `POST /orders/batch_cancel` in chunks of 100.
    async fn cancel_all_orders(
        &self,
        scope: CancelScope,
        account_type: AccountType,
    ) -> ExchangeResult<CancelAllResponse> {
        let symbol_filter = match &scope {
            CancelScope::All { symbol } => symbol.as_ref().map(|s| s.to_string()),
            CancelScope::BySymbol { symbol } => Some(symbol.to_string()),
            _ => return Err(ExchangeError::UnsupportedOperation(
                format!("{:?} not supported in cancel_all_orders", scope)
            )),
        };

        // Step 1: fetch open orders
        let open_orders = self.get_open_orders(
            symbol_filter.as_deref(),
            account_type,
        ).await?;

        if open_orders.is_empty() {
            return Ok(CancelAllResponse {
                cancelled_count: 0,
                failed_count: 0,
                details: vec![],
            });
        }

        let order_ids: Vec<String> = open_orders.iter().map(|o| o.id.clone()).collect();

        // Step 2: batch cancel in chunks of 100 (Coinbase limit)
        let mut cancelled_count = 0u32;
        let mut failed_count = 0u32;
        let mut details: Vec<OrderResult> = Vec::new();

        for chunk in order_ids.chunks(100) {
            let body = serde_json::json!({ "order_ids": chunk });
            let response = self.post(CoinbaseEndpoint::CancelOrders, body).await?;

            if let Some(results) = response.get("results").and_then(|r| r.as_array()) {
                for item in results {
                    let success = item.get("success")
                        .and_then(|s| s.as_bool())
                        .unwrap_or(false);
                    let order_id = item.get("order_id")
                        .and_then(|v| v.as_str())
                        .unwrap_or("")
                        .to_string();
                    let failure_reason = item.get("failure_reason")
                        .and_then(|v| v.as_str())
                        .filter(|s| !s.is_empty())
                        .map(|s| s.to_string());

                    if success {
                        cancelled_count += 1;
                    } else {
                        failed_count += 1;
                    }

                    details.push(OrderResult {
                        order: None,
                        client_order_id: None,
                        success,
                        error: failure_reason,
                        error_code: None,
                    });
                    let _ = order_id;
                }
            }
        }

        Ok(CancelAllResponse {
            cancelled_count,
            failed_count,
            details,
        })
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// CUSTODIAL FUNDS
// ═══════════════════════════════════════════════════════════════════════════════

/// Deposit and withdrawal management for Coinbase.
///
/// Uses the Coinbase v2 API endpoints which operate on per-asset account UUIDs.
/// The account UUID is resolved automatically via the `/accounts` endpoint.
///
/// - Deposit address: `POST /v2/accounts/{id}/addresses`
/// - Withdraw:        `POST /v2/accounts/{id}/transactions` (type=send)
/// - Deposit history: `GET  /v2/accounts/{id}/deposits`
/// - Withdrawal hist: `GET  /v2/accounts/{id}/transactions` (type=send)
#[async_trait]
impl CustodialFunds for CoinbaseConnector {
    async fn get_deposit_address(
        &self,
        asset: &str,
        _network: Option<&str>,
    ) -> ExchangeResult<DepositAddress> {
        // Resolve the per-asset account UUID first
        let account_id = self.find_account_id(asset).await?;
        let path = format!("/accounts/{}/addresses", account_id);

        let response = self.post_v2(&path, serde_json::json!({})).await?;
        CoinbaseParser::parse_deposit_address(&response, asset)
    }

    async fn withdraw(&self, req: WithdrawRequest) -> ExchangeResult<WithdrawResponse> {
        let account_id = self.find_account_id(&req.asset).await?;
        let path = format!("/accounts/{}/transactions", account_id);

        let mut body = serde_json::json!({
            "type": "send",
            "to": req.address,
            "amount": req.amount.to_string(),
            "currency": req.asset.to_uppercase(),
        });

        // Add destination tag / memo if present (required for XRP, XLM, etc.)
        if let Some(ref tag) = req.tag {
            body["destination_tag"] = serde_json::json!(tag);
        }

        // Network hint — Coinbase uses the network field for certain assets
        if let Some(ref network) = req.network {
            body["network"] = serde_json::json!(network);
        }

        let response = self.post_v2(&path, body).await?;
        CoinbaseParser::parse_withdraw_response(&response)
    }

    async fn get_funds_history(
        &self,
        filter: FundsHistoryFilter,
    ) -> ExchangeResult<Vec<FundsRecord>> {
        let asset = filter.asset.as_deref().unwrap_or("USD");
        let account_id = self.find_account_id(asset).await?;

        match filter.record_type {
            FundsRecordType::Deposit => {
                let mut params = HashMap::new();
                if let Some(limit) = filter.limit {
                    params.insert("limit".to_string(), limit.to_string());
                }
                let path = format!("/accounts/{}/deposits", account_id);
                let response = self.get_v2(&path, params).await?;
                CoinbaseParser::parse_deposit_history(&response, asset)
            }

            FundsRecordType::Withdrawal => {
                let mut params = HashMap::new();
                if let Some(limit) = filter.limit {
                    params.insert("limit".to_string(), limit.to_string());
                }
                let path = format!("/accounts/{}/transactions", account_id);
                let response = self.get_v2(&path, params).await?;
                CoinbaseParser::parse_withdrawal_history(&response, asset)
            }

            FundsRecordType::Both => {
                // Fetch deposits and outgoing transactions, combine them
                let dep_path = format!("/accounts/{}/deposits", account_id);
                let txn_path = format!("/accounts/{}/transactions", account_id);

                let mut params = HashMap::new();
                if let Some(limit) = filter.limit {
                    params.insert("limit".to_string(), limit.to_string());
                }

                let dep_response = self.get_v2(&dep_path, params.clone()).await?;
                let txn_response = self.get_v2(&txn_path, params).await?;

                let mut records = CoinbaseParser::parse_deposit_history(&dep_response, asset)?;
                records.extend(CoinbaseParser::parse_withdrawal_history(&txn_response, asset)?);
                Ok(records)
            }
        }
    }
}

// ═══════════════════════════════════════════════════════════════════════════════
// EXTENDED METHODS (not part of core traits)
// ═══════════════════════════════════════════════════════════════════════════════

impl CoinbaseConnector {
    /// Get fill history — paginated list of all fills for completed orders.
    ///
    /// `GET /api/v3/brokerage/orders/historical/fills`
    ///
    /// # Parameters
    /// - `order_id`: Filter by order ID (optional)
    /// - `product_id`: Filter by product/symbol (optional)
    /// - `limit`: Max number of fills to return (optional, max 100)
    /// - `cursor`: Pagination cursor from a previous response (optional)
    pub async fn get_fill_history(
        &self,
        order_id: Option<&str>,
        product_id: Option<&str>,
        limit: Option<u32>,
        cursor: Option<&str>,
    ) -> ExchangeResult<Value> {
        let mut params = HashMap::new();
        if let Some(oid) = order_id {
            params.insert("order_id".to_string(), oid.to_string());
        }
        if let Some(pid) = product_id {
            params.insert("product_id".to_string(), pid.to_string());
        }
        if let Some(l) = limit {
            params.insert("limit".to_string(), l.to_string());
        }
        if let Some(c) = cursor {
            params.insert("cursor".to_string(), c.to_string());
        }
        self.get(CoinbaseEndpoint::FillHistory, params).await
    }
}

fn interval_to_secs(interval: &str) -> u64 {
    match interval {
        "1m" => 60,
        "5m" => 300,
        "15m" => 900,
        "30m" => 1800,
        "1h" => 3600,
        "4h" => 14400,
        "12h" => 43200,
        "1d" => 86400,
        "1w" => 604800,
        _ => 3600,
    }
}