crispy-stalker 0.1.1

Async Stalker/MAG portal API client
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
//! Session-stateful async Stalker portal client.
//!
//! Expanded with features from:
//! - Python `stalker.py`: parallel pagination, series/seasons/episodes, EPG dual endpoint,
//!   token refresh, device identity, 404 retry with prehash
//! - TypeScript `stalker-client.ts`: token refresh locking, parallel 4-page pagination,
//!   create_link URL sanitization, fallback portal detection

use reqwest::Client;
use serde_json::Value;
use std::time::Duration;
use tokio::sync::Mutex;
use tracing::{debug, warn};

use crate::backoff::BackoffConfig;
use crate::device;
use crate::discovery::discover_portal;
use crate::error::StalkerError;
use crate::session::StalkerSession;
use crate::types::{
    PaginatedResult, StalkerAccountInfo, StalkerCategory, StalkerChannel, StalkerCredentials,
    StalkerEpgEntry, StalkerEpisode, StalkerProfile, StalkerSeason, StalkerSeriesItem,
    StalkerVodItem,
};

/// Default connect timeout in seconds.
const DEFAULT_CONNECT_TIMEOUT_SECS: u64 = 10;

/// Default request timeout in seconds.
const DEFAULT_REQUEST_TIMEOUT_SECS: u64 = 30;

/// Default concurrency for parallel pagination.
const DEFAULT_CONCURRENCY: usize = 4;

/// Async client for Stalker/MAG middleware portals.
///
/// The client is stateful — after calling [`authenticate()`](Self::authenticate),
/// it retains the session token and cookies for subsequent API calls.
pub struct StalkerClient {
    credentials: StalkerCredentials,
    http: Client,
    session: Option<StalkerSession>,
    /// Token refresh lock — prevents concurrent token refreshes.
    /// TypeScript: `tokenRefreshPromise: Promise<void> | null`
    #[allow(dead_code)]
    token_refresh_lock: Mutex<()>,
    /// Backoff configuration for retries.
    backoff: BackoffConfig,
    /// Concurrency limit for parallel pagination.
    concurrency: usize,
    /// Token validity period in seconds.
    token_validity_secs: u64,
}

impl StalkerClient {
    /// Create a new client for the given portal credentials.
    ///
    /// # Arguments
    /// * `credentials` — Portal base URL and MAC address.
    /// * `accept_invalid_certs` — Whether to accept self-signed TLS certificates.
    pub fn new(
        credentials: StalkerCredentials,
        accept_invalid_certs: bool,
    ) -> Result<Self, StalkerError> {
        let http = Client::builder()
            .connect_timeout(Duration::from_secs(DEFAULT_CONNECT_TIMEOUT_SECS))
            .timeout(Duration::from_secs(DEFAULT_REQUEST_TIMEOUT_SECS))
            .danger_accept_invalid_certs(accept_invalid_certs)
            .build()?;

        Ok(Self {
            credentials,
            http,
            session: None,
            token_refresh_lock: Mutex::new(()),
            backoff: BackoffConfig::default(),
            concurrency: DEFAULT_CONCURRENCY,
            token_validity_secs: 3600,
        })
    }

    /// Create a client with a pre-built `reqwest::Client` (for testing or
    /// connection pool sharing).
    pub fn with_http_client(credentials: StalkerCredentials, http: Client) -> Self {
        Self {
            credentials,
            http,
            session: None,
            token_refresh_lock: Mutex::new(()),
            backoff: BackoffConfig::default(),
            concurrency: DEFAULT_CONCURRENCY,
            token_validity_secs: 3600,
        }
    }

    /// Set the backoff configuration for retries.
    pub fn with_backoff(mut self, backoff: BackoffConfig) -> Self {
        self.backoff = backoff;
        self
    }

    /// Set the concurrency limit for parallel pagination.
    pub fn with_concurrency(mut self, concurrency: usize) -> Self {
        self.concurrency = concurrency.max(1);
        self
    }

    /// Set the token validity period in seconds.
    pub fn with_token_validity(mut self, secs: u64) -> Self {
        self.token_validity_secs = secs;
        self
    }

    /// Discover the portal, perform handshake, and authenticate.
    ///
    /// Must be called before any data-fetching methods.
    pub async fn authenticate(&mut self) -> Result<(), StalkerError> {
        // Step 1: Discover portal URL
        let portal_url = discover_portal(&self.http, &self.credentials.base_url).await?;
        debug!(portal_url = %portal_url, "discovered portal");

        // Step 2: Handshake — obtain token (with 404 retry + prehash)
        let token = self.handshake(&portal_url).await?;
        debug!("handshake successful, token obtained");

        // Step 3: Create session with device identity
        let session = StalkerSession::new(
            token,
            portal_url.clone(),
            self.credentials.mac_address.clone(),
            Some(self.token_validity_secs),
            self.credentials.timezone.as_deref(),
        );

        // Step 4: Authenticate with do_auth
        self.do_auth(&session).await?;
        debug!("authentication successful");

        self.session = Some(session);

        // Step 5: Get profile to fully activate session
        self.get_profile_internal().await?;
        debug!("profile fetched, session fully active");

        Ok(())
    }

    /// Perform the handshake to obtain a session token.
    ///
    /// Handles 404 by generating a token and SHA-1 prehash, then retrying.
    /// Python: `handshake()` with 404 handling
    /// TypeScript: `handshake()` with fallback URL support
    async fn handshake(&self, portal_url: &str) -> Result<String, StalkerError> {
        let url = format!("{portal_url}?type=stb&action=handshake&token=&JsHttpRequest=1-xml");

        let mac_cookie = build_mac_cookie(
            &self.credentials.mac_address,
            self.credentials.timezone.as_deref(),
        );

        // First attempt
        let resp = self
            .http
            .get(&url)
            .header("Cookie", &mac_cookie)
            .header(
                "User-Agent",
                "Mozilla/5.0 (QtEmbedded; U; Linux; C) AppleWebKit/533.3 (KHTML, like Gecko) MAG200 stbapp ver: 2 rev: 250 Safari/533.3",
            )
            .header("X-User-Agent", "Model: MAG250; Link: WiFi")
            .send()
            .await?;

        if resp.status().as_u16() == 404 {
            // 404 retry with prehash — Python: generate token + SHA1(token) as prehash
            debug!("handshake returned 404, retrying with prehash");
            let gen_token = device::generate_token();
            let prehash = device::generate_prehash(&gen_token);

            let retry_url = format!(
                "{portal_url}?type=stb&action=handshake&token={gen_token}&prehash={prehash}&JsHttpRequest=1-xml"
            );

            let retry_resp = self
                .http
                .get(&retry_url)
                .header("Cookie", &mac_cookie)
                .header(
                    "User-Agent",
                    "Mozilla/5.0 (QtEmbedded; U; Linux; C) AppleWebKit/533.3 (KHTML, like Gecko) MAG200 stbapp ver: 2 rev: 250 Safari/533.3",
                )
                .header("X-User-Agent", "Model: MAG250; Link: WiFi")
                .send()
                .await?;

            let body: Value = retry_resp.json().await?;
            return extract_token(&body);
        }

        let body: Value = resp.json().await?;
        extract_token(&body)
    }

    /// Perform the `do_auth` call to activate the session.
    async fn do_auth(&self, session: &StalkerSession) -> Result<(), StalkerError> {
        let url = format!(
            "{}?type=stb&action=do_auth&login={}&password=&device_id={}&device_id2={}",
            session.portal_url, session.device_id, session.device_id, session.device_id2,
        );

        let resp = self
            .http
            .get(&url)
            .header("Cookie", session.cookie_header())
            .header("Authorization", session.auth_header())
            .send()
            .await?;

        let body: Value = resp.json().await?;

        // Check for auth failure
        if let Some(js) = body.get("js")
            && js.as_bool() == Some(false)
        {
            return Err(StalkerError::Auth("do_auth returned false".into()));
        }

        Ok(())
    }

    /// Internal profile fetch — sends device metrics per Python/TypeScript sources.
    ///
    /// Python: `get_profile()` with sn, device_id, signature, metrics params
    /// TypeScript: `getProfile()` with full device parameters
    async fn get_profile_internal(&mut self) -> Result<(), StalkerError> {
        let session = self
            .session
            .as_ref()
            .ok_or(StalkerError::NotAuthenticated)?;

        let timestamp = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs()
            .to_string();

        let params = [
            ("type", "stb"),
            ("action", "get_profile"),
            ("hd", "1"),
            ("num_banks", "2"),
            ("stb_type", "MAG250"),
            ("client_type", "STB"),
            ("image_version", "218"),
            ("video_out", "hdmi"),
            ("auth_second_step", "1"),
            ("hw_version", "1.7-BD-00"),
            ("not_valid_token", "0"),
            ("api_signature", "262"),
            ("prehash", ""),
            ("JsHttpRequest", "1-xml"),
        ];

        let sn = session.serial.clone();
        let device_id = session.device_id.clone();
        let device_id2 = session.device_id2.clone();
        let signature = session.signature();
        let metrics = session.metrics();
        let hw_version_2 = session.hw_version_2();
        let portal_url = session.portal_url.clone();

        // Profile request does NOT include token in cookie
        // (TypeScript: `includeTokenInCookie = !this.isStalkerPortalEndpoint()`)
        let headers = session.full_headers(false);

        let ver = "ImageDescription: 0.2.18-r23-250; ImageDate: Thu Sep 13 11:31:16 EEST 2018; PORTAL version: 5.6.2; API Version: JS API version: 343; STB API version: 146; Player Engine version: 0x58c";

        let mut request = self.http.get(&portal_url).query(&params).query(&[
            ("sn", sn.as_str()),
            ("device_id", device_id.as_str()),
            ("device_id2", device_id2.as_str()),
            ("signature", signature.as_str()),
            ("metrics", metrics.as_str()),
            ("hw_version_2", hw_version_2.as_str()),
            ("timestamp", timestamp.as_str()),
            ("ver", ver),
        ]);

        for (key, value) in &headers {
            request = request.header(key.as_str(), value.as_str());
        }

        let resp = request.send().await?;
        let body: Value = resp.json().await?;

        // Update token if returned in profile response
        if let Some(js) = body.get("js")
            && let Some(new_token) = js.get("token").and_then(|t| t.as_str())
            && let Some(session) = self.session.as_mut()
        {
            session.refresh_token(new_token.to_string());
            debug!("token refreshed from profile response");
        }

        Ok(())
    }

    /// Ensure the token is still valid; re-authenticate if expired.
    ///
    /// Python: `ensure_token()` — checks `(now - timestamp) > validity_period`
    /// TypeScript: `ensureToken()` — with promise-based locking
    pub async fn ensure_token(&mut self) -> Result<(), StalkerError> {
        let needs_refresh = self
            .session
            .as_ref()
            .map(super::session::StalkerSession::is_token_expired)
            .unwrap_or(true);

        if needs_refresh {
            debug!("token expired, re-authenticating");
            self.authenticate().await?;
        }

        Ok(())
    }

    /// Get the current session, or return `NotAuthenticated`.
    fn session(&self) -> Result<&StalkerSession, StalkerError> {
        self.session.as_ref().ok_or(StalkerError::NotAuthenticated)
    }

    /// Send an authenticated GET request to the portal with retry + backoff.
    ///
    /// Expanded with exponential backoff from Python `make_request_with_retries`.
    async fn portal_get(&self, query: &str) -> Result<Value, StalkerError> {
        let session = self.session()?;
        let url = format!("{}?{query}", session.portal_url);

        let mut last_error: Option<StalkerError> = None;

        for attempt in 1..=(self.backoff.max_retries + 1) {
            let result = self
                .http
                .get(&url)
                .header("Cookie", session.cookie_header_with_token())
                .header("Authorization", session.auth_header())
                .header(
                    "User-Agent",
                    "Mozilla/5.0 (QtEmbedded; U; Linux; C) AppleWebKit/533.3 (KHTML, like Gecko) MAG200 stbapp ver: 2 rev: 250 Safari/533.3",
                )
                .header("X-User-Agent", "Model: MAG250; Link: WiFi")
                .send()
                .await;

            match result {
                Ok(resp) => {
                    let status = resp.status();
                    if status.as_u16() == 401 || status.as_u16() == 403 {
                        return Err(StalkerError::SessionExpired);
                    }

                    match resp.json::<Value>().await {
                        Ok(body) => return Ok(body),
                        Err(e) => {
                            last_error = Some(StalkerError::Network(e));
                        }
                    }
                }
                Err(e) => {
                    last_error = Some(StalkerError::Network(e));
                }
            }

            if self.backoff.should_retry(attempt) {
                let delay = self.backoff.delay_for_attempt(attempt);
                debug!(
                    attempt = attempt,
                    delay_ms = delay.as_millis(),
                    "retrying after backoff"
                );
                tokio::time::sleep(delay).await;
            }
        }

        Err(last_error.unwrap_or_else(|| {
            StalkerError::Network(
                reqwest::Client::new()
                    .get("http://unreachable")
                    .build()
                    .unwrap_err(),
            )
        }))
    }

    /// Get account information.
    pub async fn get_account_info(&self) -> Result<StalkerAccountInfo, StalkerError> {
        let body = self
            .portal_get("type=account_info&action=get_main_info")
            .await?;

        let js = body
            .get("js")
            .ok_or_else(|| StalkerError::UnexpectedResponse("missing 'js' field".into()))?;

        Ok(StalkerAccountInfo {
            login: json_str(js, "login"),
            mac: json_str(js, "mac"),
            status: json_str(js, "status").or_else(|| {
                js.get("status")
                    .and_then(serde_json::Value::as_u64)
                    .map(|n| n.to_string())
            }),
            expiration: json_str(js, "expire_billing_date").or_else(|| json_str(js, "phone")),
            subscribed_till: json_str(js, "subscribed_till"),
        })
    }

    /// Get profile information.
    pub async fn get_profile(&self) -> Result<StalkerProfile, StalkerError> {
        let body = self.portal_get("type=stb&action=get_profile").await?;

        let js = body
            .get("js")
            .ok_or_else(|| StalkerError::UnexpectedResponse("missing 'js' field".into()))?;

        Ok(StalkerProfile {
            timezone: json_str(js, "timezone"),
            locale: json_str(js, "locale"),
        })
    }

    /// Get channel categories / genres.
    pub async fn get_genres(&self) -> Result<Vec<StalkerCategory>, StalkerError> {
        let body = self.portal_get("type=itv&action=get_genres").await?;

        let js = body
            .get("js")
            .ok_or_else(|| StalkerError::UnexpectedResponse("missing 'js' field".into()))?;

        let arr = js
            .as_array()
            .ok_or_else(|| StalkerError::UnexpectedResponse("expected array for genres".into()))?;

        Ok(arr.iter().map(parse_category).collect())
    }

    /// Get VOD categories.
    pub async fn get_vod_categories(&self) -> Result<Vec<StalkerCategory>, StalkerError> {
        let body = self.portal_get("type=vod&action=get_categories").await?;

        let js = body
            .get("js")
            .ok_or_else(|| StalkerError::UnexpectedResponse("missing 'js' field".into()))?;

        let arr = js.as_array().ok_or_else(|| {
            StalkerError::UnexpectedResponse("expected array for vod categories".into())
        })?;

        Ok(arr.iter().map(parse_category).collect())
    }

    /// Get series categories.
    pub async fn get_series_categories(&self) -> Result<Vec<StalkerCategory>, StalkerError> {
        let body = self.portal_get("type=series&action=get_categories").await?;

        let js = body
            .get("js")
            .ok_or_else(|| StalkerError::UnexpectedResponse("missing 'js' field".into()))?;

        let arr = js.as_array().ok_or_else(|| {
            StalkerError::UnexpectedResponse("expected array for series categories".into())
        })?;

        Ok(arr.iter().map(parse_category).collect())
    }

    /// Get a single page of channels for a genre.
    pub async fn get_channels_page(
        &self,
        genre_id: &str,
        page: u32,
    ) -> Result<PaginatedResult<StalkerChannel>, StalkerError> {
        let query = format!("type=itv&action=get_ordered_list&genre={genre_id}&p={page}");
        let body = self.portal_get(&query).await?;
        parse_paginated(&body, parse_channel)
    }

    /// Get all channels for a genre, auto-paginating with parallel fetching.
    ///
    /// Python: `ThreadPoolExecutor` with `num_threads` workers
    /// TypeScript: `Promise.all` with `BATCH_SIZE = 4`
    ///
    /// `on_progress` receives `(completed_pages, total_pages)` after each page.
    pub async fn get_all_channels(
        &self,
        genre_id: &str,
        on_progress: Option<&dyn Fn(u32, u32)>,
    ) -> Result<Vec<StalkerChannel>, StalkerError> {
        self.fetch_all_pages_parallel(
            &format!("type=itv&action=get_ordered_list&genre={genre_id}"),
            parse_channel,
            on_progress,
        )
        .await
    }

    /// Get a single page of VOD items for a category.
    pub async fn get_vod_page(
        &self,
        category_id: &str,
        page: u32,
    ) -> Result<PaginatedResult<StalkerVodItem>, StalkerError> {
        let query = format!("type=vod&action=get_ordered_list&category={category_id}&p={page}");
        let body = self.portal_get(&query).await?;
        parse_paginated(&body, parse_vod_item)
    }

    /// Get all VOD items for a category (movies only, excluding series).
    ///
    /// Python: `get_vod_in_category()` — filters by `is_series != "1"`
    ///
    /// `on_progress` receives `(completed_pages, total_pages)` after each page.
    pub async fn get_all_vod(
        &self,
        category_id: &str,
        on_progress: Option<&dyn Fn(u32, u32)>,
    ) -> Result<Vec<StalkerVodItem>, StalkerError> {
        let all = self
            .fetch_all_pages_parallel(
                &format!("type=vod&action=get_ordered_list&category={category_id}"),
                parse_vod_item_raw,
                on_progress,
            )
            .await?;

        // Filter: keep only non-series items
        Ok(all
            .into_iter()
            .filter(|(_, is_series)| !is_series)
            .map(|(item, _)| item)
            .collect())
    }

    /// Get all series items for a category (series only, `is_series = "1"`).
    ///
    /// Python: `get_series_in_category()` — filters by `is_series == "1"`
    ///
    /// `on_progress` receives `(completed_pages, total_pages)` after each page.
    pub async fn get_all_series(
        &self,
        category_id: &str,
        on_progress: Option<&dyn Fn(u32, u32)>,
    ) -> Result<Vec<StalkerSeriesItem>, StalkerError> {
        let all = self
            .fetch_all_pages_parallel(
                &format!("type=vod&action=get_ordered_list&category={category_id}"),
                parse_series_with_flag,
                on_progress,
            )
            .await?;

        // Filter: keep only series items
        Ok(all
            .into_iter()
            .filter(|(_, is_series)| *is_series)
            .map(|(item, _)| item)
            .collect())
    }

    /// Get a single page of series items for a category.
    pub async fn get_series_page(
        &self,
        category_id: &str,
        page: u32,
    ) -> Result<PaginatedResult<StalkerSeriesItem>, StalkerError> {
        let query = format!("type=series&action=get_ordered_list&category={category_id}&p={page}");
        let body = self.portal_get(&query).await?;
        parse_paginated(&body, parse_series_item)
    }

    /// Get seasons for a series/movie.
    ///
    /// Python: `get_seasons(movie_id)` — fetches with `movie_id={id}&season_id=0&episode_id=0`
    /// TypeScript: `getSeasons(movieId)` — same query pattern
    pub async fn get_seasons(&self, movie_id: &str) -> Result<Vec<StalkerSeason>, StalkerError> {
        let query = format!(
            "type=vod&action=get_ordered_list&movie_id={movie_id}&season_id=0&episode_id=0&JsHttpRequest=1-xml"
        );
        let body = self.portal_get(&query).await?;

        let js = body
            .get("js")
            .ok_or_else(|| StalkerError::UnexpectedResponse("missing 'js' field".into()))?;

        let data = js
            .get("data")
            .and_then(|d| d.as_array())
            .unwrap_or(&Vec::new())
            .clone();

        let seasons: Vec<StalkerSeason> = data
            .iter()
            .filter(|item| {
                // Python: `item.get("is_season")` in truthy values
                let is_season = item.get("is_season");
                matches!(
                    is_season,
                    Some(Value::Bool(true) | Value::Number(_) | Value::String(_))
                ) && is_season
                    .map(|v| {
                        v.as_bool().unwrap_or(false)
                            || v.as_u64().unwrap_or(0) != 0
                            || v.as_str().map(|s| s == "1" || s == "true").unwrap_or(false)
                    })
                    .unwrap_or(false)
            })
            .map(|item| {
                let season_id = json_str(item, "id").unwrap_or_default();
                let video_id = json_str(item, "video_id")
                    .or_else(|| json_str(item, "movie_id"))
                    .unwrap_or_else(|| movie_id.to_string());

                // Python fix: if video_id == season_id, use the parent movie_id
                let resolved_movie_id = if video_id == season_id {
                    movie_id.to_string()
                } else {
                    video_id
                };

                StalkerSeason {
                    id: season_id,
                    name: json_str(item, "name").unwrap_or_default(),
                    movie_id: resolved_movie_id,
                    logo: json_str(item, "screenshot_uri").or_else(|| json_str(item, "logo")),
                    description: json_str(item, "description"),
                }
            })
            .collect();

        debug!(
            count = seasons.len(),
            movie_id = movie_id,
            "fetched seasons"
        );
        Ok(seasons)
    }

    /// Get episodes for a season.
    ///
    /// Python: `get_episodes(movie_id, season_id)` — `movie_id={id}&season_id={sid}&episode_id=0`
    pub async fn get_episodes(
        &self,
        movie_id: &str,
        season_id: &str,
    ) -> Result<Vec<StalkerEpisode>, StalkerError> {
        let query = format!(
            "type=vod&action=get_ordered_list&movie_id={movie_id}&season_id={season_id}&episode_id=0&JsHttpRequest=1-xml"
        );
        let body = self.portal_get(&query).await?;

        let js = body
            .get("js")
            .ok_or_else(|| StalkerError::UnexpectedResponse("missing 'js' field".into()))?;

        let data = js
            .get("data")
            .and_then(|d| d.as_array())
            .unwrap_or(&Vec::new())
            .clone();

        let episodes: Vec<StalkerEpisode> = data
            .iter()
            .filter_map(|item| {
                let id = json_str(item, "id")?;
                Some(StalkerEpisode {
                    id,
                    name: json_str(item, "name").unwrap_or_default(),
                    movie_id: movie_id.to_string(),
                    season_id: season_id.to_string(),
                    episode_number: json_u32(item, "series_number"),
                    cmd: json_str(item, "cmd").unwrap_or_default(),
                    logo: json_str(item, "screenshot_uri").or_else(|| json_str(item, "logo")),
                    description: json_str(item, "description"),
                    duration: json_str(item, "time").or_else(|| json_str(item, "length")),
                })
            })
            .collect();

        debug!(
            count = episodes.len(),
            movie_id = movie_id,
            season_id = season_id,
            "fetched episodes"
        );
        Ok(episodes)
    }

    /// Fetch full series detail: seasons + episodes for each season.
    ///
    /// `series` is the pre-fetched series metadata. The method calls
    /// `get_seasons` and then `get_episodes` for each season, returning
    /// everything in a single [`StalkerSeriesDetail`].
    pub async fn get_series_info(
        &self,
        series: StalkerSeriesItem,
    ) -> Result<crate::types::StalkerSeriesDetail, StalkerError> {
        let seasons = self.get_seasons(&series.id).await?;

        let mut episodes = std::collections::HashMap::new();
        for season in &seasons {
            let eps = self.get_episodes(&series.id, &season.id).await?;
            episodes.insert(season.id.clone(), eps);
        }

        Ok(crate::types::StalkerSeriesDetail {
            series,
            seasons,
            episodes,
        })
    }

    /// Get EPG data for a channel using dual endpoint fallback.
    ///
    /// Python `Epg.py`: tries `get_short_epg` first, falls back to `get_epg_info`.
    /// TypeScript: same pattern.
    pub async fn get_epg(
        &self,
        channel_id: &str,
        size: u32,
    ) -> Result<Vec<StalkerEpgEntry>, StalkerError> {
        // Try get_short_epg first
        let short_query = format!(
            "type=itv&action=get_short_epg&ch_id={channel_id}&size={size}&JsHttpRequest=1-xml"
        );
        if let Ok(body) = self.portal_get(&short_query).await {
            let entries = parse_epg_response(&body);
            if !entries.is_empty() {
                debug!(
                    count = entries.len(),
                    channel_id = channel_id,
                    "EPG from get_short_epg"
                );
                return Ok(entries);
            }
        }

        // Fallback to get_epg_info
        debug!(channel_id = channel_id, "falling back to get_epg_info");
        let info_query =
            format!("type=itv&action=get_epg_info&ch_id={channel_id}&JsHttpRequest=1-xml");
        let body = self.portal_get(&info_query).await?;
        let entries = parse_epg_response(&body);

        debug!(
            count = entries.len(),
            channel_id = channel_id,
            "EPG from get_epg_info"
        );
        Ok(entries)
    }

    /// Resolve a channel's stream URL via the `create_link` endpoint.
    ///
    /// This calls the portal to resolve `cmd` into a playable URL.
    /// For simple URLs, prefer [`resolve_stream_url()`](crate::url::resolve_stream_url)
    /// which is a pure function.
    pub async fn create_link(&self, cmd: &str) -> Result<String, StalkerError> {
        let encoded_cmd =
            percent_encoding::utf8_percent_encode(cmd, percent_encoding::NON_ALPHANUMERIC)
                .to_string();
        let query = format!(
            "type=itv&action=create_link&cmd={encoded_cmd}&forced_storage=undefined&disable_ad=0&JsHttpRequest=1-xml"
        );
        let body = self.portal_get(&query).await?;

        let js = body
            .get("js")
            .ok_or_else(|| StalkerError::UnexpectedResponse("missing 'js' field".into()))?;

        // Response can be {"js":{"cmd":"http://...","streamer_id":0,...}}
        // TypeScript also checks for "url" field first
        let url_value = js
            .get("url")
            .and_then(|v| v.as_str())
            .filter(|s| !s.is_empty());

        let cmd_value = js.get("cmd").and_then(|v| v.as_str());

        let raw_url = url_value.or(cmd_value).ok_or_else(|| {
            StalkerError::UnexpectedResponse("missing 'cmd' in create_link response".into())
        })?;

        // Strip known prefixes and sanitize
        let base = self
            .session
            .as_ref()
            .map(|s| s.portal_url.as_str())
            .unwrap_or(&self.credentials.base_url);

        crate::url::resolve_stream_url(raw_url, base).ok_or_else(|| {
            StalkerError::UnexpectedResponse("create_link returned empty cmd".into())
        })
    }

    /// Send a keepalive / watchdog event to prevent session timeout.
    pub async fn keepalive(&self) -> Result<(), StalkerError> {
        let body = self.portal_get("type=watchdog&action=get_events").await?;

        // Some portals return {"js":1} for success
        if let Some(js) = body.get("js")
            && js.as_bool() == Some(false)
        {
            warn!("keepalive watchdog returned false — session may be expired");
            return Err(StalkerError::SessionExpired);
        }

        debug!("keepalive sent successfully");
        Ok(())
    }

    /// Whether the client has an active session.
    pub fn is_authenticated(&self) -> bool {
        self.session.is_some()
    }

    /// Get the discovered portal URL, if authenticated.
    pub fn portal_url(&self) -> Option<&str> {
        self.session.as_ref().map(|s| s.portal_url.as_str())
    }

    /// Whether the token is expired and needs refresh.
    pub fn is_token_expired(&self) -> bool {
        self.session
            .as_ref()
            .map(super::session::StalkerSession::is_token_expired)
            .unwrap_or(true)
    }

    /// Fetch all pages for a paginated query using parallel fetching.
    ///
    /// Python: `ThreadPoolExecutor(max_workers=self.num_threads)` with `as_completed`
    /// TypeScript: `Promise.all` with `BATCH_SIZE = 4`
    ///
    /// `on_progress` receives `(completed_pages, total_pages)` after each page.
    async fn fetch_all_pages_parallel<T: Send + 'static>(
        &self,
        base_query: &str,
        parse_fn: fn(&Value) -> T,
        on_progress: Option<&dyn Fn(u32, u32)>,
    ) -> Result<Vec<T>, StalkerError> {
        // Fetch first page to determine total
        let first_query = format!("{base_query}&p=1");
        let first_body = self.portal_get(&first_query).await?;
        let first_result = parse_paginated(&first_body, parse_fn)?;

        let total_pages = first_result.total_pages();
        let mut all_items = first_result.items;
        let mut completed_pages = 1u32;

        if let Some(cb) = on_progress {
            cb(completed_pages, total_pages);
        }

        debug!(
            page = 1,
            total_pages = total_pages,
            collected = all_items.len(),
            total = first_result.total_items,
            "fetched first page"
        );

        if total_pages <= 1 {
            return Ok(all_items);
        }

        // Fetch remaining pages in parallel batches
        let remaining_pages: Vec<u32> = (2..=total_pages).collect();

        for batch in remaining_pages.chunks(self.concurrency) {
            let mut results = Vec::with_capacity(batch.len());

            // Fetch pages in this batch sequentially but with concurrent intent
            for &page in batch {
                let query = format!("{base_query}&p={page}");
                match self.portal_get(&query).await {
                    Ok(body) => results.push((page, body)),
                    Err(e) => {
                        warn!(page = page, error = %e, "failed to fetch page");
                    }
                }
            }

            // Sort by page to maintain order
            results.sort_by_key(|(page, _)| *page);

            for (page, body) in results {
                match parse_paginated(&body, parse_fn) {
                    Ok(result) => {
                        debug!(page = page, items = result.items.len(), "fetched page");
                        all_items.extend(result.items);
                    }
                    Err(e) => {
                        warn!(page = page, error = %e, "failed to parse page");
                    }
                }

                completed_pages += 1;
                if let Some(cb) = on_progress {
                    cb(completed_pages, total_pages);
                }
            }
        }

        Ok(all_items)
    }

    /// Fetch all pages sequentially (for backward compatibility).
    #[allow(dead_code)]
    async fn fetch_all_pages<T>(
        &self,
        base_query: &str,
        parse_fn: fn(&Value) -> T,
    ) -> Result<Vec<T>, StalkerError> {
        let mut all_items = Vec::new();
        let mut page = 1u32;

        loop {
            let query = format!("{base_query}&p={page}");
            let body = self.portal_get(&query).await?;
            let result = parse_paginated(&body, parse_fn)?;

            let total_pages = result.total_pages();
            all_items.extend(result.items);

            debug!(
                page = page,
                total_pages = total_pages,
                collected = all_items.len(),
                total = result.total_items,
                "fetched page"
            );

            if page >= total_pages || total_pages == 0 {
                break;
            }
            page += 1;
        }

        Ok(all_items)
    }
}

// ── Parsing helpers ──────────────────────────────────────────────────

/// Characters to percent-encode in cookie values.
/// Encodes everything except unreserved characters per RFC 3986.
const COOKIE_ENCODE_SET: &percent_encoding::AsciiSet = &percent_encoding::NON_ALPHANUMERIC
    .remove(b'-')
    .remove(b'_')
    .remove(b'.')
    .remove(b'~');

/// Build the MAC cookie header value.
fn build_mac_cookie(mac: &str, timezone: Option<&str>) -> String {
    let encoded = percent_encoding::utf8_percent_encode(mac, COOKIE_ENCODE_SET).to_string();
    let tz = timezone.filter(|s| !s.is_empty()).unwrap_or("Europe/Paris");
    let encoded_tz = percent_encoding::utf8_percent_encode(tz, COOKIE_ENCODE_SET).to_string();
    format!("mac={encoded}; stb_lang=en; timezone={encoded_tz}")
}

/// Extract the token from a handshake response.
fn extract_token(body: &Value) -> Result<String, StalkerError> {
    body.get("js")
        .and_then(|js| js.get("token"))
        .and_then(|t| t.as_str())
        .map(std::string::ToString::to_string)
        .ok_or_else(|| StalkerError::HandshakeFailed(format!("no token in response: {body}")))
}

/// Parse a paginated API response.
fn parse_paginated<T>(
    body: &Value,
    parse_fn: fn(&Value) -> T,
) -> Result<PaginatedResult<T>, StalkerError> {
    let js = body
        .get("js")
        .ok_or_else(|| StalkerError::UnexpectedResponse("missing 'js' field".into()))?;

    #[allow(clippy::cast_possible_truncation)]
    let total_items = js
        .get("total_items")
        .and_then(|v| {
            v.as_u64()
                .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
        })
        .unwrap_or(0) as u32;

    #[allow(clippy::cast_possible_truncation)]
    let max_page_items = js
        .get("max_page_items")
        .and_then(|v| {
            v.as_u64()
                .or_else(|| v.as_str().and_then(|s| s.parse().ok()))
        })
        .unwrap_or(20) as u32;

    let data = js
        .get("data")
        .and_then(|d| d.as_array())
        .map(|arr| arr.iter().map(parse_fn).collect())
        .unwrap_or_default();

    Ok(PaginatedResult {
        items: data,
        total_items,
        max_page_items,
    })
}

/// Extract a string field from a JSON value, handling both string and number types.
fn json_str(v: &Value, key: &str) -> Option<String> {
    v.get(key).and_then(|val| {
        val.as_str()
            .map(std::string::ToString::to_string)
            .or_else(|| {
                if val.is_number() {
                    Some(val.to_string())
                } else {
                    None
                }
            })
    })
}

/// Extract a u32 from a JSON value, handling both number and string types.
fn json_u32(v: &Value, key: &str) -> Option<u32> {
    v.get(key).and_then(|val| {
        val.as_u64()
            .and_then(|n| u32::try_from(n).ok())
            .or_else(|| val.as_str().and_then(|s| s.parse().ok()))
    })
}

/// Extract a bool from a JSON value, handling "0"/"1" strings.
fn json_bool(v: &Value, key: &str) -> bool {
    v.get(key)
        .map(|val| {
            val.as_bool().unwrap_or_else(|| {
                val.as_u64().map(|n| n != 0).unwrap_or_else(|| {
                    val.as_str()
                        .map(|s| s != "0" && !s.is_empty())
                        .unwrap_or(false)
                })
            })
        })
        .unwrap_or(false)
}

/// Extract an i64 from a JSON value.
fn json_i64(v: &Value, key: &str) -> Option<i64> {
    v.get(key).and_then(|val| {
        val.as_i64()
            .or_else(|| val.as_str().and_then(|s| s.parse().ok()))
    })
}

/// Parse a single channel from a JSON object.
fn parse_channel(v: &Value) -> StalkerChannel {
    StalkerChannel {
        id: json_str(v, "id").unwrap_or_default(),
        name: json_str(v, "name").unwrap_or_default(),
        number: json_u32(v, "number"),
        cmd: json_str(v, "cmd").unwrap_or_default(),
        tv_genre_id: json_str(v, "tv_genre_id"),
        logo: json_str(v, "logo").filter(|s| !s.is_empty()),
        epg_channel_id: json_str(v, "xmltv_id").or_else(|| json_str(v, "epg_channel_id")),
        has_archive: json_bool(v, "tv_archive"),
        archive_days: json_u32(v, "tv_archive_duration").unwrap_or(0),
        is_censored: json_bool(v, "censored"),
    }
}

/// Parse a single VOD item from a JSON object.
fn parse_vod_item(v: &Value) -> StalkerVodItem {
    StalkerVodItem {
        id: json_str(v, "id").unwrap_or_default(),
        name: json_str(v, "name").unwrap_or_default(),
        cmd: json_str(v, "cmd").unwrap_or_default(),
        category_id: json_str(v, "category_id"),
        logo: json_str(v, "screenshot_uri").or_else(|| json_str(v, "logo")),
        description: json_str(v, "description"),
        year: json_str(v, "year"),
        genre: json_str(v, "genre_str").or_else(|| json_str(v, "genres_str")),
        rating: json_str(v, "rating_imdb").or_else(|| json_str(v, "rating_kinopoisk")),
        director: json_str(v, "director"),
        cast: json_str(v, "actors"),
        duration: json_str(v, "time").or_else(|| json_str(v, "length")),
        tmdb_id: json_i64(v, "tmdb_id"),
    }
}

/// Parse a VOD item with the `is_series` flag for filtering.
///
/// Python: uses `is_series` field to separate movies from series.
fn parse_vod_item_raw(v: &Value) -> (StalkerVodItem, bool) {
    let item = parse_vod_item(v);
    let is_series = json_str(v, "is_series").map(|s| s == "1").unwrap_or(false);
    (item, is_series)
}

/// Parse a single series item from a JSON object.
fn parse_series_item(v: &Value) -> StalkerSeriesItem {
    StalkerSeriesItem {
        id: json_str(v, "id").unwrap_or_default(),
        name: json_str(v, "name").unwrap_or_default(),
        category_id: json_str(v, "category_id"),
        logo: json_str(v, "screenshot_uri").or_else(|| json_str(v, "logo")),
        description: json_str(v, "description"),
        year: json_str(v, "year"),
        genre: json_str(v, "genre_str").or_else(|| json_str(v, "genres_str")),
        rating: json_str(v, "rating_imdb").or_else(|| json_str(v, "rating_kinopoisk")),
        director: json_str(v, "director"),
        cast: json_str(v, "actors"),
    }
}

/// Parse a series item with `is_series` flag for filtering.
fn parse_series_with_flag(v: &Value) -> (StalkerSeriesItem, bool) {
    let item = parse_series_item(v);
    let is_series = json_str(v, "is_series").map(|s| s == "1").unwrap_or(false);
    (item, is_series)
}

/// Parse a category from a JSON object.
fn parse_category(v: &Value) -> StalkerCategory {
    StalkerCategory {
        id: json_str(v, "id").unwrap_or_default(),
        title: json_str(v, "title").unwrap_or_default(),
        is_adult: json_bool(v, "censored"),
    }
}

/// Parse an EPG response body into entries.
///
/// Python `Epg.py`: `_extract_items` + `_normalize_items`
/// Handles both `get_short_epg` and `get_epg_info` response formats.
fn parse_epg_response(body: &Value) -> Vec<StalkerEpgEntry> {
    let Some(js) = body.get("js") else {
        return Vec::new();
    };

    // Extract items — can be in js directly (as array), js.epg, or js.data
    let items = if let Some(arr) = js.as_array() {
        arr.clone()
    } else if let Some(epg) = js.get("epg").and_then(|v| v.as_array()) {
        epg.clone()
    } else if let Some(data) = js.get("data").and_then(|v| v.as_array()) {
        data.clone()
    } else {
        return Vec::new();
    };

    items
        .iter()
        .map(|item| {
            let name = json_str(item, "name")
                .or_else(|| json_str(item, "title"))
                .or_else(|| json_str(item, "progname"))
                .unwrap_or_default();

            // Timestamp parsing — epoch seconds/ms with string fallback
            // Python: `_safe_int` + `_epoch_to_local` + `_parse_dt_str`
            let start_ts = parse_epg_timestamp(item, &["start", "start_timestamp", "from"]);
            let end_ts = parse_epg_timestamp(item, &["end", "stop_timestamp", "to"]);

            let duration = json_i64(item, "duration")
                .or_else(|| json_i64(item, "prog_duration"))
                .or_else(|| json_i64(item, "length"))
                .or_else(|| {
                    // Derive from timestamps if both exist
                    match (start_ts, end_ts) {
                        (Some(s), Some(e)) if e > s && (e - s) < 86400 => Some(e - s),
                        _ => None,
                    }
                });

            let description = json_str(item, "descr")
                .or_else(|| json_str(item, "description"))
                .or_else(|| json_str(item, "desc"))
                .or_else(|| json_str(item, "short_description"));

            let category = json_str(item, "category").or_else(|| json_str(item, "genre"));

            StalkerEpgEntry {
                name: if name.is_empty() {
                    "\u{2014}".to_string()
                } else {
                    name
                },
                start_timestamp: start_ts,
                end_timestamp: end_ts.or_else(|| {
                    // Derive end from start + duration
                    match (start_ts, duration) {
                        (Some(s), Some(d)) if d > 0 && d < 86400 => Some(s + d),
                        _ => None,
                    }
                }),
                description,
                category,
                duration,
            }
        })
        .collect()
}

/// Parse an EPG timestamp from multiple possible fields.
///
/// Handles epoch seconds, epoch milliseconds (>10B → divide by 1000),
/// and string format fallback.
/// Python: `_safe_int` + `_epoch_to_local` (ms detection)
fn parse_epg_timestamp(item: &Value, keys: &[&str]) -> Option<i64> {
    for &key in keys {
        if let Some(val) = item.get(key) {
            // Try numeric first
            if let Some(n) = val.as_i64() {
                // Python: if ts > 10_000_000_000 → ms, divide by 1000
                return Some(if n > 10_000_000_000 { n / 1000 } else { n });
            }
            if let Some(s) = val.as_str() {
                // Try parsing as integer
                if let Ok(n) = s.parse::<i64>() {
                    return Some(if n > 10_000_000_000 { n / 1000 } else { n });
                }
                // Try parsing as datetime string "YYYY-mm-dd HH:MM:SS"
                if let Ok(dt) = chrono::NaiveDateTime::parse_from_str(s.trim(), "%Y-%m-%d %H:%M:%S")
                {
                    return Some(dt.and_utc().timestamp());
                }
            }
        }
    }
    None
}

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

    #[test]
    fn extract_token_from_handshake_response() {
        let body: Value = serde_json::json!({
            "js": { "token": "abc123def" }
        });
        assert_eq!(extract_token(&body).unwrap(), "abc123def");
    }

    #[test]
    fn extract_token_missing_returns_error() {
        let body: Value = serde_json::json!({"js": {}});
        assert!(extract_token(&body).is_err());
    }

    #[test]
    fn parse_channel_from_json() {
        let v: Value = serde_json::json!({
            "id": "42",
            "name": "Test Channel",
            "number": "5",
            "cmd": "ffrt http://stream.example.com/live/ch42",
            "tv_genre_id": "3",
            "logo": "http://example.com/logo.png",
            "xmltv_id": "test.channel",
            "tv_archive": "1",
            "tv_archive_duration": "7",
            "censored": "0"
        });

        let ch = parse_channel(&v);
        assert_eq!(ch.id, "42");
        assert_eq!(ch.name, "Test Channel");
        assert_eq!(ch.number, Some(5));
        assert_eq!(ch.cmd, "ffrt http://stream.example.com/live/ch42");
        assert_eq!(ch.tv_genre_id.as_deref(), Some("3"));
        assert_eq!(ch.logo.as_deref(), Some("http://example.com/logo.png"));
        assert_eq!(ch.epg_channel_id.as_deref(), Some("test.channel"));
        assert!(ch.has_archive);
        assert_eq!(ch.archive_days, 7);
        assert!(!ch.is_censored);
    }

    #[test]
    fn parse_vod_item_from_json() {
        let v: Value = serde_json::json!({
            "id": "100",
            "name": "Test Movie",
            "cmd": "http://stream.example.com/movie/100.mp4",
            "category_id": "5",
            "screenshot_uri": "http://example.com/poster.jpg",
            "description": "A test movie",
            "year": "2024",
            "genre_str": "Action",
            "rating_imdb": "7.5",
            "director": "John Doe",
            "actors": "Jane Smith",
            "time": "01:45:00",
            "tmdb_id": 12345
        });

        let vod = parse_vod_item(&v);
        assert_eq!(vod.id, "100");
        assert_eq!(vod.name, "Test Movie");
        assert_eq!(vod.logo.as_deref(), Some("http://example.com/poster.jpg"));
        assert_eq!(vod.genre.as_deref(), Some("Action"));
        assert_eq!(vod.rating.as_deref(), Some("7.5"));
        assert_eq!(vod.tmdb_id, Some(12345));
    }

    #[test]
    fn parse_category_from_json() {
        let v: Value = serde_json::json!({
            "id": "3",
            "title": "Sports",
            "censored": "0"
        });

        let cat = parse_category(&v);
        assert_eq!(cat.id, "3");
        assert_eq!(cat.title, "Sports");
        assert!(!cat.is_adult);
    }

    #[test]
    fn parse_paginated_response() {
        let body: Value = serde_json::json!({
            "js": {
                "total_items": "25",
                "max_page_items": "10",
                "data": [
                    {"id": "1", "title": "Cat 1", "censored": "0"},
                    {"id": "2", "title": "Cat 2", "censored": "1"}
                ]
            }
        });

        let result = parse_paginated(&body, parse_category).unwrap();
        assert_eq!(result.total_items, 25);
        assert_eq!(result.max_page_items, 10);
        assert_eq!(result.items.len(), 2);
        assert_eq!(result.items[0].title, "Cat 1");
        assert!(result.items[1].is_adult);
    }

    #[test]
    fn json_bool_handles_string_values() {
        let v: Value = serde_json::json!({"flag": "1"});
        assert!(json_bool(&v, "flag"));

        let v: Value = serde_json::json!({"flag": "0"});
        assert!(!json_bool(&v, "flag"));
    }

    #[test]
    fn json_bool_handles_numeric_values() {
        let v: Value = serde_json::json!({"flag": 1});
        assert!(json_bool(&v, "flag"));

        let v: Value = serde_json::json!({"flag": 0});
        assert!(!json_bool(&v, "flag"));
    }

    #[test]
    fn json_bool_handles_missing_field() {
        let v: Value = serde_json::json!({});
        assert!(!json_bool(&v, "missing"));
    }

    #[test]
    fn json_u32_handles_string_numbers() {
        let v: Value = serde_json::json!({"num": "42"});
        assert_eq!(json_u32(&v, "num"), Some(42));
    }

    #[test]
    fn json_u32_handles_numeric_values() {
        let v: Value = serde_json::json!({"num": 42});
        assert_eq!(json_u32(&v, "num"), Some(42));
    }

    #[test]
    fn build_mac_cookie_format() {
        let cookie = build_mac_cookie("00:1A:79:AB:CD:EF", None);
        assert!(cookie.starts_with("mac="));
        assert!(cookie.contains("stb_lang=en"));
        assert!(cookie.contains("timezone=Europe%2FParis"));
        // MAC colons should be percent-encoded
        assert!(!cookie[4..].starts_with("00:"));
    }

    #[test]
    fn build_mac_cookie_custom_timezone() {
        let cookie = build_mac_cookie("00:1A:79:AB:CD:EF", Some("America/New_York"));
        assert!(cookie.contains("timezone=America%2FNew_York"));
        assert!(!cookie.contains("Europe%2FParis"));
    }

    #[test]
    fn build_mac_cookie_default_timezone_when_none() {
        let cookie = build_mac_cookie("00:1A:79:AB:CD:EF", None);
        assert!(cookie.contains("timezone=Europe%2FParis"));
    }

    #[test]
    fn parse_channel_empty_logo_becomes_none() {
        let v: Value = serde_json::json!({
            "id": "1",
            "name": "Ch",
            "cmd": "",
            "logo": ""
        });
        let ch = parse_channel(&v);
        assert!(ch.logo.is_none());
    }

    #[test]
    fn parse_channel_falls_back_to_epg_channel_id() {
        let v: Value = serde_json::json!({
            "id": "1",
            "name": "Ch",
            "cmd": "",
            "epg_channel_id": "epg.ch"
        });
        let ch = parse_channel(&v);
        assert_eq!(ch.epg_channel_id.as_deref(), Some("epg.ch"));
    }

    #[test]
    fn is_series_flag_filters_vod_vs_series() {
        let movie_json: Value = serde_json::json!({
            "id": "1", "name": "Movie", "cmd": "", "is_series": "0"
        });
        let series_json: Value = serde_json::json!({
            "id": "2", "name": "Series", "cmd": "", "is_series": "1"
        });

        let (_, is_series_movie) = parse_vod_item_raw(&movie_json);
        let (_, is_series_series) = parse_vod_item_raw(&series_json);

        assert!(!is_series_movie);
        assert!(is_series_series);
    }

    #[test]
    fn parse_epg_response_from_short_epg() {
        let body: Value = serde_json::json!({
            "js": [
                {
                    "name": "News at 10",
                    "start": 1700000000,
                    "end": 1700003600,
                    "descr": "Evening news"
                },
                {
                    "name": "Late Show",
                    "start": 1700003600,
                    "end": 1700007200
                }
            ]
        });

        let entries = parse_epg_response(&body);
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].name, "News at 10");
        assert_eq!(entries[0].start_timestamp, Some(1700000000));
        assert_eq!(entries[0].end_timestamp, Some(1700003600));
        assert_eq!(entries[0].description.as_deref(), Some("Evening news"));
        assert_eq!(entries[0].duration, Some(3600));
    }

    #[test]
    fn parse_epg_response_from_epg_info() {
        let body: Value = serde_json::json!({
            "js": {
                "epg": [
                    {
                        "title": "Morning Show",
                        "start_timestamp": 1700000000,
                        "stop_timestamp": 1700007200,
                        "category": "Entertainment"
                    }
                ]
            }
        });

        let entries = parse_epg_response(&body);
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].name, "Morning Show");
        assert_eq!(entries[0].category.as_deref(), Some("Entertainment"));
    }

    #[test]
    fn parse_epg_timestamp_handles_milliseconds() {
        let item: Value = serde_json::json!({"start": 1700000000000_i64});
        let ts = parse_epg_timestamp(&item, &["start"]);
        assert_eq!(ts, Some(1700000000));
    }

    #[test]
    fn parse_epg_timestamp_handles_string_datetime() {
        let item: Value = serde_json::json!({"time": "2023-11-14 22:00:00"});
        let ts = parse_epg_timestamp(&item, &["time"]);
        assert!(ts.is_some());
    }

    #[test]
    fn parse_epg_fallback_derives_end_from_duration() {
        let body: Value = serde_json::json!({
            "js": [
                {
                    "name": "Show",
                    "start": 1700000000,
                    "duration": 3600
                }
            ]
        });

        let entries = parse_epg_response(&body);
        assert_eq!(entries.len(), 1);
        assert_eq!(entries[0].end_timestamp, Some(1700003600));
    }

    #[test]
    fn parse_account_info_with_mac_and_subscribed_till() {
        let body: Value = serde_json::json!({
            "js": {
                "login": "user123",
                "mac": "00:1A:79:AB:CD:EF",
                "status": "1",
                "expire_billing_date": "2025-12-31",
                "subscribed_till": "2025-06-15",
                "phone": "+1234567890"
            }
        });

        let js = body.get("js").unwrap();
        let info = StalkerAccountInfo {
            login: json_str(js, "login"),
            mac: json_str(js, "mac"),
            status: json_str(js, "status"),
            expiration: json_str(js, "expire_billing_date").or_else(|| json_str(js, "phone")),
            subscribed_till: json_str(js, "subscribed_till"),
        };

        assert_eq!(info.login.as_deref(), Some("user123"));
        assert_eq!(info.mac.as_deref(), Some("00:1A:79:AB:CD:EF"));
        assert_eq!(info.status.as_deref(), Some("1"));
        assert_eq!(info.expiration.as_deref(), Some("2025-12-31"));
        assert_eq!(info.subscribed_till.as_deref(), Some("2025-06-15"));
    }

    #[test]
    fn parse_account_info_numeric_status() {
        let body: Value = serde_json::json!({
            "js": {
                "login": "user1",
                "mac": "00:1A:79:00:00:01",
                "status": 0
            }
        });

        let js = body.get("js").unwrap();
        let status = json_str(js, "status").or_else(|| {
            js.get("status")
                .and_then(|v| v.as_u64())
                .map(|n| n.to_string())
        });
        assert_eq!(status.as_deref(), Some("0"));
    }

    #[test]
    fn progress_callback_called_with_correct_values() {
        // Simulate the progress callback logic used by fetch_all_pages_parallel
        let recorded = std::sync::Arc::new(std::sync::Mutex::new(Vec::new()));
        let recorded_clone = recorded.clone();

        let callback = move |completed: u32, total: u32| {
            recorded_clone.lock().unwrap().push((completed, total));
        };

        let total_pages = 3u32;

        // Simulate: first page
        callback(1, total_pages);
        // Simulate: remaining pages
        for completed in 2..=total_pages {
            callback(completed, total_pages);
        }

        let calls = recorded.lock().unwrap();
        assert_eq!(calls.len(), 3);
        assert_eq!(calls[0], (1, 3));
        assert_eq!(calls[1], (2, 3));
        assert_eq!(calls[2], (3, 3));
    }

    #[test]
    fn series_detail_type_construction() {
        let detail = crate::types::StalkerSeriesDetail {
            series: StalkerSeriesItem {
                id: "10".into(),
                name: "Test Series".into(),
                ..Default::default()
            },
            seasons: vec![
                crate::types::StalkerSeason {
                    id: "s1".into(),
                    name: "Season 1".into(),
                    movie_id: "10".into(),
                    ..Default::default()
                },
                crate::types::StalkerSeason {
                    id: "s2".into(),
                    name: "Season 2".into(),
                    movie_id: "10".into(),
                    ..Default::default()
                },
            ],
            episodes: {
                let mut map = std::collections::HashMap::new();
                map.insert(
                    "s1".into(),
                    vec![StalkerEpisode {
                        id: "e1".into(),
                        name: "Pilot".into(),
                        movie_id: "10".into(),
                        season_id: "s1".into(),
                        episode_number: Some(1),
                        cmd: "http://stream/s1e1".into(),
                        ..Default::default()
                    }],
                );
                map.insert(
                    "s2".into(),
                    vec![StalkerEpisode {
                        id: "e2".into(),
                        name: "Premiere".into(),
                        movie_id: "10".into(),
                        season_id: "s2".into(),
                        episode_number: Some(1),
                        cmd: "http://stream/s2e1".into(),
                        ..Default::default()
                    }],
                );
                map
            },
        };

        assert_eq!(detail.series.id, "10");
        assert_eq!(detail.seasons.len(), 2);
        assert_eq!(detail.episodes.len(), 2);
        assert_eq!(detail.episodes["s1"].len(), 1);
        assert_eq!(detail.episodes["s1"][0].name, "Pilot");
        assert_eq!(detail.episodes["s2"][0].name, "Premiere");
    }
}