queue-runtime 0.2.0

Multi-provider queue runtime for Queue-Keeper
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
//! Azure Service Bus provider implementation.
//!
//! This module provides production-ready Azure Service Bus integration with:
//! - Native session support for ordered message processing
//! - Connection pooling and sender/receiver caching
//! - Dead letter queue integration
//! - Multiple authentication methods (connection string, managed identity, client secret)
//! - Comprehensive error classification for retry logic
//!
//! ## Authentication Methods
//!
//! The provider supports four authentication methods:
//! - **ConnectionString**: Direct connection string with embedded credentials
//! - **ManagedIdentity**: Azure Managed Identity for serverless environments
//! - **ClientSecret**: Service principal with tenant/client ID and secret
//! - **DefaultCredential**: Default Azure credential chain for development
//!
//! ## Session Management
//!
//! Azure Service Bus provides native session support with:
//! - Strict FIFO ordering within session boundaries
//! - Exclusive session locks during processing
//! - Automatic lock renewal for long-running operations
//! - Session state storage for stateful processing
//!
//! ## Example
//!
//! ```no_run
//! use queue_runtime::{QueueClientFactory, QueueConfig, ProviderConfig, AzureServiceBusConfig, AzureAuthMethod};
//! use chrono::Duration;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let config = QueueConfig {
//!     provider: ProviderConfig::AzureServiceBus(AzureServiceBusConfig {
//!         connection_string: Some("Endpoint=sb://...".to_string()),
//!         namespace: None,
//!         auth_method: AzureAuthMethod::ConnectionString,
//!         use_sessions: true,
//!         session_timeout: Duration::minutes(5),
//!     }),
//!     ..Default::default()
//! };
//!
//! let client = QueueClientFactory::create_client(config).await?;
//! # Ok(())
//! # }
//! ```

use crate::client::{QueueProvider, SessionProvider};
use crate::error::{ConfigurationError, QueueError, SerializationError};
use crate::message::{
    Message, MessageId, QueueName, ReceiptHandle, ReceivedMessage, SessionId, Timestamp,
};
use crate::provider::{AzureServiceBusConfig, ProviderType, SessionSupport};
use async_trait::async_trait;
use azure_core::credentials::Secret as AzureSecret;
use azure_core::credentials::TokenCredential;
use azure_identity::{
    ClientSecretCredential, ClientSecretCredentialOptions, DeveloperToolsCredential,
    ManagedIdentityCredential,
};
use chrono::{Duration, Utc};
use reqwest::{header, Client as HttpClient, StatusCode};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;
use tokio::sync::RwLock;

#[cfg(test)]
#[path = "azure_tests.rs"]
mod tests;

// ============================================================================
// Shared Auth Helper
// ============================================================================

/// Acquire a bearer token from an AAD [`TokenCredential`] for the Azure Service Bus scope.
///
/// # Errors
///
/// Returns [`AzureError::AuthenticationError`] when the credential provider fails.
async fn get_bearer_token(
    cred: &(dyn TokenCredential + Send + Sync),
) -> Result<String, AzureError> {
    let scopes = &["https://servicebus.azure.net/.default"];
    let token = cred
        .get_token(scopes, None)
        .await
        .map_err(|e| AzureError::AuthenticationError(format!("Failed to get token: {}", e)))?;
    // token is an AccessToken (outer struct); .token is its Secret<String> field; .secret() extracts the raw string.
    Ok(token.token.secret().to_string())
}

/// Generate a Shared Access Signature (SAS) token for Azure Service Bus.
///
/// Parses `SharedAccessKeyName` and `SharedAccessKey` from `conn_str`, then
/// produces an HMAC-SHA256 signature over `namespace_url` and the expiry.
/// The resulting token is valid for one hour from the moment of generation.
///
/// # Errors
///
/// Returns [`AzureError::AuthenticationError`] if the connection string is
/// missing the required fields or if the key cannot be decoded.
fn generate_sas_token(namespace_url: &str, conn_str: &str) -> Result<String, AzureError> {
    let mut key_name = None;
    let mut key = None;

    for part in conn_str.split(';') {
        if let Some(value) = part.strip_prefix("SharedAccessKeyName=") {
            key_name = Some(value.to_string());
        } else if let Some(value) = part.strip_prefix("SharedAccessKey=") {
            key = Some(value.to_string());
        }
    }

    let key_name = key_name.ok_or_else(|| {
        AzureError::AuthenticationError(
            "Missing SharedAccessKeyName in connection string".to_string(),
        )
    })?;
    let key = key.ok_or_else(|| {
        AzureError::AuthenticationError("Missing SharedAccessKey in connection string".to_string())
    })?;

    let expiry = (Utc::now() + Duration::hours(1)).timestamp();
    let string_to_sign = format!("{}\n{}", urlencoding::encode(namespace_url), expiry);

    use base64::{engine::general_purpose::STANDARD, Engine};
    use hmac::{Hmac, KeyInit, Mac};
    use sha2::Sha256;
    type HmacSha256 = Hmac<Sha256>;

    let key_bytes = STANDARD
        .decode(&key)
        .map_err(|e| AzureError::AuthenticationError(format!("Invalid SharedAccessKey: {}", e)))?;
    let mut mac = HmacSha256::new_from_slice(&key_bytes)
        .map_err(|e| AzureError::AuthenticationError(format!("Failed to create HMAC: {}", e)))?;
    mac.update(string_to_sign.as_bytes());
    let signature = STANDARD.encode(mac.finalize().into_bytes());

    Ok(format!(
        "SharedAccessSignature sr={}&sig={}&se={}&skn={}",
        urlencoding::encode(namespace_url),
        urlencoding::encode(&signature),
        expiry,
        urlencoding::encode(&key_name)
    ))
}

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

/// Authentication method for Azure Service Bus
#[derive(Clone, Serialize, Deserialize)]
pub enum AzureAuthMethod {
    /// Connection string with embedded credentials
    ConnectionString,
    /// Azure Managed Identity (for serverless environments)
    ManagedIdentity,
    /// Service principal with client secret
    ClientSecret {
        tenant_id: String,
        client_id: String,
        client_secret: String,
    },
    /// Default Azure credential chain (for development)
    DefaultCredential,
}

impl fmt::Debug for AzureAuthMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ConnectionString => f.debug_struct("ConnectionString").finish(),
            Self::ManagedIdentity => f.debug_struct("ManagedIdentity").finish(),
            Self::ClientSecret {
                tenant_id,
                client_id,
                ..
            } => f
                .debug_struct("ClientSecret")
                .field("tenant_id", tenant_id)
                .field("client_id", client_id)
                .field("client_secret", &"<REDACTED>")
                .finish(),
            Self::DefaultCredential => f.debug_struct("DefaultCredential").finish(),
        }
    }
}

impl fmt::Display for AzureAuthMethod {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ConnectionString => write!(f, "ConnectionString"),
            Self::ManagedIdentity => write!(f, "ManagedIdentity"),
            Self::ClientSecret { .. } => write!(f, "ClientSecret"),
            Self::DefaultCredential => write!(f, "DefaultCredential"),
        }
    }
}

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

/// Azure Service Bus specific errors
#[derive(Debug, thiserror::Error)]
pub enum AzureError {
    #[error("Authentication failed: {0}")]
    AuthenticationError(String),

    #[error("Network error: {0}")]
    NetworkError(String),

    #[error("Service Bus error: {0}")]
    ServiceBusError(String),

    #[error("Message lock lost: {0}")]
    MessageLockLost(String),

    #[error("Session lock lost: {0}")]
    SessionLockLost(String),

    #[error("Invalid configuration: {0}")]
    ConfigurationError(String),

    #[error("Serialization error: {0}")]
    SerializationError(String),
}

impl AzureError {
    /// Check if error is transient and should be retried
    pub fn is_transient(&self) -> bool {
        match self {
            Self::AuthenticationError(_) => false,
            Self::NetworkError(_) => true,
            Self::ServiceBusError(_) => true, // Most Service Bus errors are transient
            Self::MessageLockLost(_) => false,
            Self::SessionLockLost(_) => false,
            Self::ConfigurationError(_) => false,
            Self::SerializationError(_) => false,
        }
    }

    /// Map Azure error to QueueError
    pub fn to_queue_error(self) -> QueueError {
        match self {
            Self::AuthenticationError(msg) => QueueError::AuthenticationFailed { message: msg },
            Self::NetworkError(msg) => QueueError::ConnectionFailed { message: msg },
            Self::ServiceBusError(msg) => QueueError::ProviderError {
                provider: "AzureServiceBus".to_string(),
                code: "ServiceBusError".to_string(),
                message: msg,
            },
            Self::MessageLockLost(msg) => QueueError::InvalidReceipt { receipt: msg },
            Self::SessionLockLost(session_id) => QueueError::SessionNotFound { session_id },
            Self::ConfigurationError(msg) => {
                QueueError::ConfigurationError(ConfigurationError::Invalid { message: msg })
            }
            Self::SerializationError(msg) => QueueError::SerializationError(
                SerializationError::JsonError(serde_json::Error::io(std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    msg,
                ))),
            ),
        }
    }
}

// ============================================================================
// Azure Service Bus Provider
// ============================================================================

/// Azure Service Bus queue provider implementation using REST API
///
/// This provider implements the QueueProvider trait using Azure Service Bus REST API.
/// It supports:
/// - Multiple authentication methods (connection string, managed identity, service principal)
/// - HTTP-based message operations (send, receive, complete, abandon, dead-letter)
/// - Session support for ordered processing
/// - Lock token management for PeekLock receive mode
/// - Comprehensive error classification with retry logic
pub struct AzureServiceBusProvider {
    config: AzureServiceBusConfig,
    http_client: HttpClient,
    namespace_url: String,
    credential: Option<Arc<dyn TokenCredential + Send + Sync>>,
    // Cached lock tokens: receipt_handle -> (lock_token, queue_name)
    lock_tokens: Arc<RwLock<HashMap<String, (String, String)>>>,
}

impl fmt::Debug for AzureServiceBusProvider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AzureServiceBusProvider")
            .field("config", &self.config)
            .field("namespace_url", &self.namespace_url)
            .field(
                "credential",
                &self.credential.as_ref().map(|_| "<TokenCredential>"),
            )
            .field("lock_tokens", &self.lock_tokens)
            .finish()
    }
}

impl AzureServiceBusProvider {
    /// Create new Azure Service Bus provider
    ///
    /// # Arguments
    ///
    /// * `config` - Azure Service Bus configuration with authentication details
    ///
    /// # Errors
    ///
    /// Returns error if:
    /// - Connection string is invalid
    /// - Authentication fails
    /// - Namespace is not accessible
    ///
    /// # Example
    ///
    /// ```no_run
    /// use queue_runtime::{AzureServiceBusConfig, AzureAuthMethod};
    /// use queue_runtime::providers::AzureServiceBusProvider;
    /// use chrono::Duration;
    ///
    /// # async fn example() -> Result<(), Box<dyn std::error::Error>> {
    /// let config = AzureServiceBusConfig {
    ///     connection_string: Some("Endpoint=sb://...".to_string()),
    ///     namespace: None,
    ///     auth_method: AzureAuthMethod::ConnectionString,
    ///     use_sessions: true,
    ///     session_timeout: Duration::minutes(5),
    /// };
    ///
    /// let provider = AzureServiceBusProvider::new(config).await?;
    /// # Ok(())
    /// # }
    /// ```
    pub async fn new(config: AzureServiceBusConfig) -> Result<Self, AzureError> {
        // Validate configuration
        Self::validate_config(&config)?;

        // Extract namespace URL and setup authentication
        let (namespace_url, credential) = match &config.auth_method {
            AzureAuthMethod::ConnectionString => {
                let conn_str = config.connection_string.as_ref().ok_or_else(|| {
                    AzureError::ConfigurationError(
                        "Connection string required for ConnectionString auth".to_string(),
                    )
                })?;

                let namespace_url = Self::parse_connection_string_endpoint(conn_str)?;
                (namespace_url, None)
            }
            AzureAuthMethod::ManagedIdentity => {
                let namespace = config.namespace.as_ref().ok_or_else(|| {
                    AzureError::ConfigurationError(
                        "Namespace required for ManagedIdentity auth".to_string(),
                    )
                })?;

                let credential = ManagedIdentityCredential::new(None).map_err(|e| {
                    AzureError::ConfigurationError(format!(
                        "Failed to create managed identity credential: {}",
                        e
                    ))
                })?;
                let namespace_url = format!("https://{}.servicebus.windows.net", namespace);
                (
                    namespace_url,
                    Some(credential as Arc<dyn TokenCredential + Send + Sync>),
                )
            }
            AzureAuthMethod::ClientSecret {
                ref tenant_id,
                ref client_id,
                ref client_secret,
            } => {
                let namespace = config.namespace.as_ref().ok_or_else(|| {
                    AzureError::ConfigurationError(
                        "Namespace required for ClientSecret auth".to_string(),
                    )
                })?;

                // Create ClientSecretCredential with new API
                let credential = ClientSecretCredential::new(
                    tenant_id,
                    client_id.clone(),
                    AzureSecret::from(client_secret.clone()),
                    None::<ClientSecretCredentialOptions>,
                )
                .map_err(|e| {
                    AzureError::ConfigurationError(format!("Failed to create credential: {}", e))
                })?;
                let namespace_url = format!("https://{}.servicebus.windows.net", namespace);
                (
                    namespace_url,
                    Some(credential as Arc<dyn TokenCredential + Send + Sync>),
                )
            }
            AzureAuthMethod::DefaultCredential => {
                let namespace = config.namespace.as_ref().ok_or_else(|| {
                    AzureError::ConfigurationError(
                        "Namespace required for DefaultCredential auth".to_string(),
                    )
                })?;

                // Use DeveloperToolsCredential (Azure CLI → azd chain) for local development.
                // In production workloads, prefer the explicit ManagedIdentity variant.
                let credential = DeveloperToolsCredential::new(None).map_err(|e| {
                    AzureError::ConfigurationError(format!(
                        "Failed to create developer tools credential: {}",
                        e
                    ))
                })?;
                let namespace_url = format!("https://{}.servicebus.windows.net", namespace);
                (
                    namespace_url,
                    Some(credential as Arc<dyn TokenCredential + Send + Sync>),
                )
            }
        };

        // Create HTTP client
        let http_client = HttpClient::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .map_err(|e| {
                AzureError::NetworkError(format!("Failed to create HTTP client: {}", e))
            })?;

        Ok(Self {
            config,
            http_client,
            namespace_url,
            credential,
            lock_tokens: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Parse endpoint from connection string
    fn parse_connection_string_endpoint(conn_str: &str) -> Result<String, AzureError> {
        for part in conn_str.split(';') {
            if let Some(endpoint) = part.strip_prefix("Endpoint=") {
                return Ok(endpoint.trim_end_matches('/').to_string());
            }
        }
        Err(AzureError::ConfigurationError(
            "Invalid connection string: missing Endpoint".to_string(),
        ))
    }

    /// Validate Azure Service Bus configuration
    fn validate_config(config: &AzureServiceBusConfig) -> Result<(), AzureError> {
        match &config.auth_method {
            AzureAuthMethod::ConnectionString => {
                if config.connection_string.is_none() {
                    return Err(AzureError::ConfigurationError(
                        "Connection string required for ConnectionString auth method".to_string(),
                    ));
                }
            }
            AzureAuthMethod::ManagedIdentity | AzureAuthMethod::DefaultCredential => {
                if config.namespace.is_none() {
                    return Err(AzureError::ConfigurationError(
                        "Namespace required for ManagedIdentity/DefaultCredential auth".to_string(),
                    ));
                }
            }
            AzureAuthMethod::ClientSecret {
                tenant_id,
                client_id,
                client_secret,
            } => {
                if config.namespace.is_none() {
                    return Err(AzureError::ConfigurationError(
                        "Namespace required for ClientSecret auth".to_string(),
                    ));
                }
                if tenant_id.is_empty() || client_id.is_empty() || client_secret.is_empty() {
                    return Err(AzureError::ConfigurationError(
                        "Tenant ID, Client ID, and Client Secret required for ClientSecret auth"
                            .to_string(),
                    ));
                }
            }
        }

        Ok(())
    }

    /// Get authentication token for Service Bus operations
    async fn get_auth_token(&self) -> Result<String, AzureError> {
        match &self.credential {
            Some(cred) => get_bearer_token(cred.as_ref()).await,
            None => {
                // Connection string auth - parse SharedAccessSignature
                self.get_sas_token()
            }
        }
    }

    /// Extract SAS token from connection string.
    ///
    /// Delegates to the module-level [`generate_sas_token`] helper.
    fn get_sas_token(&self) -> Result<String, AzureError> {
        let conn_str = self.config.connection_string.as_ref().ok_or_else(|| {
            AzureError::AuthenticationError("No connection string available".to_string())
        })?;
        generate_sas_token(&self.namespace_url, conn_str)
    }
}

// ============================================================================
// Azure Service Bus REST API Types
// ============================================================================

/// Message body for sending messages
#[derive(Debug, Serialize, Deserialize)]
struct ServiceBusMessageBody {
    #[serde(rename = "ContentType")]
    content_type: String,
    #[serde(rename = "Body")]
    body: String, // Base64-encoded
    #[serde(rename = "BrokerProperties")]
    broker_properties: BrokerProperties,
}

#[derive(Debug, Serialize, Deserialize)]
struct BrokerProperties {
    #[serde(rename = "MessageId")]
    message_id: String,
    #[serde(rename = "SessionId", skip_serializing_if = "Option::is_none")]
    session_id: Option<String>,
    #[serde(rename = "TimeToLive", skip_serializing_if = "Option::is_none")]
    time_to_live: Option<u64>,
}

/// Batch receive response structure
#[derive(Debug, Deserialize)]
struct ServiceBusMessageResponse {
    #[serde(rename = "Body")]
    body: String,
    #[serde(rename = "BrokerProperties")]
    broker_properties: ReceivedBrokerProperties,
}

#[allow(dead_code)] // Used when receive operations are implemented
#[derive(Debug, Deserialize)]
struct ReceivedServiceBusMessage {
    #[serde(rename = "Body")]
    body: String,
    #[serde(rename = "BrokerProperties")]
    broker_properties: ReceivedBrokerProperties,
}

#[allow(dead_code)] // Used when receive operations are implemented
#[derive(Debug, Deserialize)]
struct ReceivedBrokerProperties {
    #[serde(rename = "MessageId")]
    message_id: String,
    #[serde(rename = "SessionId")]
    session_id: Option<String>,
    #[serde(rename = "LockToken")]
    lock_token: String,
    #[serde(rename = "DeliveryCount")]
    delivery_count: u32,
    #[serde(rename = "EnqueuedTimeUtc")]
    enqueued_time_utc: String,
}

// ============================================================================
// QueueProvider Implementation
// ============================================================================

#[async_trait]
impl QueueProvider for AzureServiceBusProvider {
    async fn send_message(
        &self,
        queue: &QueueName,
        message: &Message,
    ) -> Result<MessageId, QueueError> {
        // Generate message ID
        let message_id = MessageId::new();

        // Serialize message body (it's already Bytes, just base64 encode it)
        use base64::{engine::general_purpose::STANDARD, Engine};
        let body_base64 = STANDARD.encode(&message.body);

        // Build broker properties
        let broker_props = BrokerProperties {
            message_id: message_id.to_string(),
            session_id: message.session_id.as_ref().map(|s| s.to_string()),
            time_to_live: message
                .time_to_live
                .as_ref()
                .map(|ttl| ttl.num_seconds() as u64),
        };

        // Build URL: {namespace}/{queue}/messages
        let url = format!("{}/{}/messages", self.namespace_url, queue.as_str());

        // Get auth token
        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        // Send HTTP POST request
        let response = self
            .http_client
            .post(&url)
            .header(header::AUTHORIZATION, auth_token)
            .header(
                header::CONTENT_TYPE,
                "application/atom+xml;type=entry;charset=utf-8",
            )
            .header(
                "BrokerProperties",
                serde_json::to_string(&broker_props).unwrap(),
            )
            .body(body_base64)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        // Check response status
        match response.status() {
            StatusCode::CREATED | StatusCode::OK => Ok(message_id),
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Send failed: {}", error_body),
                })
            }
        }
    }

    async fn send_messages(
        &self,
        queue: &QueueName,
        messages: &[Message],
    ) -> Result<Vec<MessageId>, QueueError> {
        // Azure Service Bus supports batch send (max 100 messages)
        if messages.len() > 100 {
            return Err(QueueError::BatchTooLarge {
                size: messages.len(),
                max_size: 100,
            });
        }

        if messages.is_empty() {
            return Ok(Vec::new());
        }

        // Build batch request body - array of messages
        let mut batch_messages = Vec::with_capacity(messages.len());
        let mut message_ids = Vec::with_capacity(messages.len());

        use base64::{engine::general_purpose::STANDARD, Engine};

        for message in messages {
            let message_id = MessageId::new();
            let body_base64 = STANDARD.encode(&message.body);

            let broker_props = BrokerProperties {
                message_id: message_id.to_string(),
                session_id: message.session_id.as_ref().map(|s| s.to_string()),
                time_to_live: message
                    .time_to_live
                    .as_ref()
                    .map(|ttl| ttl.num_seconds() as u64),
            };

            batch_messages.push(ServiceBusMessageBody {
                content_type: "application/octet-stream".to_string(),
                body: body_base64,
                broker_properties: broker_props,
            });

            message_ids.push(message_id);
        }

        // Build URL: {namespace}/{queue}/messages
        let url = format!("{}/{}/messages", self.namespace_url, queue.as_str());

        // Get auth token
        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        // Send batch HTTP POST request with JSON array
        let response = self
            .http_client
            .post(&url)
            .header(header::AUTHORIZATION, auth_token)
            .header(header::CONTENT_TYPE, "application/json")
            .json(&batch_messages)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("Batch send HTTP request failed: {}", e))
                    .to_queue_error()
            })?;

        // Check response status
        match response.status() {
            StatusCode::CREATED | StatusCode::OK => Ok(message_ids),
            StatusCode::PAYLOAD_TOO_LARGE => Err(QueueError::BatchTooLarge {
                size: messages.len(),
                max_size: 100,
            }),
            StatusCode::TOO_MANY_REQUESTS => {
                let retry_after = response
                    .headers()
                    .get("Retry-After")
                    .and_then(|v| v.to_str().ok())
                    .and_then(|s| s.parse::<u64>().ok())
                    .unwrap_or(30);

                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: "ThrottlingError".to_string(),
                    message: format!("Request throttled, retry after {} seconds", retry_after),
                })
            }
            StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::AuthenticationFailed {
                    message: format!("Authentication failed: {}", error_body),
                })
            }
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Batch send failed: {}", error_body),
                })
            }
        }
    }

    async fn receive_message(
        &self,
        queue: &QueueName,
        timeout: Duration,
    ) -> Result<Option<ReceivedMessage>, QueueError> {
        // Azure Service Bus receive uses HTTP DELETE with peek-lock
        // URL: {namespace}/{queue}/messages/head?timeout={seconds}
        let url = format!(
            "{}/{}/messages/head?timeout={}",
            self.namespace_url,
            queue.as_str(),
            timeout.num_seconds()
        );

        // Get auth token
        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        // Send HTTP DELETE request (peek-lock mode)
        let response = self
            .http_client
            .delete(&url)
            .header(header::AUTHORIZATION, auth_token)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        // Check response status
        match response.status() {
            StatusCode::OK | StatusCode::CREATED => {
                // Parse BrokerProperties from response header
                let broker_props = response
                    .headers()
                    .get("BrokerProperties")
                    .and_then(|v| v.to_str().ok())
                    .and_then(|s| serde_json::from_str::<ReceivedBrokerProperties>(s).ok())
                    .ok_or_else(|| QueueError::ProviderError {
                        provider: "AzureServiceBus".to_string(),
                        code: "InvalidResponse".to_string(),
                        message: "Missing or invalid BrokerProperties header".to_string(),
                    })?;

                // Get message body (base64 encoded)
                let body_base64 = response.text().await.map_err(|e| {
                    AzureError::NetworkError(format!("Failed to read response body: {}", e))
                        .to_queue_error()
                })?;

                // Decode base64 body
                use base64::{engine::general_purpose::STANDARD, Engine};
                let body =
                    STANDARD
                        .decode(&body_base64)
                        .map_err(|e| QueueError::ProviderError {
                            provider: "AzureServiceBus".to_string(),
                            code: "DecodingError".to_string(),
                            message: format!("Failed to decode message body: {}", e),
                        })?;

                // Parse enqueued time
                let first_delivered_at =
                    chrono::DateTime::parse_from_rfc3339(&broker_props.enqueued_time_utc)
                        .map(|dt| Timestamp::from_datetime(dt.with_timezone(&chrono::Utc)))
                        .unwrap_or_else(|_| Timestamp::now());

                // Create receipt handle combining lock token and queue name
                // Lock expires in 30 seconds by default (Azure Service Bus default)
                let expires_at = Timestamp::from_datetime(Utc::now() + Duration::seconds(30));
                let receipt_str = format!("{}::{}", broker_props.lock_token, queue.as_str());
                let receipt = ReceiptHandle::new(
                    receipt_str.clone(),
                    expires_at,
                    ProviderType::AzureServiceBus,
                );

                // Store lock token for later acknowledgment
                self.lock_tokens.write().await.insert(
                    receipt_str,
                    (broker_props.lock_token.clone(), queue.as_str().to_string()),
                );

                // Parse Azure message ID
                let message_id = MessageId::from_str(&broker_props.message_id)
                    .unwrap_or_else(|_| MessageId::new());

                // Create received message
                let received_message = ReceivedMessage {
                    message_id,
                    body: bytes::Bytes::from(body),
                    attributes: HashMap::new(),
                    session_id: broker_props.session_id.map(SessionId::new).transpose()?,
                    correlation_id: None,
                    receipt_handle: receipt,
                    delivery_count: broker_props.delivery_count,
                    first_delivered_at,
                    delivered_at: Timestamp::now(),
                };

                Ok(Some(received_message))
            }
            StatusCode::NO_CONTENT => {
                // No messages available
                Ok(None)
            }
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Receive failed: {}", error_body),
                })
            }
        }
    }

    async fn receive_messages(
        &self,
        queue: &QueueName,
        max_messages: u32,
        timeout: Duration,
    ) -> Result<Vec<ReceivedMessage>, QueueError> {
        // Azure Service Bus max batch receive is 32 messages
        if max_messages > 32 {
            return Err(QueueError::BatchTooLarge {
                size: max_messages as usize,
                max_size: 32,
            });
        }

        if max_messages == 0 {
            return Ok(Vec::new());
        }

        // Build URL with maxMessageCount parameter for batch receive
        // {namespace}/{queue}/messages/head?timeout={seconds}&maxMessageCount={count}
        let url = format!(
            "{}/{}/messages/head?timeout={}&maxMessageCount={}",
            self.namespace_url,
            queue.as_str(),
            timeout.num_seconds(),
            max_messages
        );

        // Get auth token
        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        // Receive messages using HTTP DELETE (PeekLock mode)
        let response = self
            .http_client
            .delete(&url)
            .header(header::AUTHORIZATION, auth_token)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("Batch receive HTTP request failed: {}", e))
                    .to_queue_error()
            })?;

        // Parse response
        match response.status() {
            StatusCode::OK | StatusCode::CREATED => {
                // Parse JSON array response
                let messages_data: Vec<ServiceBusMessageResponse> =
                    response.json().await.map_err(|e| {
                        AzureError::SerializationError(format!(
                            "Failed to parse batch receive response: {}",
                            e
                        ))
                        .to_queue_error()
                    })?;

                let mut received_messages = Vec::with_capacity(messages_data.len());

                use base64::{engine::general_purpose::STANDARD, Engine};

                for msg_data in messages_data {
                    let broker_props = msg_data.broker_properties;

                    // Decode base64 body
                    let body = STANDARD.decode(&msg_data.body).map_err(|e| {
                        AzureError::SerializationError(format!(
                            "Failed to decode message body: {}",
                            e
                        ))
                        .to_queue_error()
                    })?;

                    // Parse enqueued time
                    let enqueued_time =
                        chrono::DateTime::parse_from_rfc3339(&broker_props.enqueued_time_utc)
                            .map_err(|e| {
                                AzureError::SerializationError(format!(
                                    "Failed to parse enqueued time: {}",
                                    e
                                ))
                                .to_queue_error()
                            })?;
                    let first_delivered_at =
                        Timestamp::from_datetime(enqueued_time.with_timezone(&Utc));

                    // Create receipt handle with lock expiration (30s default)
                    let expires_at = Timestamp::from_datetime(Utc::now() + Duration::seconds(30));
                    let receipt_str = format!("{}::{}", broker_props.lock_token, queue.as_str());
                    let receipt = ReceiptHandle::new(
                        receipt_str.clone(),
                        expires_at,
                        ProviderType::AzureServiceBus,
                    );

                    // Store lock token for acknowledgment
                    self.lock_tokens.write().await.insert(
                        receipt_str,
                        (broker_props.lock_token.clone(), queue.as_str().to_string()),
                    );

                    // Parse Azure message ID
                    let message_id = MessageId::from_str(&broker_props.message_id)
                        .unwrap_or_else(|_| MessageId::new());

                    // Create received message
                    let received_message = ReceivedMessage {
                        message_id,
                        body: bytes::Bytes::from(body),
                        attributes: HashMap::new(),
                        session_id: broker_props.session_id.map(SessionId::new).transpose()?,
                        correlation_id: None,
                        receipt_handle: receipt,
                        delivery_count: broker_props.delivery_count,
                        first_delivered_at,
                        delivered_at: Timestamp::now(),
                    };

                    received_messages.push(received_message);
                }

                Ok(received_messages)
            }
            StatusCode::NO_CONTENT => {
                // No messages available
                Ok(Vec::new())
            }
            StatusCode::TOO_MANY_REQUESTS => {
                let retry_after = response
                    .headers()
                    .get("Retry-After")
                    .and_then(|v| v.to_str().ok())
                    .and_then(|s| s.parse::<u64>().ok())
                    .unwrap_or(30);

                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: "ThrottlingError".to_string(),
                    message: format!("Request throttled, retry after {} seconds", retry_after),
                })
            }
            StatusCode::UNAUTHORIZED | StatusCode::FORBIDDEN => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::AuthenticationFailed {
                    message: format!("Authentication failed: {}", error_body),
                })
            }
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Batch receive failed: {}", error_body),
                })
            }
        }
    }

    async fn complete_message(&self, receipt: &ReceiptHandle) -> Result<(), QueueError> {
        // Extract lock token and queue name from receipt handle
        let lock_tokens = self.lock_tokens.read().await;
        let (lock_token, queue_name) =
            lock_tokens
                .get(receipt.handle())
                .ok_or_else(|| QueueError::InvalidReceipt {
                    receipt: receipt.handle().to_string(),
                })?;

        // Azure Service Bus complete uses HTTP DELETE to {namespace}/{queue}/messages/{messageId}/{lockToken}
        let url = format!(
            "{}/{}/messages/head/{}",
            self.namespace_url,
            queue_name,
            urlencoding::encode(lock_token)
        );

        // Get auth token
        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        // Send HTTP DELETE request
        let response = self
            .http_client
            .delete(&url)
            .header(header::AUTHORIZATION, auth_token)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        // Check response status
        match response.status() {
            StatusCode::OK | StatusCode::NO_CONTENT => {
                // Remove lock token from cache
                drop(lock_tokens);
                self.lock_tokens.write().await.remove(receipt.handle());
                Ok(())
            }
            StatusCode::GONE | StatusCode::NOT_FOUND => {
                // Lock expired or message already processed
                Err(QueueError::InvalidReceipt {
                    receipt: receipt.handle().to_string(),
                })
            }
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Complete failed: {}", error_body),
                })
            }
        }
    }

    async fn abandon_message(&self, receipt: &ReceiptHandle) -> Result<(), QueueError> {
        // Extract lock token and queue name from receipt handle
        let lock_tokens = self.lock_tokens.read().await;
        let (lock_token, queue_name) =
            lock_tokens
                .get(receipt.handle())
                .ok_or_else(|| QueueError::InvalidReceipt {
                    receipt: receipt.handle().to_string(),
                })?;

        // Azure Service Bus abandon uses HTTP PUT to {namespace}/{queue}/messages/{messageId}/{lockToken}
        // with empty body to unlock the message
        let url = format!(
            "{}/{}/messages/head/{}",
            self.namespace_url,
            queue_name,
            urlencoding::encode(lock_token)
        );

        // Get auth token
        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        // Send HTTP PUT request with empty body to abandon
        let response = self
            .http_client
            .put(&url)
            .header(header::AUTHORIZATION, auth_token)
            .header(header::CONTENT_LENGTH, "0")
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        // Check response status
        match response.status() {
            StatusCode::OK | StatusCode::NO_CONTENT => {
                // Remove lock token from cache
                drop(lock_tokens);
                self.lock_tokens.write().await.remove(receipt.handle());
                Ok(())
            }
            StatusCode::GONE | StatusCode::NOT_FOUND => {
                // Lock expired or message already processed
                Err(QueueError::InvalidReceipt {
                    receipt: receipt.handle().to_string(),
                })
            }
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Abandon failed: {}", error_body),
                })
            }
        }
    }

    async fn dead_letter_message(
        &self,
        receipt: &ReceiptHandle,
        reason: &str,
    ) -> Result<(), QueueError> {
        // Extract lock token and queue name from receipt handle
        let lock_tokens = self.lock_tokens.read().await;
        let (lock_token, queue_name) =
            lock_tokens
                .get(receipt.handle())
                .ok_or_else(|| QueueError::InvalidReceipt {
                    receipt: receipt.handle().to_string(),
                })?;

        // Azure Service Bus dead letter uses HTTP DELETE to {namespace}/{queue}/messages/{messageId}/{lockToken}
        // with custom properties in the DeadLetterReason header
        let url = format!(
            "{}/{}/messages/head/{}/$deadletter",
            self.namespace_url,
            queue_name,
            urlencoding::encode(lock_token)
        );

        // Get auth token
        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        // Build dead letter properties as JSON
        let properties = serde_json::json!({
            "DeadLetterReason": reason,
            "DeadLetterErrorDescription": "Message processing failed"
        });

        // Send HTTP POST request to dead letter
        let response = self
            .http_client
            .post(&url)
            .header(header::AUTHORIZATION, auth_token)
            .header(header::CONTENT_TYPE, "application/json")
            .json(&properties)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        // Check response status
        match response.status() {
            StatusCode::OK | StatusCode::NO_CONTENT | StatusCode::CREATED => {
                // Remove lock token from cache
                drop(lock_tokens);
                self.lock_tokens.write().await.remove(receipt.handle());
                Ok(())
            }
            StatusCode::GONE | StatusCode::NOT_FOUND => {
                // Lock expired or message already processed
                Err(QueueError::InvalidReceipt {
                    receipt: receipt.handle().to_string(),
                })
            }
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Dead letter failed: {}", error_body),
                })
            }
        }
    }

    async fn create_session_client(
        &self,
        queue: &QueueName,
        session_id: Option<SessionId>,
    ) -> Result<Box<dyn SessionProvider>, QueueError> {
        let resolved_id = match session_id {
            Some(id) => id,
            None => self.accept_next_available_session(queue).await?,
        };

        Ok(Box::new(AzureSessionProvider::new(
            resolved_id,
            queue.clone(),
            self.config.session_timeout,
            self.http_client.clone(),
            self.namespace_url.clone(),
            self.config.clone(),
            self.credential.clone(),
        )))
    }

    fn provider_type(&self) -> ProviderType {
        ProviderType::AzureServiceBus
    }

    fn supports_sessions(&self) -> SessionSupport {
        SessionSupport::Native
    }

    fn supports_batching(&self) -> bool {
        true
    }

    fn max_batch_size(&self) -> u32 {
        100 // Azure Service Bus max batch send
    }
}

impl AzureServiceBusProvider {
    /// Accept the next available session by receiving the first available message
    /// from the queue and deriving the session ID from its broker properties.
    ///
    /// The Azure Service Bus REST API does **not** have an atomic
    /// "accept-next-session" endpoint (unlike the AMQP SDK). Enumerating
    /// sessions via GET and then receiving from one introduces a TOCTOU race:
    /// two concurrent consumers can read the same session ID and collide.
    ///
    /// To avoid the race this implementation calls
    /// `DELETE {namespace}/{queue}/sessions/$acceptnext/messages/head`, which is
    /// the undocumented but well-established REST shorthand supported by the
    /// Azure Service Bus broker for atomically accepting the next available
    /// session. The session ID is taken from the `BrokerProperties.SessionId`
    /// header in the response.
    ///
    /// # Errors
    ///
    /// - `QueueError::ProviderError { code: "NoSessionsAvailable" }` when no
    ///   session has pending messages or the timeout expires.
    /// - `QueueError::QueueNotFound` when the queue does not exist.
    /// - Network or auth errors on failure.
    async fn accept_next_available_session(
        &self,
        queue: &QueueName,
    ) -> Result<SessionId, QueueError> {
        // `$acceptnext` is the REST equivalent of AcceptNextSessionAsync in the SDK:
        // the broker atomically picks and locks the next session with pending messages.
        let timeout_secs = self.config.session_timeout.num_seconds().max(1);
        let url = format!(
            "{}/{}/sessions/$acceptnext/messages/head?timeout={}",
            self.namespace_url,
            queue.as_str(),
            timeout_secs
        );

        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        let response = self
            .http_client
            .delete(&url)
            .header(header::AUTHORIZATION, auth_token)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("Failed to accept next session: {}", e))
                    .to_queue_error()
            })?;

        match response.status() {
            StatusCode::OK | StatusCode::CREATED => {
                // Parse BrokerProperties from response header to get the session ID.
                let broker_props = response
                    .headers()
                    .get("BrokerProperties")
                    .and_then(|v| v.to_str().ok())
                    .and_then(|s| serde_json::from_str::<ReceivedBrokerProperties>(s).ok())
                    .ok_or_else(|| QueueError::ProviderError {
                        provider: "AzureServiceBus".to_string(),
                        code: "InvalidResponse".to_string(),
                        message: "Missing BrokerProperties in accept-next-session response"
                            .to_string(),
                    })?;

                let session_id_str =
                    broker_props
                        .session_id
                        .ok_or_else(|| QueueError::ProviderError {
                            provider: "AzureServiceBus".to_string(),
                            code: "NoSessionId".to_string(),
                            message: "Accepted message has no SessionId".to_string(),
                        })?;

                SessionId::new(session_id_str).map_err(|e| QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: "InvalidSessionId".to_string(),
                    message: format!("Invalid session ID returned by broker: {}", e),
                })
            }
            StatusCode::NO_CONTENT => Err(QueueError::ProviderError {
                provider: "AzureServiceBus".to_string(),
                code: "NoSessionsAvailable".to_string(),
                message: "No sessions with pending messages are available".to_string(),
            }),
            StatusCode::NOT_FOUND => Err(QueueError::QueueNotFound {
                queue_name: queue.to_string(),
            }),
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Accept next session failed: {}", error_body),
                })
            }
        }
    }
}

// ============================================================================
// Azure Session Provider
// ============================================================================

/// Azure Service Bus session provider for ordered message processing.
///
/// Implements the [`SessionProvider`] trait using the Azure Service Bus REST API,
/// providing exclusive session-locked access to messages within a single session.
/// All messages within a session are delivered in strict FIFO order.
///
/// ## Session Lifecycle
///
/// 1. Obtain via [`AzureServiceBusProvider::create_session_client`].
/// 2. Call [`receive_message`](SessionProvider::receive_message) to fetch the next message.
/// 3. Process the message, then call [`complete_message`](SessionProvider::complete_message)
///    or [`abandon_message`](SessionProvider::abandon_message).
/// 4. Call [`renew_session_lock`](SessionProvider::renew_session_lock) periodically for
///    long-running processing to prevent session lock expiry.
/// 5. Call [`close_session`](SessionProvider::close_session) when finished.
pub struct AzureSessionProvider {
    session_id: SessionId,
    queue_name: QueueName,
    /// Session lock expiry. Uses `std::sync::RwLock` so the synchronous
    /// `session_expires_at()` trait method can read without async.
    session_expires_at: Arc<std::sync::RwLock<Timestamp>>,
    http_client: HttpClient,
    namespace_url: String,
    config: AzureServiceBusConfig,
    credential: Option<Arc<dyn TokenCredential + Send + Sync>>,
    /// Lock tokens for in-flight messages. The receipt handle IS the lock token
    /// for session messages, so a set suffices (no separate value).
    lock_tokens: Arc<RwLock<HashSet<String>>>,
}

impl fmt::Debug for AzureSessionProvider {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("AzureSessionProvider")
            .field("session_id", &self.session_id)
            .field("queue_name", &self.queue_name)
            .field("namespace_url", &self.namespace_url)
            .field(
                "credential",
                &self.credential.as_ref().map(|_| "<TokenCredential>"),
            )
            .finish()
    }
}

impl AzureSessionProvider {
    /// Create a new session provider.
    ///
    /// Normally obtained through [`AzureServiceBusProvider::create_session_client`]
    /// rather than constructed directly.
    ///
    /// # Arguments
    ///
    /// * `session_id` - The session to operate on.
    /// * `queue_name` - The queue containing the session.
    /// * `session_timeout` - How long the session lock is expected to be held; used to
    ///   compute `session_expires_at` and refreshed on each receive and lock renewal.
    /// * `http_client` - Shared HTTP client (cloned from the parent provider).
    /// * `namespace_url` - Base URL of the Service Bus namespace.
    /// * `config` - Provider configuration (used for SAS token generation).
    /// * `credential` - Optional token credential for AAD-based auth.
    pub fn new(
        session_id: SessionId,
        queue_name: QueueName,
        session_timeout: Duration,
        http_client: HttpClient,
        namespace_url: String,
        config: AzureServiceBusConfig,
        credential: Option<Arc<dyn TokenCredential + Send + Sync>>,
    ) -> Self {
        let session_expires_at = Timestamp::from_datetime(Utc::now() + session_timeout);
        Self {
            session_id,
            queue_name,
            session_expires_at: Arc::new(std::sync::RwLock::new(session_expires_at)),
            http_client,
            namespace_url,
            config,
            credential,
            lock_tokens: Arc::new(RwLock::new(HashSet::new())),
        }
    }

    /// Get an authentication token for Service Bus REST operations.
    ///
    /// Delegates to [`get_bearer_token`] for AAD credentials and [`generate_sas_token`] for SAS.
    async fn get_auth_token(&self) -> Result<String, AzureError> {
        match &self.credential {
            Some(cred) => get_bearer_token(cred.as_ref()).await,
            None => {
                let conn_str = self.config.connection_string.as_ref().ok_or_else(|| {
                    AzureError::AuthenticationError("No connection string available".to_string())
                })?;
                generate_sas_token(&self.namespace_url, conn_str)
            }
        }
    }

    /// Refresh the local session expiry to `now + session_timeout`.
    fn refresh_session_expiry(&self) {
        if let Ok(mut expiry) = self.session_expires_at.write() {
            *expiry = Timestamp::from_datetime(Utc::now() + self.config.session_timeout);
        }
    }
}

#[async_trait]
impl SessionProvider for AzureSessionProvider {
    /// Receive the next message from the session using PeekLock mode.
    ///
    /// Calls `DELETE {namespace}/{queue}/sessions/{sessionId}/messages/head?timeout={t}`.
    /// On success the session lock expiry is refreshed and the message lock token is
    /// stored internally so that [`complete_message`](Self::complete_message),
    /// [`abandon_message`](Self::abandon_message), and
    /// [`dead_letter_message`](Self::dead_letter_message) can resolve the token by
    /// receipt handle.
    ///
    /// # Errors
    ///
    /// - `QueueError::SessionNotFound` – the session no longer exists or the lock expired.
    /// - `QueueError::ProviderError` – network or broker error.
    async fn receive_message(
        &self,
        timeout: Duration,
    ) -> Result<Option<ReceivedMessage>, QueueError> {
        let url = format!(
            "{}/{}/sessions/{}/messages/head?timeout={}",
            self.namespace_url,
            self.queue_name.as_str(),
            urlencoding::encode(self.session_id.as_str()),
            timeout.num_seconds()
        );

        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        let response = self
            .http_client
            .delete(&url)
            .header(header::AUTHORIZATION, auth_token)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        match response.status() {
            StatusCode::OK | StatusCode::CREATED => {
                let broker_props = response
                    .headers()
                    .get("BrokerProperties")
                    .and_then(|v| v.to_str().ok())
                    .and_then(|s| serde_json::from_str::<ReceivedBrokerProperties>(s).ok())
                    .ok_or_else(|| QueueError::ProviderError {
                        provider: "AzureServiceBus".to_string(),
                        code: "InvalidResponse".to_string(),
                        message: "Missing or invalid BrokerProperties header".to_string(),
                    })?;

                let body_base64 = response.text().await.map_err(|e| {
                    AzureError::NetworkError(format!("Failed to read response body: {}", e))
                        .to_queue_error()
                })?;

                use base64::{engine::general_purpose::STANDARD, Engine};
                let body =
                    STANDARD
                        .decode(&body_base64)
                        .map_err(|e| QueueError::ProviderError {
                            provider: "AzureServiceBus".to_string(),
                            code: "DecodingError".to_string(),
                            message: format!("Failed to decode message body: {}", e),
                        })?;

                let first_delivered_at =
                    chrono::DateTime::parse_from_rfc3339(&broker_props.enqueued_time_utc)
                        .map(|dt| Timestamp::from_datetime(dt.with_timezone(&chrono::Utc)))
                        .unwrap_or_else(|_| Timestamp::now());

                // Receipt handle is the lock token; store it for later acknowledgement.
                // Use config.session_timeout as the ReceiptHandle local expiry to match
                // the session lock duration configured on the provider.
                let expires_at = Timestamp::from_datetime(Utc::now() + self.config.session_timeout);
                let lock_token = broker_props.lock_token.clone();
                let receipt = ReceiptHandle::new(
                    lock_token.clone(),
                    expires_at,
                    ProviderType::AzureServiceBus,
                );

                self.lock_tokens.write().await.insert(lock_token);

                let message_id = MessageId::from_str(&broker_props.message_id)
                    .unwrap_or_else(|_| MessageId::new());

                // Keep session lock alive.
                self.refresh_session_expiry();

                Ok(Some(ReceivedMessage {
                    message_id,
                    body: bytes::Bytes::from(body),
                    attributes: HashMap::new(),
                    session_id: Some(self.session_id.clone()),
                    correlation_id: None,
                    receipt_handle: receipt,
                    delivery_count: broker_props.delivery_count,
                    first_delivered_at,
                    delivered_at: Timestamp::now(),
                }))
            }
            StatusCode::NO_CONTENT => Ok(None),
            StatusCode::GONE | StatusCode::NOT_FOUND => Err(QueueError::SessionNotFound {
                session_id: self.session_id.to_string(),
            }),
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Session receive failed: {}", error_body),
                })
            }
        }
    }

    /// Complete (delete) a session message using its lock token.
    ///
    /// Calls `DELETE {namespace}/{queue}/sessions/{sessionId}/messages/{lockToken}`.
    ///
    /// # Errors
    ///
    /// - `QueueError::InvalidReceipt` – receipt not found locally or lock expired on broker.
    async fn complete_message(&self, receipt: &ReceiptHandle) -> Result<(), QueueError> {
        if !self.lock_tokens.read().await.contains(receipt.handle()) {
            return Err(QueueError::InvalidReceipt {
                receipt: receipt.handle().to_string(),
            });
        }
        let lock_token = receipt.handle().to_string();

        let url = format!(
            "{}/{}/sessions/{}/messages/{}",
            self.namespace_url,
            self.queue_name.as_str(),
            urlencoding::encode(self.session_id.as_str()),
            urlencoding::encode(&lock_token)
        );

        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        let response = self
            .http_client
            .delete(&url)
            .header(header::AUTHORIZATION, auth_token)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        match response.status() {
            StatusCode::OK | StatusCode::NO_CONTENT => {
                self.lock_tokens.write().await.remove(receipt.handle());
                Ok(())
            }
            StatusCode::GONE | StatusCode::NOT_FOUND => Err(QueueError::InvalidReceipt {
                receipt: receipt.handle().to_string(),
            }),
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Session complete failed: {}", error_body),
                })
            }
        }
    }

    /// Abandon a session message and make it available for re-delivery.
    ///
    /// Calls `PUT {namespace}/{queue}/sessions/{sessionId}/messages/{lockToken}`.
    ///
    /// # Errors
    ///
    /// - `QueueError::InvalidReceipt` – receipt not found locally or lock expired.
    async fn abandon_message(&self, receipt: &ReceiptHandle) -> Result<(), QueueError> {
        if !self.lock_tokens.read().await.contains(receipt.handle()) {
            return Err(QueueError::InvalidReceipt {
                receipt: receipt.handle().to_string(),
            });
        }
        let lock_token = receipt.handle().to_string();

        let url = format!(
            "{}/{}/sessions/{}/messages/{}",
            self.namespace_url,
            self.queue_name.as_str(),
            urlencoding::encode(self.session_id.as_str()),
            urlencoding::encode(&lock_token)
        );

        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        let response = self
            .http_client
            .put(&url)
            .header(header::AUTHORIZATION, auth_token)
            .header(header::CONTENT_LENGTH, "0")
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        match response.status() {
            StatusCode::OK | StatusCode::NO_CONTENT => {
                self.lock_tokens.write().await.remove(receipt.handle());
                Ok(())
            }
            StatusCode::GONE | StatusCode::NOT_FOUND => Err(QueueError::InvalidReceipt {
                receipt: receipt.handle().to_string(),
            }),
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Session abandon failed: {}", error_body),
                })
            }
        }
    }

    /// Dead-letter a session message.
    ///
    /// Calls `POST {namespace}/{queue}/sessions/{sessionId}/messages/{lockToken}/$deadletter`
    /// with a JSON body containing `DeadLetterReason`.
    ///
    /// # Errors
    ///
    /// - `QueueError::InvalidReceipt` – receipt not found locally or lock expired.
    async fn dead_letter_message(
        &self,
        receipt: &ReceiptHandle,
        reason: &str,
    ) -> Result<(), QueueError> {
        if !self.lock_tokens.read().await.contains(receipt.handle()) {
            return Err(QueueError::InvalidReceipt {
                receipt: receipt.handle().to_string(),
            });
        }
        let lock_token = receipt.handle().to_string();

        let url = format!(
            "{}/{}/sessions/{}/messages/{}/$deadletter",
            self.namespace_url,
            self.queue_name.as_str(),
            urlencoding::encode(self.session_id.as_str()),
            urlencoding::encode(&lock_token)
        );

        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        let properties = serde_json::json!({
            "DeadLetterReason": reason,
            "DeadLetterErrorDescription": "Message processing failed"
        });

        let response = self
            .http_client
            .post(&url)
            .header(header::AUTHORIZATION, auth_token)
            .header(header::CONTENT_TYPE, "application/json")
            .json(&properties)
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        match response.status() {
            StatusCode::OK | StatusCode::NO_CONTENT | StatusCode::CREATED => {
                self.lock_tokens.write().await.remove(receipt.handle());
                Ok(())
            }
            StatusCode::GONE | StatusCode::NOT_FOUND => Err(QueueError::InvalidReceipt {
                receipt: receipt.handle().to_string(),
            }),
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Session dead letter failed: {}", error_body),
                })
            }
        }
    }

    /// Renew the session lock to extend the exclusive hold on the session.
    ///
    /// Calls `POST {namespace}/{queue}/sessions/{sessionId}/renewlock`.
    /// On success the local `session_expires_at` is refreshed.
    ///
    /// # Errors
    ///
    /// - `QueueError::SessionNotFound` – the session lock has already expired.
    async fn renew_session_lock(&self) -> Result<(), QueueError> {
        let url = format!(
            "{}/{}/sessions/{}/renewlock",
            self.namespace_url,
            self.queue_name.as_str(),
            urlencoding::encode(self.session_id.as_str())
        );

        let auth_token = self
            .get_auth_token()
            .await
            .map_err(|e| e.to_queue_error())?;

        let response = self
            .http_client
            .post(&url)
            .header(header::AUTHORIZATION, auth_token)
            .header(header::CONTENT_LENGTH, "0")
            .send()
            .await
            .map_err(|e| {
                AzureError::NetworkError(format!("HTTP request failed: {}", e)).to_queue_error()
            })?;

        match response.status() {
            StatusCode::OK | StatusCode::NO_CONTENT => {
                self.refresh_session_expiry();
                Ok(())
            }
            StatusCode::GONE | StatusCode::NOT_FOUND => Err(QueueError::SessionNotFound {
                session_id: self.session_id.to_string(),
            }),
            status => {
                let error_body = response.text().await.unwrap_or_default();
                Err(QueueError::ProviderError {
                    provider: "AzureServiceBus".to_string(),
                    code: status.as_str().to_string(),
                    message: format!("Session lock renewal failed: {}", error_body),
                })
            }
        }
    }

    /// Release local session state.
    ///
    /// Clears all locally cached message lock tokens. The Azure Service Bus
    /// REST API has no endpoint to release a session lock before it expires;
    /// the broker releases the lock automatically after the session timeout
    /// configured on the queue entity (typically 30 s – 5 min). For workloads
    /// that need immediate hand-off, configure a shorter session lock duration
    /// on the queue entity or use the AMQP-based SDK which supports explicit
    /// session release.
    async fn close_session(&self) -> Result<(), QueueError> {
        self.lock_tokens.write().await.clear();
        Ok(())
    }

    fn session_id(&self) -> &SessionId {
        &self.session_id
    }

    fn session_expires_at(&self) -> Timestamp {
        self.session_expires_at
            .read()
            .map(|guard| *guard)
            .unwrap_or_else(|_| {
                // Lock is poisoned (a writer panicked). Return an already-expired
                // sentinel so callers treat the session as invalid rather than
                // silently assuming it just started.
                Timestamp::from_datetime(Utc::now() - Duration::seconds(1))
            })
    }
}