polymarket-rs-sdk 0.1.14

Rust SDK for Polymarket prediction markets - REST APIs, WebSocket streams, order signing, and Safe wallet integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
//! Common types for Polymarket SDK
//!
//! This module contains shared types used across different API clients,
//! including trading types, market data structures, and authentication types.

#[cfg(feature = "auth")]
use alloy_primitives::U256;
use chrono::{DateTime, Utc};
use rust_decimal::Decimal;
use serde::{de, Deserialize, Deserializer, Serialize};
use serde_json::Value;
use std::str::FromStr;

// ============================================================================
// Trading Types
// ============================================================================

/// Trading side for orders
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum Side {
    Buy,
    Sell,
}

impl Side {
    /// Get string representation
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Buy => "BUY",
            Self::Sell => "SELL",
        }
    }

    /// Get opposite side
    #[must_use]
    pub fn opposite(&self) -> Self {
        match self {
            Self::Buy => Self::Sell,
            Self::Sell => Self::Buy,
        }
    }
}

/// Order book level (price/size pair)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BookLevel {
    #[serde(with = "rust_decimal::serde::str")]
    pub price: Decimal,
    #[serde(with = "rust_decimal::serde::str")]
    pub size: Decimal,
}

// ============================================================================
// Authentication Types
// ============================================================================

/// API credentials for Polymarket authentication
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ApiCredentials {
    /// API key
    #[serde(rename = "apiKey")]
    pub api_key: String,
    /// API secret (base64 encoded)
    pub secret: String,
    /// API passphrase
    pub passphrase: String,
}

impl ApiCredentials {
    /// Create new API credentials
    #[must_use]
    pub fn new(
        api_key: impl Into<String>,
        secret: impl Into<String>,
        passphrase: impl Into<String>,
    ) -> Self {
        Self {
            api_key: api_key.into(),
            secret: secret.into(),
            passphrase: passphrase.into(),
        }
    }

    /// Check if credentials are configured
    #[must_use]
    pub fn is_configured(&self) -> bool {
        !self.api_key.is_empty() && !self.secret.is_empty() && !self.passphrase.is_empty()
    }
}

// ============================================================================
// Balance & Allowance Types
// ============================================================================

/// Asset type for balance/allowance queries
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum AssetType {
    /// USDC collateral token
    Collateral,
    /// Conditional outcome token (requires token_id)
    Conditional,
}

impl AssetType {
    /// Get string representation for API query parameter
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Collateral => "COLLATERAL",
            Self::Conditional => "CONDITIONAL",
        }
    }
}

impl std::fmt::Display for AssetType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

/// Parameters for balance/allowance queries
///
/// Used by [`ClobClient::get_balance_allowance`] and [`ClobClient::update_balance_allowance`].
#[derive(Debug, Clone)]
pub struct BalanceAllowanceParams {
    /// The type of asset to query
    pub asset_type: AssetType,
    /// Token ID (required when `asset_type` is `Conditional`)
    pub token_id: Option<String>,
    /// Signature type override (0=EOA, 1=PolyProxy, 2=PolyGnosisSafe).
    /// If `None`, defaults to the client's configured signature type.
    pub signature_type: Option<i32>,
}

impl BalanceAllowanceParams {
    /// Create params for querying USDC collateral balance/allowance
    #[must_use]
    pub fn collateral() -> Self {
        Self {
            asset_type: AssetType::Collateral,
            token_id: None,
            signature_type: None,
        }
    }

    /// Create params for querying a conditional token balance/allowance
    #[must_use]
    pub fn conditional(token_id: impl Into<String>) -> Self {
        Self {
            asset_type: AssetType::Conditional,
            token_id: Some(token_id.into()),
            signature_type: None,
        }
    }

    /// Set signature type override
    #[must_use]
    pub fn with_signature_type(mut self, sig_type: i32) -> Self {
        self.signature_type = Some(sig_type);
        self
    }

    /// Build query string for the API URL
    pub(crate) fn to_query_string(&self) -> String {
        let mut params = vec![format!("asset_type={}", self.asset_type.as_str())];
        if let Some(ref token_id) = self.token_id {
            params.push(format!("token_id={}", token_id));
        }
        if let Some(sig_type) = self.signature_type {
            params.push(format!("signature_type={}", sig_type));
        }
        params.join("&")
    }
}

/// Response from balance/allowance endpoints
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BalanceAllowanceResponse {
    /// Current balance for the specified asset
    pub balance: String,
    /// Current allowance for the specified asset
    pub allowance: String,
}

// ============================================================================
// Order Types
// ============================================================================

/// Configuration options for order creation
#[derive(Debug, Clone, Default)]
pub struct OrderOptions {
    /// Tick size for price rounding
    pub tick_size: Option<Decimal>,
    /// Whether to use negative risk contracts
    pub neg_risk: Option<bool>,
    /// Fee rate in basis points
    pub fee_rate_bps: Option<u32>,
}

impl OrderOptions {
    /// Create new order options
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set tick size
    #[must_use]
    pub fn with_tick_size(mut self, tick_size: Decimal) -> Self {
        self.tick_size = Some(tick_size);
        self
    }

    /// Set negative risk flag
    #[must_use]
    pub fn with_neg_risk(mut self, neg_risk: bool) -> Self {
        self.neg_risk = Some(neg_risk);
        self
    }

    /// Set fee rate in basis points
    #[must_use]
    pub fn with_fee_rate_bps(mut self, fee_rate_bps: u32) -> Self {
        self.fee_rate_bps = Some(fee_rate_bps);
        self
    }
}

/// Extra arguments for order creation
#[cfg(feature = "auth")]
#[derive(Debug, Clone)]
pub struct ExtraOrderArgs {
    /// Fee rate in basis points
    pub fee_rate_bps: u32,
    /// Nonce for replay protection
    pub nonce: U256,
    /// Taker address (usually zero address)
    pub taker: String,
}

#[cfg(feature = "auth")]
impl Default for ExtraOrderArgs {
    fn default() -> Self {
        Self {
            fee_rate_bps: 0,
            nonce: U256::ZERO,
            taker: "0x0000000000000000000000000000000000000000".to_string(),
        }
    }
}

#[cfg(feature = "auth")]
impl ExtraOrderArgs {
    /// Create new extra order args
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set fee rate in basis points
    #[must_use]
    pub fn with_fee_rate_bps(mut self, fee_rate_bps: u32) -> Self {
        self.fee_rate_bps = fee_rate_bps;
        self
    }

    /// Set nonce
    #[must_use]
    pub fn with_nonce(mut self, nonce: U256) -> Self {
        self.nonce = nonce;
        self
    }

    /// Set taker address
    #[must_use]
    pub fn with_taker(mut self, taker: impl Into<String>) -> Self {
        self.taker = taker.into();
        self
    }
}

/// Market order arguments
#[derive(Debug, Clone)]
pub struct MarketOrderArgs {
    /// Token ID (condition token)
    pub token_id: String,
    /// Amount to trade
    pub amount: Decimal,
}

impl MarketOrderArgs {
    /// Create new market order args
    #[must_use]
    pub fn new(token_id: impl Into<String>, amount: Decimal) -> Self {
        Self {
            token_id: token_id.into(),
            amount,
        }
    }
}

/// Signed order request ready for submission
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SignedOrderRequest {
    /// Random salt for uniqueness
    pub salt: u64,
    /// Maker/funder address
    pub maker: String,
    /// Signer address
    pub signer: String,
    /// Taker address (usually zero)
    pub taker: String,
    /// Token ID
    pub token_id: String,
    /// Maker amount in token units
    pub maker_amount: String,
    /// Taker amount in token units
    pub taker_amount: String,
    /// Expiration timestamp
    pub expiration: String,
    /// Nonce for replay protection
    pub nonce: String,
    /// Fee rate in basis points
    pub fee_rate_bps: String,
    /// Order side (BUY/SELL)
    pub side: String,
    /// Signature type
    pub signature_type: u8,
    /// EIP-712 signature
    pub signature: String,
}

/// Order type for CLOB orders
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
pub enum OrderType {
    /// Good Till Cancelled (default for limit orders)
    GTC,
    /// Fill Or Kill (for market orders)
    FOK,
    /// Good Till Date
    GTD,
    /// Fill And Kill
    FAK,
}

impl Default for OrderType {
    fn default() -> Self {
        OrderType::GTC
    }
}

/// NewOrder is the payload structure for posting orders to the Polymarket API
/// It wraps order data with orderType, owner, and deferExec fields
/// IMPORTANT: Field order MUST match TypeScript SDK for HMAC signature compatibility
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NewOrder {
    /// Whether to defer execution (MUST be first field for JSON field order)
    #[serde(default)]
    pub defer_exec: bool,
    /// The order data
    pub order: NewOrderData,
    /// Owner - should be the API key, NOT the wallet address
    pub owner: String,
    /// Order type (GTC, FOK, etc.)
    pub order_type: OrderType,
}

/// NewOrderData contains the actual order fields
/// Note: salt must be a number (i64) in JSON, not a string
/// IMPORTANT: Field order MUST match TypeScript SDK for HMAC signature compatibility
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NewOrderData {
    /// Random salt for uniqueness - MUST be a number in JSON
    pub salt: i64,
    /// Maker/funder address
    pub maker: String,
    /// Signer address
    pub signer: String,
    /// Taker address (usually zero)
    pub taker: String,
    /// Token ID
    pub token_id: String,
    /// Maker amount in token units
    pub maker_amount: String,
    /// Taker amount in token units
    pub taker_amount: String,
    /// Order side (BUY/SELL) - MUST come after takerAmount, before expiration
    pub side: String,
    /// Expiration timestamp
    pub expiration: String,
    /// Nonce for replay protection
    pub nonce: String,
    /// Fee rate in basis points
    pub fee_rate_bps: String,
    /// Signature type (0=EOA, 1=PolyProxy, 2=PolyGnosisSafe)
    pub signature_type: u8,
    /// EIP-712 signature
    pub signature: String,
}

impl NewOrder {
    /// Convert SignedOrderRequest to NewOrder format for API submission
    /// Field initialization order matches struct field order for consistency
    pub fn from_signed_order(
        order: &SignedOrderRequest,
        api_key: &str,
        order_type: OrderType,
        defer_exec: bool,
    ) -> Self {
        NewOrder {
            defer_exec,
            order: NewOrderData {
                // Salt must be i64 for JSON serialization as number
                salt: order.salt as i64,
                maker: order.maker.clone(),
                signer: order.signer.clone(),
                taker: order.taker.clone(),
                token_id: order.token_id.clone(),
                maker_amount: order.maker_amount.clone(),
                taker_amount: order.taker_amount.clone(),
                side: order.side.clone(),
                expiration: order.expiration.clone(),
                nonce: order.nonce.clone(),
                fee_rate_bps: order.fee_rate_bps.clone(),
                signature_type: order.signature_type,
                signature: order.signature.clone(),
            },
            owner: api_key.to_string(),
            order_type,
        }
    }
}

// ============================================================================
// Market Types
// ============================================================================

/// Market token information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Token {
    /// Token ID (condition token)
    pub token_id: String,
    /// Outcome name (e.g., "Yes", "No")
    pub outcome: String,
    /// Current price if available
    #[serde(skip_serializing_if = "Option::is_none")]
    #[serde(default, deserialize_with = "deserialize_decimal_opt")]
    pub price: Option<Decimal>,
}

/// Market information from Gamma API
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Market {
    /// Condition ID (market identifier)
    pub condition_id: String,
    /// Market slug for URL
    pub slug: String,
    /// Market question/title
    #[serde(default)]
    pub question: Option<String>,
    /// Market description
    #[serde(default)]
    pub description: Option<String>,
    /// Category/tag
    #[serde(default)]
    pub category: Option<String>,
    /// Whether market is active
    pub active: bool,
    /// Whether market is closed
    pub closed: bool,
    /// Market end date
    #[serde(default)]
    pub end_date: Option<String>,
    /// Market icon URL
    #[serde(default)]
    pub icon: Option<String>,
    /// CLOB token IDs (JSON string array)
    #[serde(default)]
    pub clob_token_ids: Option<String>,
    /// Outcomes (JSON string array)
    #[serde(default)]
    pub outcomes: Option<String>,
    /// Outcome prices (JSON string array, e.g. "[\"0.95\", \"0.05\"]")
    #[serde(default)]
    pub outcome_prices: Option<String>,
    /// Liquidity
    #[serde(default, deserialize_with = "deserialize_decimal_opt")]
    pub liquidity_num: Option<Decimal>,
    /// 24-hour volume
    #[serde(
        default,
        rename = "volume24hr",
        deserialize_with = "deserialize_decimal_opt"
    )]
    pub volume_24hr: Option<Decimal>,
    /// Total volume
    #[serde(default, deserialize_with = "deserialize_decimal_opt")]
    pub volume_num: Option<Decimal>,
    /// Minimum order size
    #[serde(default, deserialize_with = "deserialize_decimal_opt")]
    pub order_min_size: Option<Decimal>,
    /// Price tick size
    #[serde(
        default,
        rename = "orderPriceMinTickSize",
        deserialize_with = "deserialize_decimal_opt"
    )]
    pub order_tick_size: Option<Decimal>,
}

impl Market {
    /// Parse CLOB token IDs from JSON string
    #[must_use]
    pub fn parse_token_ids(&self) -> Vec<String> {
        self.clob_token_ids
            .as_ref()
            .and_then(|raw| serde_json::from_str(raw).ok())
            .unwrap_or_default()
    }

    /// Parse outcomes from JSON string
    #[must_use]
    pub fn parse_outcomes(&self) -> Vec<String> {
        self.outcomes
            .as_ref()
            .and_then(|raw| serde_json::from_str(raw).ok())
            .unwrap_or_else(|| vec!["Yes".to_string(), "No".to_string()])
    }

    /// Parse outcome prices from JSON string
    /// Returns (yes_price, no_price) as `Option<f64>` values
    #[must_use]
    pub fn parse_outcome_prices(&self) -> (Option<f64>, Option<f64>) {
        let prices: Vec<String> = self
            .outcome_prices
            .as_ref()
            .and_then(|raw| serde_json::from_str(raw).ok())
            .unwrap_or_default();

        let yes_price = prices.first().and_then(|s| s.parse::<f64>().ok());
        let no_price = prices.get(1).and_then(|s| s.parse::<f64>().ok());

        (yes_price, no_price)
    }
}

/// Deserialize Option<Decimal> from string/number/null.
fn deserialize_decimal_opt<'de, D>(deserializer: D) -> Result<Option<Decimal>, D::Error>
where
    D: Deserializer<'de>,
{
    match Value::deserialize(deserializer)? {
        Value::Null => Ok(None),
        Value::String(s) => {
            if s.is_empty() {
                Ok(None)
            } else {
                Decimal::from_str(&s).map(Some).map_err(de::Error::custom)
            }
        }
        Value::Number(n) => Decimal::from_str(&n.to_string())
            .map(Some)
            .map_err(de::Error::custom),
        other => Err(de::Error::custom(format!("expected decimal, got {other}"))),
    }
}

/// Event metadata from Gamma API
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Event {
    /// Event ID
    pub id: String,
    /// Event ticker
    #[serde(default)]
    pub ticker: Option<String>,
    /// Event slug
    pub slug: String,
    /// Event title
    #[serde(default)]
    pub title: Option<String>,
    /// Event description
    #[serde(default)]
    pub description: Option<String>,
    /// Resolution source URL or description
    #[serde(default)]
    pub resolution_source: Option<String>,
    /// Start date (ISO format)
    #[serde(default)]
    pub start_date: Option<String>,
    /// Creation date (ISO format)
    #[serde(default)]
    pub creation_date: Option<String>,
    /// End date (ISO format)
    #[serde(default)]
    pub end_date: Option<String>,
    /// Event image URL
    #[serde(default)]
    pub image: Option<String>,
    /// Event icon URL
    #[serde(default)]
    pub icon: Option<String>,
    /// Whether event is active
    #[serde(default)]
    pub active: Option<bool>,
    /// Whether event is closed
    #[serde(default)]
    pub closed: Option<bool>,
    /// Whether event is archived
    #[serde(default)]
    pub archived: Option<bool>,
    /// Whether event is new
    #[serde(default)]
    pub new: Option<bool>,
    /// Whether event is featured
    #[serde(default)]
    pub featured: Option<bool>,
    /// Whether event is restricted
    #[serde(default)]
    pub restricted: Option<bool>,
    /// Total liquidity
    #[serde(default)]
    pub liquidity: Option<f64>,
    /// Total volume
    #[serde(default)]
    pub volume: Option<f64>,
    /// Open interest
    #[serde(default)]
    pub open_interest: Option<f64>,
    /// Sort by field
    #[serde(default)]
    pub sort_by: Option<String>,
    /// Created at timestamp
    #[serde(default)]
    pub created_at: Option<String>,
    /// Updated at timestamp
    #[serde(default)]
    pub updated_at: Option<String>,
    /// Competitive score
    #[serde(default)]
    pub competitive: Option<f64>,
    /// 24-hour volume
    #[serde(default)]
    pub volume24hr: Option<f64>,
    /// 1-week volume
    #[serde(default)]
    pub volume1wk: Option<f64>,
    /// 1-month volume
    #[serde(default)]
    pub volume1mo: Option<f64>,
    /// 1-year volume
    #[serde(default)]
    pub volume1yr: Option<f64>,
    /// Whether order book is enabled
    #[serde(default)]
    pub enable_order_book: Option<bool>,
    /// CLOB liquidity
    #[serde(default)]
    pub liquidity_clob: Option<f64>,
    /// Whether negative risk is enabled
    #[serde(default)]
    pub neg_risk: Option<bool>,
    /// Negative risk market ID
    #[serde(default)]
    pub neg_risk_market_id: Option<String>,
    /// Comment count
    #[serde(default)]
    pub comment_count: Option<i32>,
    /// Associated markets
    #[serde(default)]
    pub markets: Vec<EventMarket>,
    /// Associated tags
    #[serde(default)]
    pub tags: Vec<EventTag>,
    /// Whether this is a CYOM (Create Your Own Market)
    #[serde(default)]
    pub cyom: Option<bool>,
    /// Whether to show all outcomes
    #[serde(default)]
    pub show_all_outcomes: Option<bool>,
    /// Whether to show market images
    #[serde(default)]
    pub show_market_images: Option<bool>,
    /// Whether negative risk is enabled for this event
    #[serde(default)]
    pub enable_neg_risk: Option<bool>,
    /// Whether automatically active
    #[serde(default)]
    pub automatically_active: Option<bool>,
    /// GMP chart mode
    #[serde(default)]
    pub gmp_chart_mode: Option<String>,
    /// Whether negative risk is augmented
    #[serde(default)]
    pub neg_risk_augmented: Option<bool>,
    /// Whether markets are cumulative
    #[serde(default)]
    pub cumulative_markets: Option<bool>,
    /// Whether pending deployment
    #[serde(default)]
    pub pending_deployment: Option<bool>,
    /// Whether currently deploying
    #[serde(default)]
    pub deploying: Option<bool>,
    /// Deploying timestamp
    #[serde(default)]
    pub deploying_timestamp: Option<String>,
    /// Whether requires translation
    #[serde(default)]
    pub requires_translation: Option<bool>,
}

/// Tag associated with an event
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventTag {
    /// Tag ID
    #[serde(default)]
    pub id: Option<String>,
    /// Tag label (display name)
    #[serde(default)]
    pub label: Option<String>,
    /// Tag slug
    #[serde(default)]
    pub slug: Option<String>,
    /// Whether to force show
    #[serde(default)]
    pub force_show: Option<bool>,
    /// Whether to force hide
    #[serde(default)]
    pub force_hide: Option<bool>,
    /// Published at timestamp
    #[serde(default)]
    pub published_at: Option<String>,
    /// Updated by user ID
    #[serde(default)]
    pub updated_by: Option<i32>,
    /// Created at timestamp
    #[serde(default)]
    pub created_at: Option<String>,
    /// Updated at timestamp
    #[serde(default)]
    pub updated_at: Option<String>,
    /// Whether requires translation
    #[serde(default)]
    pub requires_translation: Option<bool>,
}

/// Market info within an event (full details from events endpoint)
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EventMarket {
    /// Market ID
    #[serde(default)]
    pub id: Option<String>,
    /// Market question
    #[serde(default)]
    pub question: Option<String>,
    /// Condition ID
    pub condition_id: String,
    /// Market slug
    #[serde(default)]
    pub slug: Option<String>,
    /// End date (ISO format)
    #[serde(default)]
    pub end_date: Option<String>,
    /// Start date (ISO format)
    #[serde(default)]
    pub start_date: Option<String>,
    /// Liquidity (string format from API)
    #[serde(default)]
    pub liquidity: Option<String>,
    /// Market image URL
    #[serde(default)]
    pub image: Option<String>,
    /// Market icon URL
    #[serde(default)]
    pub icon: Option<String>,
    /// Market description
    #[serde(default)]
    pub description: Option<String>,
    /// Outcomes JSON string (e.g., "[\"Yes\", \"No\"]")
    #[serde(default)]
    pub outcomes: Option<String>,
    /// Outcome prices JSON string (e.g., "[\"0.5\", \"0.5\"]")
    #[serde(default)]
    pub outcome_prices: Option<String>,
    /// Volume (string format from API)
    #[serde(default)]
    pub volume: Option<String>,
    /// Whether market is active
    #[serde(default)]
    pub active: Option<bool>,
    /// Whether market is closed
    #[serde(default)]
    pub closed: Option<bool>,
    /// Market maker address
    #[serde(default)]
    pub market_maker_address: Option<String>,
    /// Created at timestamp
    #[serde(default)]
    pub created_at: Option<String>,
    /// Updated at timestamp
    #[serde(default)]
    pub updated_at: Option<String>,
    /// Whether market is new
    #[serde(default)]
    pub new: Option<bool>,
    /// Whether market is featured
    #[serde(default)]
    pub featured: Option<bool>,
    /// Submitted by address
    #[serde(default)]
    pub submitted_by: Option<String>,
    /// Whether market is archived
    #[serde(default)]
    pub archived: Option<bool>,
    /// Resolved by address
    #[serde(default)]
    pub resolved_by: Option<String>,
    /// Whether market is restricted
    #[serde(default)]
    pub restricted: Option<bool>,
    /// Group item title (for multi-outcome events)
    #[serde(default)]
    pub group_item_title: Option<String>,
    /// Group item threshold
    #[serde(default)]
    pub group_item_threshold: Option<String>,
    /// Question ID (for negative risk markets)
    #[serde(default, rename = "questionID")]
    pub question_id: Option<String>,
    /// Whether order book is enabled
    #[serde(default)]
    pub enable_order_book: Option<bool>,
    /// Order price minimum tick size
    #[serde(default)]
    pub order_price_min_tick_size: Option<f64>,
    /// Order minimum size
    #[serde(default)]
    pub order_min_size: Option<f64>,
    /// Volume as number
    #[serde(default)]
    pub volume_num: Option<f64>,
    /// Liquidity as number
    #[serde(default)]
    pub liquidity_num: Option<f64>,
    /// End date ISO format (YYYY-MM-DD)
    #[serde(default)]
    pub end_date_iso: Option<String>,
    /// Start date ISO format (YYYY-MM-DD)
    #[serde(default)]
    pub start_date_iso: Option<String>,
    /// Whether dates have been reviewed
    #[serde(default)]
    pub has_reviewed_dates: Option<bool>,
    /// 24-hour volume
    #[serde(default)]
    pub volume24hr: Option<f64>,
    /// 1-week volume
    #[serde(default)]
    pub volume1wk: Option<f64>,
    /// 1-month volume
    #[serde(default)]
    pub volume1mo: Option<f64>,
    /// 1-year volume
    #[serde(default)]
    pub volume1yr: Option<f64>,
    /// CLOB token IDs JSON string
    #[serde(default)]
    pub clob_token_ids: Option<String>,
    /// UMA bond amount
    #[serde(default)]
    pub uma_bond: Option<String>,
    /// UMA reward amount
    #[serde(default)]
    pub uma_reward: Option<String>,
    /// 24-hour CLOB volume
    #[serde(default)]
    pub volume24hr_clob: Option<f64>,
    /// 1-week CLOB volume
    #[serde(default)]
    pub volume1wk_clob: Option<f64>,
    /// 1-month CLOB volume
    #[serde(default)]
    pub volume1mo_clob: Option<f64>,
    /// 1-year CLOB volume
    #[serde(default)]
    pub volume1yr_clob: Option<f64>,
    /// CLOB volume
    #[serde(default)]
    pub volume_clob: Option<f64>,
    /// CLOB liquidity
    #[serde(default)]
    pub liquidity_clob: Option<f64>,
    /// Whether accepting orders
    #[serde(default)]
    pub accepting_orders: Option<bool>,
    /// Whether negative risk is enabled
    #[serde(default)]
    pub neg_risk: Option<bool>,
    /// Negative risk market ID
    #[serde(default, rename = "negRiskMarketID")]
    pub neg_risk_market_id: Option<String>,
    /// Negative risk request ID
    #[serde(default, rename = "negRiskRequestID")]
    pub neg_risk_request_id: Option<String>,
    /// Whether market is ready
    #[serde(default)]
    pub ready: Option<bool>,
    /// Whether market is funded
    #[serde(default)]
    pub funded: Option<bool>,
    /// Accepting orders timestamp
    #[serde(default)]
    pub accepting_orders_timestamp: Option<String>,
    /// Whether this is a CYOM market
    #[serde(default)]
    pub cyom: Option<bool>,
    /// Competitive score
    #[serde(default)]
    pub competitive: Option<f64>,
    /// Whether PagerDuty notification is enabled
    #[serde(default)]
    pub pager_duty_notification_enabled: Option<bool>,
    /// Whether market is approved
    #[serde(default)]
    pub approved: Option<bool>,
    /// Rewards minimum size
    #[serde(default)]
    pub rewards_min_size: Option<f64>,
    /// Rewards maximum spread
    #[serde(default)]
    pub rewards_max_spread: Option<f64>,
    /// Current spread
    #[serde(default)]
    pub spread: Option<f64>,
    /// One day price change
    #[serde(default)]
    pub one_day_price_change: Option<f64>,
    /// One hour price change
    #[serde(default)]
    pub one_hour_price_change: Option<f64>,
    /// One week price change
    #[serde(default)]
    pub one_week_price_change: Option<f64>,
    /// One month price change
    #[serde(default)]
    pub one_month_price_change: Option<f64>,
    /// One year price change
    #[serde(default)]
    pub one_year_price_change: Option<f64>,
    /// Last trade price
    #[serde(default)]
    pub last_trade_price: Option<f64>,
    /// Best bid price
    #[serde(default)]
    pub best_bid: Option<f64>,
    /// Best ask price
    #[serde(default)]
    pub best_ask: Option<f64>,
    /// Whether automatically active
    #[serde(default)]
    pub automatically_active: Option<bool>,
    /// Whether to clear book on start
    #[serde(default)]
    pub clear_book_on_start: Option<bool>,
    /// Whether to show GMP series
    #[serde(default)]
    pub show_gmp_series: Option<bool>,
    /// Whether to show GMP outcome
    #[serde(default)]
    pub show_gmp_outcome: Option<bool>,
    /// Whether manual activation is required
    #[serde(default)]
    pub manual_activation: Option<bool>,
    /// Whether this is the "other" option in negative risk
    #[serde(default)]
    pub neg_risk_other: Option<bool>,
    /// UMA resolution statuses JSON string
    #[serde(default)]
    pub uma_resolution_statuses: Option<String>,
    /// Whether pending deployment
    #[serde(default)]
    pub pending_deployment: Option<bool>,
    /// Whether currently deploying
    #[serde(default)]
    pub deploying: Option<bool>,
    /// Deploying timestamp
    #[serde(default)]
    pub deploying_timestamp: Option<String>,
    /// Whether RFQ is enabled
    #[serde(default)]
    pub rfq_enabled: Option<bool>,
    /// Whether holding rewards are enabled
    #[serde(default)]
    pub holding_rewards_enabled: Option<bool>,
    /// Whether fees are enabled
    #[serde(default)]
    pub fees_enabled: Option<bool>,
    /// Whether requires translation
    #[serde(default)]
    pub requires_translation: Option<bool>,
}

impl EventMarket {
    /// Parse outcome prices from JSON string to vector of f64
    ///
    /// Returns a tuple of (yes_price, no_price) for binary markets
    #[must_use]
    pub fn parse_outcome_prices(&self) -> (Option<f64>, Option<f64>) {
        let Some(prices_str) = &self.outcome_prices else {
            return (None, None);
        };

        let prices: Vec<String> = serde_json::from_str(prices_str).unwrap_or_default();
        let yes_price = prices.first().and_then(|s| s.parse::<f64>().ok());
        let no_price = prices.get(1).and_then(|s| s.parse::<f64>().ok());
        (yes_price, no_price)
    }

    /// Parse outcomes from JSON string to vector of strings
    #[must_use]
    pub fn parse_outcomes(&self) -> Vec<String> {
        self.outcomes
            .as_ref()
            .and_then(|s| serde_json::from_str(s).ok())
            .unwrap_or_default()
    }

    /// Parse CLOB token IDs from JSON string to vector of strings
    #[must_use]
    pub fn parse_clob_token_ids(&self) -> Vec<String> {
        self.clob_token_ids
            .as_ref()
            .and_then(|s| serde_json::from_str(s).ok())
            .unwrap_or_default()
    }

    /// Get the Yes token ID (first token in CLOB token IDs)
    #[must_use]
    pub fn yes_token_id(&self) -> Option<String> {
        self.parse_clob_token_ids().first().cloned()
    }

    /// Get the No token ID (second token in CLOB token IDs)
    #[must_use]
    pub fn no_token_id(&self) -> Option<String> {
        self.parse_clob_token_ids().get(1).cloned()
    }

    /// Check if this market is tradeable (active, accepting orders, not closed)
    #[must_use]
    pub fn is_tradeable(&self) -> bool {
        self.active.unwrap_or(false)
            && self.accepting_orders.unwrap_or(false)
            && !self.closed.unwrap_or(false)
    }
}

impl Event {
    /// Get the display name for this event (title or slug as fallback)
    #[must_use]
    pub fn display_name(&self) -> &str {
        self.title.as_deref().unwrap_or(&self.slug)
    }

    /// Get active markets only
    #[must_use]
    pub fn active_markets(&self) -> Vec<&EventMarket> {
        self.markets
            .iter()
            .filter(|m| m.active.unwrap_or(false) && !m.closed.unwrap_or(false))
            .collect()
    }

    /// Get tradeable markets only
    #[must_use]
    pub fn tradeable_markets(&self) -> Vec<&EventMarket> {
        self.markets.iter().filter(|m| m.is_tradeable()).collect()
    }

    /// Check if this event is active and has tradeable markets
    #[must_use]
    pub fn is_tradeable(&self) -> bool {
        self.active.unwrap_or(false)
            && !self.closed.unwrap_or(false)
            && self.markets.iter().any(|m| m.is_tradeable())
    }

    /// Get tag slugs for this event
    #[must_use]
    pub fn tag_slugs(&self) -> Vec<&str> {
        self.tags.iter().filter_map(|t| t.slug.as_deref()).collect()
    }

    /// Check if this event has a specific tag
    #[must_use]
    pub fn has_tag(&self, tag_slug: &str) -> bool {
        self.tags
            .iter()
            .any(|t| t.slug.as_deref() == Some(tag_slug))
    }
}

/// Tag metadata from Gamma API
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Tag {
    /// Tag ID
    #[serde(default)]
    pub id: Option<String>,
    /// Tag slug
    #[serde(default)]
    pub slug: Option<String>,
    /// Tag label (display name)
    #[serde(default)]
    pub label: Option<String>,
    /// Tag name (legacy field, use label instead)
    #[serde(default)]
    pub name: Option<String>,
    /// Tag description
    #[serde(default)]
    pub description: Option<String>,
    /// Whether to force show
    #[serde(default)]
    pub force_show: Option<bool>,
    /// Whether to force hide
    #[serde(default)]
    pub force_hide: Option<bool>,
    /// Published at timestamp
    #[serde(default)]
    pub published_at: Option<String>,
    /// Updated by user ID
    #[serde(default)]
    pub updated_by: Option<i32>,
    /// Created at timestamp
    #[serde(default)]
    pub created_at: Option<String>,
    /// Updated at timestamp
    #[serde(default)]
    pub updated_at: Option<String>,
    /// Whether requires translation
    #[serde(default)]
    pub requires_translation: Option<bool>,
}

// ============================================================================
// Profile Types
// ============================================================================

/// Trader profile information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TraderProfile {
    /// Wallet address
    pub address: String,
    /// Display name
    #[serde(default)]
    pub name: Option<String>,
    /// Username/pseudonym
    #[serde(default)]
    pub username: Option<String>,
    /// Profile image URL
    #[serde(default, rename = "profileImage")]
    pub profile_image: Option<String>,
    /// Bio/description
    #[serde(default)]
    pub bio: Option<String>,
    /// Total volume traded
    #[serde(default)]
    pub volume: Option<Decimal>,
    /// Number of markets traded
    #[serde(default, rename = "marketsTraded")]
    pub markets_traded: Option<i32>,
    /// Profit and loss
    #[serde(default)]
    pub pnl: Option<Decimal>,
}

/// Leaderboard entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LeaderboardEntry {
    /// Rank position
    pub rank: i32,
    /// Wallet address
    pub address: String,
    /// Display name
    #[serde(default)]
    pub name: Option<String>,
    /// Username
    #[serde(default)]
    pub username: Option<String>,
    /// Profile image
    #[serde(default, rename = "profileImage")]
    pub profile_image: Option<String>,
    /// Volume for the period
    pub volume: Decimal,
    /// Profit for the period
    #[serde(default)]
    pub profit: Option<Decimal>,
}

// ============================================================================
// Query Parameter Types
// ============================================================================

/// Pagination parameters
#[derive(Debug, Clone, Default)]
pub struct PaginationParams {
    /// Maximum number of results
    pub limit: Option<u32>,
    /// Offset for pagination
    pub offset: Option<u32>,
    /// Cursor for cursor-based pagination
    pub cursor: Option<String>,
}

impl PaginationParams {
    /// Create new pagination params
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set limit
    #[must_use]
    pub fn with_limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set offset
    #[must_use]
    pub fn with_offset(mut self, offset: u32) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Set cursor
    #[must_use]
    pub fn with_cursor(mut self, cursor: impl Into<String>) -> Self {
        self.cursor = Some(cursor.into());
        self
    }
}

/// Common query parameters for listing endpoints
#[derive(Debug, Clone, Default)]
pub struct ListParams {
    /// Pagination
    pub pagination: PaginationParams,
    /// Filter by closed status
    pub closed: Option<bool>,
    /// Filter by active status
    pub active: Option<bool>,
    /// Sort field
    pub order: Option<String>,
    /// Sort ascending
    pub ascending: Option<bool>,
}

impl ListParams {
    /// Create new list params
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set limit
    #[must_use]
    pub fn with_limit(mut self, limit: u32) -> Self {
        self.pagination.limit = Some(limit);
        self
    }

    /// Set offset
    #[must_use]
    pub fn with_offset(mut self, offset: u32) -> Self {
        self.pagination.offset = Some(offset);
        self
    }

    /// Filter by closed status
    #[must_use]
    pub fn with_closed(mut self, closed: bool) -> Self {
        self.closed = Some(closed);
        self
    }

    /// Filter by active status
    #[must_use]
    pub fn with_active(mut self, active: bool) -> Self {
        self.active = Some(active);
        self
    }

    /// Set sort order
    #[must_use]
    pub fn with_order(mut self, field: impl Into<String>, ascending: bool) -> Self {
        self.order = Some(field.into());
        self.ascending = Some(ascending);
        self
    }
}

// ============================================================================
// Connection Statistics
// ============================================================================

/// WebSocket connection statistics
#[derive(Debug, Clone, Default)]
pub struct ConnectionStats {
    /// Number of messages received
    pub messages_received: u64,
    /// Number of reconnection attempts
    pub reconnect_attempts: u32,
    /// Last message timestamp
    pub last_message_at: Option<DateTime<Utc>>,
    /// Connection established timestamp
    pub connected_at: Option<DateTime<Utc>>,
}

impl ConnectionStats {
    /// Create new stats
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Record a message received
    pub fn record_message(&mut self) {
        self.messages_received += 1;
        self.last_message_at = Some(Utc::now());
    }

    /// Record a reconnection attempt
    pub fn record_reconnect(&mut self) {
        self.reconnect_attempts += 1;
    }

    /// Record connection established
    pub fn record_connected(&mut self) {
        self.connected_at = Some(Utc::now());
        self.reconnect_attempts = 0;
    }
}

// ============================================================================
// Data API Types (for data-api.polymarket.com)
// ============================================================================

/// Polymarket trader profile from Data API
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataApiTrader {
    /// Wallet address
    pub address: String,
    /// Display name
    #[serde(rename = "displayName", default)]
    pub display_name: Option<String>,
    /// Profile image URL
    #[serde(rename = "profileImage", default)]
    pub profile_image: Option<String>,
    /// Total PnL (as string)
    #[serde(rename = "totalPnl", default)]
    pub total_pnl: Option<String>,
    /// Total volume (as string)
    #[serde(rename = "totalVolume", default)]
    pub total_volume: Option<String>,
    /// Number of markets traded
    #[serde(rename = "marketsTraded", default)]
    pub markets_traded: Option<i32>,
    /// Win rate (0.0-1.0)
    #[serde(rename = "winRate", default)]
    pub win_rate: Option<f64>,
}

/// Polymarket position from Data API
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataApiPosition {
    /// User proxy wallet address
    #[serde(rename = "proxyWallet")]
    pub proxy_wallet: String,
    /// Asset token ID
    pub asset: String,
    /// Market condition ID
    #[serde(rename = "conditionId")]
    pub condition_id: String,
    /// Position size (number of tokens)
    pub size: f64,
    /// Average entry price
    #[serde(rename = "avgPrice")]
    pub avg_price: f64,
    /// Initial value of position
    #[serde(rename = "initialValue")]
    pub initial_value: f64,
    /// Current value of position
    #[serde(rename = "currentValue")]
    pub current_value: f64,
    /// Cash PnL
    #[serde(rename = "cashPnl")]
    pub cash_pnl: f64,
    /// Percentage PnL
    #[serde(rename = "percentPnl")]
    pub percent_pnl: f64,
    /// Total amount bought
    #[serde(rename = "totalBought")]
    pub total_bought: f64,
    /// Realized PnL
    #[serde(rename = "realizedPnl")]
    pub realized_pnl: f64,
    /// Percentage realized PnL
    #[serde(rename = "percentRealizedPnl")]
    pub percent_realized_pnl: f64,
    /// Current market price
    #[serde(rename = "curPrice")]
    pub cur_price: f64,
    /// Whether position is redeemable
    pub redeemable: bool,
    /// Whether position is mergeable
    pub mergeable: bool,
    /// Market title
    pub title: String,
    /// Market slug
    pub slug: String,
    /// Market icon URL
    #[serde(default)]
    pub icon: Option<String>,
    /// Event slug
    #[serde(rename = "eventSlug")]
    pub event_slug: String,
    /// Outcome name (Yes/No)
    pub outcome: String,
    /// Outcome index
    #[serde(rename = "outcomeIndex")]
    pub outcome_index: i32,
    /// Opposite outcome name
    #[serde(rename = "oppositeOutcome")]
    pub opposite_outcome: String,
    /// Opposite asset token ID
    #[serde(rename = "oppositeAsset")]
    pub opposite_asset: String,
    /// Market end date
    #[serde(rename = "endDate")]
    pub end_date: String,
    /// Whether this is a negative risk market
    #[serde(rename = "negativeRisk")]
    pub negative_risk: bool,
}

/// Polymarket trade from Data API
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataApiTrade {
    /// Trade ID
    pub id: String,
    /// Market condition ID
    #[serde(rename = "conditionId")]
    pub condition_id: String,
    /// Maker address
    pub maker: String,
    /// Taker address
    pub taker: String,
    /// Trade side
    pub side: String,
    /// Outcome
    pub outcome: String,
    /// Trade size
    pub size: String,
    /// Trade price
    pub price: String,
    /// Trade timestamp
    pub timestamp: String,
    /// Transaction hash
    #[serde(rename = "transactionHash", default)]
    pub transaction_hash: Option<String>,
}

/// User activity (trade/position change) from Data API
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DataApiActivity {
    /// Transaction hash
    #[serde(rename = "transactionHash")]
    pub transaction_hash: String,
    /// Activity timestamp (unix)
    pub timestamp: i64,
    /// User's proxy wallet address
    #[serde(rename = "proxyWallet")]
    pub proxy_wallet: String,
    /// Activity type (TRADE, SPLIT, MERGE, REDEEM, REWARD, CONVERSION, MAKER_REBATE)
    #[serde(rename = "type")]
    pub activity_type: ActivityType,
    /// User display name
    #[serde(default)]
    pub name: Option<String>,
    /// User pseudonym
    #[serde(default)]
    pub pseudonym: Option<String>,
    /// User bio
    #[serde(default)]
    pub bio: Option<String>,
    /// User profile image
    #[serde(rename = "profileImage", default)]
    pub profile_image: Option<String>,
    /// Optimized profile image
    #[serde(rename = "profileImageOptimized", default)]
    pub profile_image_optimized: Option<String>,
    /// Trade side (BUY/SELL)
    #[serde(default)]
    pub side: Option<String>,
    /// Outcome (Yes/No)
    #[serde(default)]
    pub outcome: Option<String>,
    /// Outcome index (0 or 1)
    #[serde(rename = "outcomeIndex", default)]
    pub outcome_index: Option<i32>,
    /// Trade price
    #[serde(default)]
    pub price: Option<f64>,
    /// Trade size
    #[serde(default)]
    pub size: Option<f64>,
    /// USDC size
    #[serde(rename = "usdcSize", default)]
    pub usdc_size: Option<f64>,
    /// Asset/token ID
    #[serde(default)]
    pub asset: Option<String>,
    /// Market condition ID
    #[serde(rename = "conditionId")]
    pub condition_id: String,
    /// Market title
    #[serde(default)]
    pub title: Option<String>,
    /// Market slug
    #[serde(default)]
    pub slug: Option<String>,
    /// Event slug
    #[serde(rename = "eventSlug", default)]
    pub event_slug: Option<String>,
    /// Market icon
    #[serde(default)]
    pub icon: Option<String>,
}

/// Biggest Winner entry from Data API
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BiggestWinner {
    /// Rank (string format)
    #[serde(rename = "winRank")]
    pub win_rank: String,
    /// Wallet address (0x...)
    #[serde(rename = "proxyWallet")]
    pub proxy_wallet: String,
    /// User name
    #[serde(rename = "userName", default)]
    pub user_name: String,
    /// Event slug
    #[serde(rename = "eventSlug")]
    pub event_slug: String,
    /// Event title
    #[serde(rename = "eventTitle")]
    pub event_title: String,
    /// Initial value (USD)
    #[serde(rename = "initialValue")]
    pub initial_value: f64,
    /// Final value (USD)
    #[serde(rename = "finalValue")]
    pub final_value: f64,
    /// Realized profit (USD)
    pub pnl: f64,
    /// Profile image URL
    #[serde(rename = "profileImage", default)]
    pub profile_image: String,
}

/// Query parameters for biggest winners API
#[derive(Debug, Clone)]
pub struct BiggestWinnersQuery {
    /// Time period: day, week, month, all_time
    pub time_period: String,
    /// Max results (max 100 per request)
    pub limit: usize,
    /// Pagination offset
    pub offset: usize,
    /// Category filter (lowercase): all, politics, sports, crypto, etc.
    pub category: String,
}

impl Default for BiggestWinnersQuery {
    fn default() -> Self {
        Self {
            time_period: "all_time".to_string(),
            limit: 100,
            offset: 0,
            category: "all".to_string(),
        }
    }
}

impl BiggestWinnersQuery {
    /// Create new query with defaults
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set time period
    #[must_use]
    pub fn with_time_period(mut self, period: impl Into<String>) -> Self {
        self.time_period = period.into();
        self
    }

    /// Set limit
    #[must_use]
    pub fn with_limit(mut self, limit: usize) -> Self {
        self.limit = limit;
        self
    }

    /// Set offset
    #[must_use]
    pub fn with_offset(mut self, offset: usize) -> Self {
        self.offset = offset;
        self
    }

    /// Set category
    #[must_use]
    pub fn with_category(mut self, category: impl Into<String>) -> Self {
        self.category = category.into();
        self
    }
}

/// Sort field for positions query
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PositionSortBy {
    /// Sort by current value
    Current,
    /// Sort by initial value
    Initial,
    /// Sort by number of tokens
    Tokens,
    /// Sort by cash PnL
    CashPnl,
    /// Sort by percent PnL
    PercentPnl,
    /// Sort by title
    Title,
    /// Sort by resolving date
    Resolving,
    /// Sort by current price
    Price,
    /// Sort by average price
    AvgPrice,
}

impl PositionSortBy {
    /// Convert to API string
    pub fn as_str(&self) -> &str {
        match self {
            Self::Current => "CURRENT",
            Self::Initial => "INITIAL",
            Self::Tokens => "TOKENS",
            Self::CashPnl => "CASHPNL",
            Self::PercentPnl => "PERCENTPNL",
            Self::Title => "TITLE",
            Self::Resolving => "RESOLVING",
            Self::Price => "PRICE",
            Self::AvgPrice => "AVGPRICE",
        }
    }
}

/// Sort direction for positions query
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortDirection {
    /// Ascending
    Asc,
    /// Descending
    Desc,
}

impl SortDirection {
    /// Convert to API string
    pub fn as_str(&self) -> &str {
        match self {
            Self::Asc => "ASC",
            Self::Desc => "DESC",
        }
    }
}

// ============================================================================
// Activity Types
// ============================================================================

/// Activity type for user activity API
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ActivityType {
    /// Trade activity
    Trade,
    /// Split activity (splitting USDC into tokens)
    Split,
    /// Merge activity (merging tokens back to USDC)
    Merge,
    /// Redeem activity (claiming winnings)
    Redeem,
    /// Reward activity (earning rewards)
    Reward,
    /// Conversion activity
    Conversion,
    /// Maker rebate activity
    MakerRebate,
}

impl ActivityType {
    /// Convert to API string
    pub fn as_str(&self) -> &str {
        match self {
            Self::Trade => "TRADE",
            Self::Split => "SPLIT",
            Self::Merge => "MERGE",
            Self::Redeem => "REDEEM",
            Self::Reward => "REWARD",
            Self::Conversion => "CONVERSION",
            Self::MakerRebate => "MAKER_REBATE",
        }
    }
}

/// Sort field for activity query
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ActivitySortBy {
    /// Sort by timestamp (default)
    Timestamp,
    /// Sort by token amount
    Tokens,
    /// Sort by cash amount (USDC)
    Cash,
}

impl ActivitySortBy {
    /// Convert to API string
    pub fn as_str(&self) -> &str {
        match self {
            Self::Timestamp => "TIMESTAMP",
            Self::Tokens => "TOKENS",
            Self::Cash => "CASH",
        }
    }
}

/// Query parameters for positions API
#[derive(Debug, Clone, Default)]
pub struct PositionsQuery {
    /// User address (required)
    pub user: String,
    /// Comma-separated list of condition IDs (mutually exclusive with event_ids)
    pub markets: Option<Vec<String>>,
    /// Comma-separated list of event IDs (mutually exclusive with markets)
    pub event_ids: Option<Vec<i64>>,
    /// Minimum position size threshold
    pub size_threshold: Option<f64>,
    /// Filter by redeemable positions
    pub redeemable: Option<bool>,
    /// Filter by mergeable positions
    pub mergeable: Option<bool>,
    /// Maximum number of results (0-500)
    pub limit: Option<u32>,
    /// Pagination offset (0-10000)
    pub offset: Option<u32>,
    /// Sort field
    pub sort_by: Option<PositionSortBy>,
    /// Sort direction
    pub sort_direction: Option<SortDirection>,
    /// Filter by title (partial match)
    pub title: Option<String>,
}

impl PositionsQuery {
    /// Create new query with user address
    #[must_use]
    pub fn new(user: impl Into<String>) -> Self {
        Self {
            user: user.into(),
            ..Default::default()
        }
    }

    /// Set markets filter (condition IDs)
    #[must_use]
    pub fn with_markets(mut self, markets: Vec<String>) -> Self {
        self.markets = Some(markets);
        self
    }

    /// Set event IDs filter
    #[must_use]
    pub fn with_event_ids(mut self, event_ids: Vec<i64>) -> Self {
        self.event_ids = Some(event_ids);
        self
    }

    /// Set size threshold
    #[must_use]
    pub fn with_size_threshold(mut self, threshold: f64) -> Self {
        self.size_threshold = Some(threshold);
        self
    }

    /// Filter redeemable positions only
    #[must_use]
    pub fn redeemable_only(mut self) -> Self {
        self.redeemable = Some(true);
        self
    }

    /// Filter mergeable positions only
    #[must_use]
    pub fn mergeable_only(mut self) -> Self {
        self.mergeable = Some(true);
        self
    }

    /// Set limit
    #[must_use]
    pub fn with_limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set offset
    #[must_use]
    pub fn with_offset(mut self, offset: u32) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Set sort field
    #[must_use]
    pub fn sort_by(mut self, sort_by: PositionSortBy) -> Self {
        self.sort_by = Some(sort_by);
        self
    }

    /// Set sort direction
    #[must_use]
    pub fn sort_direction(mut self, direction: SortDirection) -> Self {
        self.sort_direction = Some(direction);
        self
    }

    /// Filter by title
    #[must_use]
    pub fn with_title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    /// Build URL query string
    pub fn to_query_string(&self) -> String {
        let mut params = vec![format!("user={}", self.user)];

        if let Some(markets) = &self.markets {
            if !markets.is_empty() {
                params.push(format!("market={}", markets.join(",")));
            }
        }

        if let Some(event_ids) = &self.event_ids {
            if !event_ids.is_empty() {
                let ids: Vec<String> = event_ids.iter().map(|id| id.to_string()).collect();
                params.push(format!("eventId={}", ids.join(",")));
            }
        }

        if let Some(threshold) = self.size_threshold {
            params.push(format!("sizeThreshold={}", threshold));
        }

        if let Some(redeemable) = self.redeemable {
            params.push(format!("redeemable={}", redeemable));
        }

        if let Some(mergeable) = self.mergeable {
            params.push(format!("mergeable={}", mergeable));
        }

        if let Some(limit) = self.limit {
            params.push(format!("limit={}", limit));
        }

        if let Some(offset) = self.offset {
            params.push(format!("offset={}", offset));
        }

        if let Some(sort_by) = &self.sort_by {
            params.push(format!("sortBy={}", sort_by.as_str()));
        }

        if let Some(direction) = &self.sort_direction {
            params.push(format!("sortDirection={}", direction.as_str()));
        }

        if let Some(title) = &self.title {
            params.push(format!("title={}", urlencoding::encode(title)));
        }

        params.join("&")
    }
}

// ============================================================================
// Activity Query Types
// ============================================================================

/// Query parameters for user activity API
#[derive(Debug, Clone, Default)]
pub struct ActivityQuery {
    /// User address (required)
    pub user: String,
    /// Comma-separated list of condition IDs (mutually exclusive with event_ids)
    pub markets: Option<Vec<String>>,
    /// Comma-separated list of event IDs (mutually exclusive with markets)
    pub event_ids: Option<Vec<i64>>,
    /// Activity types to filter
    pub activity_types: Option<Vec<ActivityType>>,
    /// Start timestamp (unix)
    pub start: Option<i64>,
    /// End timestamp (unix)
    pub end: Option<i64>,
    /// Trade side filter (BUY/SELL)
    pub side: Option<Side>,
    /// Maximum number of results (0-500)
    pub limit: Option<u32>,
    /// Pagination offset (0-10000)
    pub offset: Option<u32>,
    /// Sort field
    pub sort_by: Option<ActivitySortBy>,
    /// Sort direction
    pub sort_direction: Option<SortDirection>,
}

impl ActivityQuery {
    /// Create new query with user address
    #[must_use]
    pub fn new(user: impl Into<String>) -> Self {
        Self {
            user: user.into(),
            ..Default::default()
        }
    }

    /// Set markets filter (condition IDs)
    #[must_use]
    pub fn with_markets(mut self, markets: Vec<String>) -> Self {
        self.markets = Some(markets);
        self
    }

    /// Set event IDs filter
    #[must_use]
    pub fn with_event_ids(mut self, event_ids: Vec<i64>) -> Self {
        self.event_ids = Some(event_ids);
        self
    }

    /// Set activity types filter
    #[must_use]
    pub fn with_types(mut self, types: Vec<ActivityType>) -> Self {
        self.activity_types = Some(types);
        self
    }

    /// Filter by single activity type
    #[must_use]
    pub fn with_type(mut self, activity_type: ActivityType) -> Self {
        self.activity_types = Some(vec![activity_type]);
        self
    }

    /// Filter trades only
    #[must_use]
    pub fn trades_only(self) -> Self {
        self.with_type(ActivityType::Trade)
    }

    /// Filter redeems only
    #[must_use]
    pub fn redeems_only(self) -> Self {
        self.with_type(ActivityType::Redeem)
    }

    /// Set start timestamp
    #[must_use]
    pub fn from_timestamp(mut self, start: i64) -> Self {
        self.start = Some(start);
        self
    }

    /// Set end timestamp
    #[must_use]
    pub fn to_timestamp(mut self, end: i64) -> Self {
        self.end = Some(end);
        self
    }

    /// Set time range
    #[must_use]
    pub fn with_time_range(mut self, start: i64, end: i64) -> Self {
        self.start = Some(start);
        self.end = Some(end);
        self
    }

    /// Filter by trade side
    #[must_use]
    pub fn with_side(mut self, side: Side) -> Self {
        self.side = Some(side);
        self
    }

    /// Filter buy trades only
    #[must_use]
    pub fn buys_only(self) -> Self {
        self.with_side(Side::Buy)
    }

    /// Filter sell trades only
    #[must_use]
    pub fn sells_only(self) -> Self {
        self.with_side(Side::Sell)
    }

    /// Set limit
    #[must_use]
    pub fn with_limit(mut self, limit: u32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set offset
    #[must_use]
    pub fn with_offset(mut self, offset: u32) -> Self {
        self.offset = Some(offset);
        self
    }

    /// Set sort field
    #[must_use]
    pub fn sort_by(mut self, sort_by: ActivitySortBy) -> Self {
        self.sort_by = Some(sort_by);
        self
    }

    /// Set sort direction
    #[must_use]
    pub fn sort_direction(mut self, direction: SortDirection) -> Self {
        self.sort_direction = Some(direction);
        self
    }

    /// Sort by timestamp descending (newest first)
    #[must_use]
    pub fn newest_first(self) -> Self {
        self.sort_by(ActivitySortBy::Timestamp)
            .sort_direction(SortDirection::Desc)
    }

    /// Sort by timestamp ascending (oldest first)
    #[must_use]
    pub fn oldest_first(self) -> Self {
        self.sort_by(ActivitySortBy::Timestamp)
            .sort_direction(SortDirection::Asc)
    }

    /// Sort by cash amount descending (largest first)
    #[must_use]
    pub fn largest_first(self) -> Self {
        self.sort_by(ActivitySortBy::Cash)
            .sort_direction(SortDirection::Desc)
    }

    /// Build URL query string
    pub fn to_query_string(&self) -> String {
        let mut params = vec![format!("user={}", self.user)];

        if let Some(markets) = &self.markets {
            if !markets.is_empty() {
                params.push(format!("market={}", markets.join(",")));
            }
        }

        if let Some(event_ids) = &self.event_ids {
            if !event_ids.is_empty() {
                let ids: Vec<String> = event_ids.iter().map(|id| id.to_string()).collect();
                params.push(format!("eventId={}", ids.join(",")));
            }
        }

        if let Some(types) = &self.activity_types {
            if !types.is_empty() {
                let type_strs: Vec<&str> = types.iter().map(|t| t.as_str()).collect();
                params.push(format!("type={}", type_strs.join(",")));
            }
        }

        if let Some(start) = self.start {
            params.push(format!("start={}", start));
        }

        if let Some(end) = self.end {
            params.push(format!("end={}", end));
        }

        if let Some(side) = &self.side {
            params.push(format!("side={}", side.as_str()));
        }

        if let Some(limit) = self.limit {
            params.push(format!("limit={}", limit));
        }

        if let Some(offset) = self.offset {
            params.push(format!("offset={}", offset));
        }

        if let Some(sort_by) = &self.sort_by {
            params.push(format!("sortBy={}", sort_by.as_str()));
        }

        if let Some(direction) = &self.sort_direction {
            params.push(format!("sortDirection={}", direction.as_str()));
        }

        params.join("&")
    }
}

// ============================================================================
// Public Search API Types
// ============================================================================

/// Search request parameters for /public-search endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchRequest {
    /// Search query string
    pub q: String,
    /// Limit per result type
    #[serde(skip_serializing_if = "Option::is_none")]
    pub limit_per_type: Option<u32>,
    /// Whether to search profiles (traders)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_profiles: Option<bool>,
    /// Whether to search tags
    #[serde(skip_serializing_if = "Option::is_none")]
    pub search_tags: Option<bool>,
}

impl SearchRequest {
    /// Create a new search request
    #[must_use]
    pub fn new(query: impl Into<String>) -> Self {
        Self {
            q: query.into(),
            limit_per_type: None,
            search_profiles: None,
            search_tags: None,
        }
    }

    /// Set limit per type
    #[must_use]
    pub fn with_limit(mut self, limit: u32) -> Self {
        self.limit_per_type = Some(limit);
        self
    }

    /// Set whether to search profiles
    #[must_use]
    pub fn with_profiles(mut self, search_profiles: bool) -> Self {
        self.search_profiles = Some(search_profiles);
        self
    }

    /// Set whether to search tags
    #[must_use]
    pub fn with_tags(mut self, search_tags: bool) -> Self {
        self.search_tags = Some(search_tags);
        self
    }
}

/// Search response from /public-search endpoint
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct SearchResponse {
    /// Matching events/markets
    #[serde(default)]
    pub events: Vec<SearchEvent>,
    /// Matching profiles/traders
    #[serde(default)]
    pub profiles: Vec<SearchProfile>,
    /// Matching tags
    #[serde(default)]
    pub tags: Vec<SearchTag>,
}

/// Search event result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchEvent {
    /// Event ID
    #[serde(default)]
    pub id: String,
    /// Event slug
    #[serde(default)]
    pub slug: String,
    /// Event question/title
    #[serde(default)]
    pub question: Option<String>,
    /// Event image
    #[serde(default)]
    pub image: Option<String>,
    /// Whether event is active
    #[serde(default)]
    pub active: bool,
    /// Whether event is closed
    #[serde(default)]
    pub closed: bool,
    /// Total volume
    #[serde(default)]
    pub volume: f64,
    /// 24-hour volume
    #[serde(rename = "volume24hr", default)]
    pub volume_24hr: Option<f64>,
    /// End date
    #[serde(rename = "endDate", default)]
    pub end_date: Option<String>,
}

/// Search profile/trader result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchProfile {
    /// Profile ID
    #[serde(default)]
    pub id: Option<String>,
    /// Display name
    #[serde(default)]
    pub name: Option<String>,
    /// Old API field: imageURI
    #[serde(rename = "imageURI", default)]
    pub image_uri: Option<String>,
    /// New API field: profileImage
    #[serde(rename = "profileImage", default)]
    pub profile_image: Option<String>,
    /// Bio/description
    #[serde(default)]
    pub bio: Option<String>,
    /// Pseudonym
    #[serde(default)]
    pub pseudonym: Option<String>,
    /// Whether to display username publicly
    #[serde(rename = "displayUsernamePublic", default)]
    pub display_username_public: bool,
    /// Old API field: walletAddress
    #[serde(rename = "walletAddress", default)]
    pub wallet_address: Option<String>,
    /// New API field: proxyWallet
    #[serde(rename = "proxyWallet", default)]
    pub proxy_wallet: Option<String>,
}

impl SearchProfile {
    /// Get wallet address (prefer proxy_wallet, fallback to wallet_address)
    #[must_use]
    pub fn get_wallet_address(&self) -> Option<String> {
        self.proxy_wallet
            .clone()
            .or_else(|| self.wallet_address.clone())
    }

    /// Get profile image (prefer profile_image, fallback to image_uri)
    #[must_use]
    pub fn get_profile_image(&self) -> Option<String> {
        self.profile_image
            .clone()
            .or_else(|| self.image_uri.clone())
    }

    /// Get display name (prefer name, fallback to pseudonym)
    #[must_use]
    pub fn get_display_name(&self) -> Option<String> {
        self.name.clone().or_else(|| self.pseudonym.clone())
    }
}

/// Search tag result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchTag {
    /// Tag ID
    pub id: String,
    /// Tag label
    pub label: String,
    /// Tag slug
    #[serde(default)]
    pub slug: Option<String>,
}

// ============================================================================
// Public Profile Types (Gamma API)
// ============================================================================

/// Public profile response from Gamma API `/public-profile` endpoint
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicProfile {
    /// ISO 8601 timestamp of when the profile was created
    #[serde(default)]
    pub created_at: Option<String>,
    /// The proxy wallet address
    #[serde(default)]
    pub proxy_wallet: Option<String>,
    /// URL to the profile image
    #[serde(default)]
    pub profile_image: Option<String>,
    /// Whether the username is displayed publicly
    #[serde(default)]
    pub display_username_public: Option<bool>,
    /// Profile bio
    #[serde(default)]
    pub bio: Option<String>,
    /// Auto-generated pseudonym
    #[serde(default)]
    pub pseudonym: Option<String>,
    /// User-chosen display name
    #[serde(default)]
    pub name: Option<String>,
    /// X (Twitter) username
    #[serde(rename = "xUsername", default)]
    pub x_username: Option<String>,
    /// Whether the profile has a verified badge
    #[serde(default)]
    pub verified_badge: Option<bool>,
    /// Array of associated user objects
    #[serde(default)]
    pub users: Vec<PublicProfileUser>,
}

impl PublicProfile {
    /// Get display name (prefer name, fallback to pseudonym)
    #[must_use]
    pub fn get_display_name(&self) -> Option<&str> {
        self.name.as_deref().or(self.pseudonym.as_deref())
    }

    /// Check if this profile has a verified badge
    #[must_use]
    pub fn is_verified(&self) -> bool {
        self.verified_badge.unwrap_or(false)
    }
}

/// User object associated with a public profile
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PublicProfileUser {
    /// User ID
    #[serde(default)]
    pub id: Option<String>,
    /// Whether the user is a creator
    #[serde(default)]
    pub creator: Option<bool>,
    /// Whether the user is a moderator
    #[serde(rename = "mod", default)]
    pub is_mod: Option<bool>,
}

/// Closed position from Data API (for PnL calculation)
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClosedPosition {
    /// Position ID
    #[serde(default)]
    pub id: Option<String>,
    /// Proxy wallet address
    #[serde(rename = "proxyWallet", default)]
    pub proxy_wallet: Option<String>,
    /// Token asset ID
    #[serde(default)]
    pub asset: Option<String>,
    /// Market condition ID
    #[serde(rename = "conditionId")]
    pub condition_id: String,
    /// Market title
    pub title: String,
    /// Market slug
    pub slug: String,
    /// Event slug
    #[serde(rename = "eventSlug")]
    pub event_slug: String,
    /// Outcome (Yes/No)
    pub outcome: String,
    /// Outcome index
    #[serde(rename = "outcomeIndex")]
    pub outcome_index: i32,
    /// Entry price
    #[serde(rename = "avgPrice")]
    pub avg_price: f64,
    /// Current price
    #[serde(rename = "curPrice", default)]
    pub cur_price: Option<f64>,
    /// Exit price
    #[serde(rename = "exitPrice", default)]
    pub exit_price: Option<f64>,
    /// Position size (shares)
    #[serde(default)]
    pub size: Option<f64>,
    /// Total bought amount (USDC)
    #[serde(rename = "totalBought", default)]
    pub total_bought: Option<f64>,
    /// Realized PnL
    #[serde(rename = "realizedPnl", default)]
    pub realized_pnl: Option<f64>,
    /// Cash out amount
    #[serde(rename = "cashOut", default)]
    pub cash_out: Option<f64>,
    /// Is winning position
    #[serde(rename = "isWinner", default)]
    pub is_winner: Option<bool>,
    /// Closed timestamp (unix)
    #[serde(default)]
    pub timestamp: Option<i64>,
    /// Closed timestamp (ISO string)
    #[serde(rename = "closedAt", default)]
    pub closed_at: Option<String>,
    /// End date
    #[serde(rename = "endDate", default)]
    pub end_date: Option<String>,
    /// Market icon
    #[serde(default)]
    pub icon: Option<String>,
    /// Opposite outcome name
    #[serde(rename = "oppositeOutcome", default)]
    pub opposite_outcome: Option<String>,
    /// Opposite asset ID
    #[serde(rename = "oppositeAsset", default)]
    pub opposite_asset: Option<String>,
}

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

    /// Test that NewOrder JSON serialization matches TypeScript SDK field order
    /// This is critical for HMAC signature compatibility
    #[test]
    fn test_new_order_json_field_order() {
        let order = NewOrder {
            defer_exec: false,
            order: NewOrderData {
                salt: 2915952280710976,
                maker: "0xc2ca793cf057d48a054bedabf625f301b40d38aa".to_string(),
                signer: "0xd13765b3e68431bf2b6e9994a0f4c3d2495799e9".to_string(),
                taker: "0x0000000000000000000000000000000000000000".to_string(),
                token_id: "21489772516410038586556744342392982044189999368638682594741395650226594484811".to_string(),
                maker_amount: "10000".to_string(),
                taker_amount: "1000000".to_string(),
                side: "BUY".to_string(),
                expiration: "0".to_string(),
                nonce: "0".to_string(),
                fee_rate_bps: "0".to_string(),
                signature_type: 1,
                signature: "0x0cfb0e318afe33e1189f23d4b11a1092963865d7ff7f7a035110d50d71a2ab484ae4828b3fcfcac2ada92fbd825eedfe4eb21d4e1cdd5aa1a47e23bf5d539b781c".to_string(),
            },
            owner: "fe9fb6b1-9ae6-6c5b-3cca-1ace6a8b1f29".to_string(),
            order_type: OrderType::GTC,
        };

        let json = serde_json::to_string(&order).unwrap();

        // Expected format from TypeScript SDK (field order matters for HMAC)
        let expected = r#"{"deferExec":false,"order":{"salt":2915952280710976,"maker":"0xc2ca793cf057d48a054bedabf625f301b40d38aa","signer":"0xd13765b3e68431bf2b6e9994a0f4c3d2495799e9","taker":"0x0000000000000000000000000000000000000000","tokenId":"21489772516410038586556744342392982044189999368638682594741395650226594484811","makerAmount":"10000","takerAmount":"1000000","side":"BUY","expiration":"0","nonce":"0","feeRateBps":"0","signatureType":1,"signature":"0x0cfb0e318afe33e1189f23d4b11a1092963865d7ff7f7a035110d50d71a2ab484ae4828b3fcfcac2ada92fbd825eedfe4eb21d4e1cdd5aa1a47e23bf5d539b781c"},"owner":"fe9fb6b1-9ae6-6c5b-3cca-1ace6a8b1f29","orderType":"GTC"}"#;

        assert_eq!(
            json, expected,
            "\nJSON field order mismatch!\n\nGot:\n{}\n\nExpected:\n{}\n",
            json, expected
        );
    }
}