desirable 1.1.0

A minimal Rust web application framework
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
//! Cookie-based session management for the desirable web framework.
//!
//! This module provides a secure, cookie-based session management system for
//! storing user state across HTTP requests. Sessions are signed using HMAC-SHA256
//! to prevent tampering and serialized using Base64URL encoding for safe cookie
//! transmission.
//!
//! # Example
//!
//! ```rust,ignore
//! use desirable::{SessionManager, SessionConfig};
//!
//! // Create a session manager with a signing key (must be at least 32 bytes)
//! let key = b"your-32-byte-secret-key-here!!!!";
//! let config = SessionConfig::new(key);
//! let manager = SessionManager::new(config);
//!
//! // Create a new session
//! let mut session = manager.create_session();
//! session.insert("user_id", 42).unwrap();
//! session.insert("username", "alice").unwrap();
//!
//! // Generate a cookie header for the session
//! let cookie_value = manager.write_session(&session);
//!
//! // Later, read the session from a cookie value
//! if let Some(loaded_session) = manager.read_session(&cookie_value).unwrap() {
//!     let user_id: Option<i32> = loaded_session.get("user_id").unwrap();
//!     println!("User ID: {:?}", user_id);
//! }
//!
//! // To destroy a session, use the deletion cookie
//! let deletion_cookie = manager.make_deletion_cookie();
//! ```
//!
//! # Security
//!
//! - All session data is signed with HMAC-SHA256 to prevent tampering
//!
//! - The signing key must be at least 32 bytes long
//!
//! - By default, cookies are configured with:
//!   - HttpOnly: Prevents JavaScript access
//!   - SameSite=Lax: CSRF protection
//!   - Secure: HTTPS-only transmission
//!
//! # Session Lifecycle
//!
//! 1. **Creation**: Call [`SessionManager::create_session()`] to create a new session
//!
//! 2. **Storage**: Use [`Session::insert()`] to store data in the session
//!
//! 3. **Transmission**: Generate a cookie header with [`SessionManager::make_cookie_header()`]
//!
//! 4. **Retrieving**: Read the cookie value and load the session with [`SessionManager::read_session()`]
//!
//! 5. **Destruction**: Use [`SessionManager::make_deletion_cookie()`] to invalidate a session

use crate::Result;
use base64::Engine as _;
use chrono::{DateTime, Utc};
use hmac::{Hmac, Mac};
use hyper::http;
use rand::RngCore as _;
use serde::{Deserialize, Serialize};
use sha2::Sha256;
use std::collections::HashMap;
use std::sync::Arc;

type HmacSha256 = Hmac<Sha256>;
const DEFAULT_MAX_AGE_SECS: i64 = 30 * 24 * 60 * 60;
const SESSION_ID_LENGTH: usize = 32;
const DEFAULT_COOKIE_NAME: &str = "desirable_session";

/// Errors that can occur during session operations.
///
/// This enum represents all possible errors that may arise when creating,
/// reading, or managing sessions. Each variant includes a user-friendly
/// error message suitable for debugging and logging.
///
/// # Example
///
/// ```rust
/// use desirable::SessionError;
///
/// fn handle_session_error(error: SessionError) {
///     match error {
///         SessionError::InvalidCookie => {
///             println!("The session cookie was malformed or tampered with");
///         }
///         SessionError::SignatureMismatch => {
///             println!("Session cookie signature verification failed");
///         }
///         SessionError::Expired => {
///             println!("The session has expired");
///         }
///         SessionError::NotFound => {
///             println!("Session not found");
///         }
///         SessionError::KeyNotFound(key) => {
///             println!("Key '{}' not found in session", key);
///         }
///         SessionError::Serialization(e) => {
///             println!("JSON serialization error: {}", e);
///         }
///     }
/// }
/// ```
#[derive(Debug, thiserror::Error)]
pub enum SessionError {
  /// The session cookie is malformed, empty, or has an invalid format.
  ///
  /// This error occurs when:
  /// - The cookie value is empty
  /// - The cookie cannot be decoded from Base64URL
  /// - The cookie format doesn't match `data|signature`
  #[error("invalid session cookie")]
  InvalidCookie,

  /// The session cookie signature does not match the expected signature.
  ///
  /// This indicates potential tampering with the cookie. The cookie data
  /// may have been modified after it was signed, or the signing key
  /// may have changed.
  #[error("session signature mismatch")]
  SignatureMismatch,

  /// The session has expired and is no longer valid.
  ///
  /// Sessions automatically expire after their configured maximum age.
  /// Consider creating a new session for the user.
  #[error("session expired")]
  Expired,

  /// The requested session was not found.
  ///
  /// This may occur when:
  /// - No session cookie is present in the request
  /// - The session ID doesn't exist in the session store
  #[error("session not found")]
  NotFound,

  /// The specified key was not found in the session data.
  ///
  /// # Arguments
  ///
  /// * `key` - The key that was not found
  #[error("key not found in session: {0}")]
  KeyNotFound(String),

  /// An error occurred during session data serialization or deserialization.
  ///
  /// This typically indicates corrupted session data or a mismatch between
  /// the serialization format and the expected structure.
  ///
  /// # Arguments
  ///
  /// * `error` - The underlying serde_json error
  #[error("session serialization error: {0}")]
  Serialization(#[from] serde_json::Error),
}

/// Configuration for session cookie behavior.
///
/// `SessionConfig` controls all aspects of how session cookies are created,
/// transmitted, and managed. It uses a builder pattern for flexible configuration.
///
/// # Default Configuration
///
/// By default, sessions use:
/// - Cookie name: `"desirable_session"`
/// - Path: `"/"`
/// - Secure: `true` (HTTPS only)
/// - HttpOnly: `true` (no JavaScript access)
/// - SameSite: `Lax`
/// - Max age: 30 days
///
/// # Example
///
/// ```rust
/// use desirable::SessionConfig;
///
/// // Create a config with a custom signing key
/// let key = b"your-32-byte-secret-key-here!!!!";
/// let config = SessionConfig::new(key)
///     .cookie_name("my_app_session")
///     .path("/api")
///     .domain("example.com")
///     .secure(true)
///     .http_only(true)
///     .same_site(cookie::SameSite::Strict)
///     .max_age_secs(86400); // 1 day
/// ```
///
/// # Security Notes
///
/// - The signing key must be at least 32 bytes long
/// - Use a cryptographically random key in production
/// - Keep the signing key secret and consistent across restarts
/// - Consider rotating keys periodically
#[derive(Clone, Debug)]
pub struct SessionConfig {
  /// The name of the session cookie.
  ///
  /// Default: `"desirable_session"`
  pub cookie_name: String,

  /// The URL path scope for the cookie.
  ///
  /// Determines which paths can access the cookie.
  /// Default: `"/"`
  pub path: String,

  /// The domain scope for the cookie.
  ///
  /// If `None`, the cookie is only sent to the exact origin.
  /// Default: `None`
  pub domain: Option<String>,

  /// Whether the cookie requires HTTPS.
  ///
  /// When `true`, the cookie will only be sent over secure connections.
  /// Default: `true`
  pub secure: bool,

  /// Whether the cookie is inaccessible to JavaScript.
  ///
  /// When `true`, the cookie cannot be accessed via `document.cookie`,
  /// providing protection against XSS attacks.
  /// Default: `true`
  pub http_only: bool,

  /// The SameSite attribute for CSRF protection.
  ///
  /// - `Strict`: Cookie is only sent in first-party context
  /// - `Lax`: Cookie is sent with top-level navigations and safe HTTP methods
  /// - `None`: Cookie is sent in all contexts (requires `secure = true`)
  ///
  /// Default: `SameSite::Lax`
  pub same_site: cookie::SameSite,

  /// The maximum age of the session cookie in seconds.
  ///
  /// If `None`, the cookie is a session cookie (deleted when browser closes).
  /// Default: `Some(30 days)` (2592000 seconds)
  pub max_age_secs: Option<i64>,

  /// The secret key used to sign session cookies.
  ///
  /// Must be at least 32 bytes. Used with HMAC-SHA256 to prevent tampering.
  pub signing_key: Vec<u8>,
}

impl SessionConfig {
  /// Creates a new `SessionConfig` with the specified signing key.
  ///
  /// The signing key is used to create HMAC-SHA256 signatures for session cookies,
  /// preventing tampering with session data. It must be at least 32 bytes long.
  ///
  /// # Arguments
  ///
  /// * `signing_key` - A byte slice containing the signing key (must be >= 32 bytes)
  ///
  /// # Panics
  ///
  /// Panics if the signing key is less than 32 bytes.
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::SessionConfig;
  ///
  /// // Use a cryptographically random 32-byte key
  /// let key = b"this-is-a-32-byte-secret-key-!!!!";
  /// let config = SessionConfig::new(key);
  /// ```
  pub fn new(signing_key: &[u8]) -> Self {
    assert!(
      signing_key.len() >= 32,
      "signing key must be at least 32 bytes"
    );
    Self {
      cookie_name: DEFAULT_COOKIE_NAME.to_string(),
      path: "/".to_string(),
      domain: None,
      secure: true,
      http_only: true,
      same_site: cookie::SameSite::Lax,
      max_age_secs: Some(DEFAULT_MAX_AGE_SECS),
      signing_key: signing_key.to_vec(),
    }
  }

  /// Sets the name of the session cookie.
  ///
  /// Default: `"desirable_session"`
  ///
  /// # Arguments
  ///
  /// * `name` - The cookie name to use for sessions
  ///
  /// # Returns
  ///
  /// The updated `SessionConfig` for method chaining
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::SessionConfig;
  ///
  /// let config = SessionConfig::new(&[0; 32])
  ///     .cookie_name("my_session_id");
  /// ```
  #[must_use]
  pub fn cookie_name(mut self, name: &str) -> Self {
    self.cookie_name = name.to_string();
    self
  }

  /// Sets the path scope for the session cookie.
  ///
  /// Determines which URLs the cookie will be sent with.
  /// Default: `"/"`
  ///
  /// # Arguments
  ///
  /// * `path` - The URL path scope for the cookie
  ///
  /// # Returns
  ///
  /// The updated `SessionConfig` for method chaining
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::SessionConfig;
  ///
  /// // Cookie only sent to /api routes
  /// let config = SessionConfig::new(&[0; 32])
  ///     .path("/api");
  /// ```
  #[must_use]
  pub fn path(mut self, path: &str) -> Self {
    self.path = path.to_string();
    self
  }

  /// Sets the domain scope for the session cookie.
  ///
  /// If set, the cookie will be sent to this domain and all subdomains.
  /// Default: `None` (exact origin only)
  ///
  /// # Arguments
  ///
  /// * `domain` - The domain scope for the cookie (e.g., "example.com")
  ///
  /// # Returns
  ///
  /// The updated `SessionConfig` for method chaining
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::SessionConfig;
  ///
  /// // Cookie sent to example.com and sub.example.com
  /// let config = SessionConfig::new(&[0; 32])
  ///     .domain("example.com");
  /// ```
  #[must_use]
  pub fn domain(mut self, domain: &str) -> Self {
    self.domain = Some(domain.to_string());
    self
  }

  /// Sets whether the cookie requires HTTPS.
  ///
  /// When `true`, the browser will only send the cookie over secure (HTTPS) connections.
  /// Default: `true`
  ///
  /// # Arguments
  ///
  /// * `secure` - Whether to require HTTPS for the cookie
  ///
  /// # Returns
  ///
  /// The updated `SessionConfig` for method chaining
  ///
  /// # Security Note
  ///
  /// Setting this to `false` in production is not recommended as it allows
  /// session cookies to be sent over unencrypted connections.
  #[must_use]
  pub fn secure(mut self, secure: bool) -> Self {
    self.secure = secure;
    self
  }

  /// Sets whether the cookie is inaccessible to JavaScript.
  ///
  /// When `true`, the cookie cannot be accessed via `document.cookie`,
  /// providing protection against XSS attacks that could steal session data.
  /// Default: `true`
  ///
  /// # Arguments
  ///
  /// * `http_only` - Whether to make the cookie HttpOnly
  ///
  /// # Returns
  ///
  /// The updated `SessionConfig` for method chaining
  ///
  /// # Security Note
  ///
  /// This should generally remain `true` to prevent XSS attacks from stealing sessions.
  #[must_use]
  pub fn http_only(mut self, http_only: bool) -> Self {
    self.http_only = http_only;
    self
  }

  /// Sets the SameSite attribute for the session cookie.
  ///
  /// This provides CSRF protection by controlling when the cookie is sent with cross-site requests.
  /// Default: `SameSite::Lax`
  ///
  /// # Arguments
  ///
  /// * `same_site` - The SameSite mode to use
  ///
  /// # Returns
  ///
  /// The updated `SessionConfig` for method chaining
  ///
  /// # SameSite Modes
  ///
  /// - `Strict`: Cookie is only sent in a first-party context
  /// - `Lax`: Cookie is sent with top-level navigations and safe HTTP methods (GET for navigation)
  /// - `None`: Cookie is sent in all contexts (requires `secure = true`)
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::SessionConfig;
  /// use cookie::SameSite;
  ///
  /// let config = SessionConfig::new(&[0; 32])
  ///     .same_site(SameSite::Strict);
  /// ```
  #[must_use]
  pub fn same_site(mut self, same_site: cookie::SameSite) -> Self {
    self.same_site = same_site;
    self
  }

  /// Sets the maximum age of the session cookie in seconds.
  ///
  /// This determines how long the session cookie will persist in the browser.
  /// Default: `Some(30 days)` (2592000 seconds)
  ///
  /// # Arguments
  ///
  /// * `secs` - The maximum age in seconds. `None` creates a session cookie
  ///   that is deleted when the browser closes.
  ///
  /// # Returns
  ///
  /// The updated `SessionConfig` for method chaining
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::SessionConfig;
  ///
  /// // 1 hour session
  /// let config = SessionConfig::new(b"your-32-byte-secret-key-here!!!!")
  ///     .max_age_secs(3600);
  ///
  /// // Note: For session cookies (deleted when browser closes),
  /// // set max_age_secs to None in the config before creating the manager
  /// ```
  #[must_use]
  pub fn max_age_secs(mut self, secs: i64) -> Self {
    self.max_age_secs = Some(secs);
    self
  }
}

impl Default for SessionConfig {
  fn default() -> Self {
    let mut key = [0u8; 32];
    rand::rng().fill_bytes(&mut key);
    Self::new(&key)
  }
}

/// The internal data structure for a session.
///
/// `SessionData` contains the raw session information that gets serialized
/// to JSON and signed for storage in a cookie. It includes:
/// - A unique session ID
/// - Creation and access timestamps
/// - Key-value data stored in the session
///
/// This struct is serialized to JSON and signed with HMAC-SHA256 before
/// being stored in a cookie.
///
/// # Note
///
/// This is the internal representation. Use [`Session`] for runtime session
/// operations which provides additional tracking of modifications.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SessionData {
  /// A unique identifier for the session.
  ///
  /// Generated using cryptographically random bytes and Base64URL encoded.
  /// Used to identify the session in logs and for session management.
  pub id: String,

  /// The timestamp when the session was created.
  ///
  /// Set once during session creation and never modified.
  pub created: DateTime<Utc>,

  /// The timestamp of the last session access.
  ///
  /// Updated whenever the session is accessed or modified.
  /// Used for session expiration and activity tracking.
  pub accessed: DateTime<Utc>,

  /// The session data stored as key-value pairs.
  ///
  /// Values are serialized to JSON strings for storage.
  /// Supports any type that implements [`serde::Serialize`] and [`serde::Deserialize`].
  #[serde(flatten)]
  pub data: HashMap<String, String>,
}

impl SessionData {
  pub fn new() -> Self {
    let now = Utc::now();
    let mut bytes = [0u8; SESSION_ID_LENGTH];
    rand::rng().fill_bytes(&mut bytes);
    let id = base64::engine::general_purpose::URL_SAFE.encode(bytes);
    Self {
      id,
      created: now,
      accessed: now,
      data: HashMap::new(),
    }
  }
}

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

/// A runtime session for storing and retrieving user data.
///
/// `Session` provides a high-level API for managing session data. It wraps
/// `SessionData` and tracks modifications to optimize cookie generation.
///
/// # Session Lifecycle
///
/// 1. Create or load a session
/// 2. Read/write session data using [`Session::get`] and [`Session::insert`]
/// 3. Generate a cookie header with [`SessionManager::make_cookie_header`]
/// 4. On subsequent requests, load the session with [`SessionManager::read_session`]
///
/// # Example
///
/// ```rust
/// use desirable::{SessionManager, SessionConfig, Session, Result};
///
/// fn handle_user_login(manager: &SessionManager, user_id: i32, username: String) -> Result<Session> {
///     let mut session = manager.create_session();
///     session.insert("user_id", user_id)?;
///     session.insert("username", username)?;
///     session.insert("role", "user")?;
///     Ok(session)
/// }
///
/// fn check_auth(session: &Session) -> Result<Option<i32>> {
///     session.get("user_id")
/// }
/// ```
///
/// # Modification Tracking
///
/// The `Session` tracks whether it has been modified. This is useful for
/// conditional cookie updates - only send a Set-Cookie header if the session
/// was modified.
///
/// ```rust
/// use desirable::{SessionManager, SessionConfig};
///
/// let key = b"your-32-byte-secret-key-here!!!!";
/// let manager = SessionManager::new(SessionConfig::new(key));
///
/// let mut session = manager.create_session();
/// assert!(!session.is_modified()); // New sessions start unmodified
///
/// session.insert("key", "value").unwrap();
/// assert!(session.is_modified()); // Now modified
/// ```
#[derive(Clone, Debug)]
pub struct Session {
  /// The underlying session data
  inner: SessionData,
  /// Whether the session has been modified
  modified: bool,
}

impl Session {
  /// Creates a new `Session` wrapping the provided `SessionData`.
  ///
  /// The session starts in an unmodified state.
  ///
  /// # Arguments
  ///
  /// * `data` - The `SessionData` to wrap
  ///
  /// # Returns
  ///
  /// A new `Session` instance
  ///
  /// # Example
  ///
  /// ```rust,ignore
  /// use desirable::{Session, SessionData};
  ///
  /// let data = SessionData::new();
  /// let session = Session::new(data);
  /// ```
  pub fn new(data: SessionData) -> Self {
    Self {
      inner: data,
      modified: false,
    }
  }

  /// Returns the unique identifier of this session.
  ///
  /// The session ID is generated during creation and remains constant
  /// for the lifetime of the session.
  ///
  /// # Returns
  ///
  /// A string slice containing the session ID
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let session = manager.create_session();
  /// println!("Session ID: {}", session.id());
  /// ```
  pub fn id(&self) -> &str {
    &self.inner.id
  }

  /// Returns the timestamp when this session was created.
  ///
  /// # Returns
  ///
  /// A `DateTime<Utc>` representing the creation time
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  /// use chrono::Duration;
  ///
  /// let manager = SessionManager::with_random_key();
  /// let session = manager.create_session();
  /// let now = chrono::Utc::now();
  /// let age = now.signed_duration_since(session.created());
  /// println!("Session created {} seconds ago", age.num_seconds());
  /// ```
  pub fn created(&self) -> DateTime<Utc> {
    self.inner.created
  }

  /// Returns the timestamp of the last session access.
  ///
  /// This timestamp is updated when [`Session::touch`] is called,
  /// typically during session read or write operations.
  ///
  /// # Returns
  ///
  /// A `DateTime<Utc>` representing the last access time
  ///
  /// # See Also
  ///
  /// - [`Session::touch`] - Update the access timestamp
  pub fn accessed(&self) -> DateTime<Utc> {
    self.inner.accessed
  }

  /// Returns the number of key-value pairs stored in the session.
  ///
  /// # Returns
  ///
  /// The number of entries in the session data
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  /// assert_eq!(session.len(), 0);
  ///
  /// session.insert("a", 1).unwrap();
  /// session.insert("b", 2).unwrap();
  /// assert_eq!(session.len(), 2);
  /// ```
  pub fn len(&self) -> usize {
    self.inner.data.len()
  }

  /// Returns `true` if the session contains no key-value pairs.
  ///
  /// # Returns
  ///
  /// Whether the session is empty
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let session = manager.create_session();
  /// assert!(session.is_empty());
  /// ```
  pub fn is_empty(&self) -> bool {
    self.inner.data.is_empty()
  }

  /// Returns a reference to the underlying session data.
  ///
  /// This provides direct access to the `HashMap` storing the session values.
  ///
  /// # Returns
  ///
  /// A reference to the session data map
  ///
  /// # Note
  ///
  /// Modifying this map directly will not mark the session as modified.
  /// Use [`Session::insert`] or [`Session::data_mut`] instead.
  pub fn data(&self) -> &HashMap<String, String> {
    &self.inner.data
  }

  /// Returns `true` if the session contains the specified key.
  ///
  /// # Arguments
  ///
  /// * `key` - The key to check for
  ///
  /// # Returns
  ///
  /// Whether the key exists in the session
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  ///
  /// assert!(!session.contains_key("user_id"));
  /// session.insert("user_id", 42).unwrap();
  /// assert!(session.contains_key("user_id"));
  /// ```
  pub fn contains_key(&self, key: &str) -> bool {
    self.inner.data.contains_key(key)
  }

  /// Returns `true` if the session has been modified since creation or last save.
  ///
  /// This is useful for optimizing cookie updates - only send a Set-Cookie
  /// header if the session was actually modified.
  ///
  /// # Returns
  ///
  /// Whether the session has been modified
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  ///
  /// if session.is_modified() {
  ///     // Update cookie
  /// }
  /// ```
  pub fn is_modified(&self) -> bool {
    self.modified
  }

  /// Retrieves a value from the session and deserializes it.
  ///
  /// Looks up the specified key and deserializes the stored JSON value
  /// back into the requested type.
  ///
  /// # Type Parameters
  ///
  /// * `T` - The type to deserialize the value as. Must implement
  ///   `serde::Deserialize`.
  ///
  /// # Arguments
  ///
  /// * `key` - The key to look up
  ///
  /// # Returns
  ///
  /// `Ok(Some(value))` if the key exists, `Ok(None)` if not found,
  /// or an error if deserialization fails.
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  /// session.insert("user_id", 42).unwrap();
  /// session.insert("name", "Alice").unwrap();
  /// session.insert("preferences", vec!["dark_mode", "notifications"]).unwrap();
  ///
  /// let user_id: Option<i32> = session.get("user_id").unwrap();
  /// assert_eq!(user_id, Some(42));
  ///
  /// let name: Option<String> = session.get("name").unwrap();
  /// assert_eq!(name, Some("Alice".to_string()));
  ///
  /// let missing: Option<i32> = session.get("nonexistent").unwrap();
  /// assert_eq!(missing, None);
  /// ```
  pub fn get<T>(&self, key: &str) -> Result<Option<T>>
  where
    T: for<'de> serde::de::Deserialize<'de>,
  {
    if let Some(value) = self.inner.data.get(key) {
      Ok(Some(serde_json::from_str(value)?))
    } else {
      Ok(None)
    }
  }

  /// Retrieves a string value from the session.
  ///
  /// This is a convenience method for retrieving values that are already strings,
  /// avoiding the overhead of JSON deserialization.
  ///
  /// # Arguments
  ///
  /// * `key` - The key to look up
  ///
  /// # Returns
  ///
  /// `Some(&str)` if the key exists, `None` if not found
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  /// session.insert("count", 42).unwrap();
  ///
  /// let count = session.get_str("count");
  /// assert_eq!(count, Some("42"));
  /// ```
  pub fn get_str(&self, key: &str) -> Option<&str> {
    self.inner.data.get(key).map(|s| s.as_str())
  }

  /// Inserts a value into the session.
  ///
  /// Serializes the value to JSON and stores it under the given key.
  /// Marks the session as modified.
  ///
  /// # Type Parameters
  ///
  /// * `T` - The type to serialize. Must implement `serde::Serialize`.
  ///
  /// # Arguments
  ///
  /// * `key` - The key to store the value under
  /// * `value` - The value to serialize and store
  ///
  /// # Returns
  ///
  /// `Ok(())` on success, or an error if serialization fails
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  ///
  /// session.insert("user_id", 42).unwrap();
  /// session.insert("username", "alice").unwrap();
  /// session.insert("logged_in", true).unwrap();
  /// ```
  pub fn insert<T>(&mut self, key: &str, value: T) -> Result<()>
  where
    T: Serialize,
  {
    let json = serde_json::to_string(&value)?;
    self.inner.data.insert(key.to_string(), json);
    self.modified = true;
    Ok(())
  }

  /// Removes and returns a value from the session.
  ///
  /// Looks up and removes the specified key, deserializing the stored value.
  /// Marks the session as modified.
  ///
  /// # Type Parameters
  ///
  /// * `T` - The type to deserialize the value as. Must implement
  ///   `serde::Deserialize`.
  ///
  /// # Arguments
  ///
  /// * `key` - The key to remove
  ///
  /// # Returns
  ///
  /// `Ok(Some(value))` if the key existed, `Ok(None)` if not found,
  /// or an error if deserialization fails.
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  /// session.insert("temp", "value").unwrap();
  ///
  /// let removed: Option<String> = session.remove("temp").unwrap();
  /// assert_eq!(removed, Some("value".to_string()));
  /// assert!(session.is_empty());
  /// ```
  pub fn remove<T>(&mut self, key: &str) -> Result<Option<T>>
  where
    T: for<'de> serde::de::Deserialize<'de>,
  {
    if let Some(value) = self.inner.data.remove(key) {
      self.modified = true;
      Ok(Some(serde_json::from_str(&value)?))
    } else {
      Ok(None)
    }
  }

  /// Removes and returns a string value from the session.
  ///
  /// This is a convenience method for removing string values.
  /// Marks the session as modified.
  ///
  /// # Arguments
  ///
  /// * `key` - The key to remove
  ///
  /// # Returns
  ///
  /// `Some(String)` if the key existed, `None` if not found
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  /// session.insert("count", 42).unwrap();
  ///
  /// let removed = session.remove_str("count");
  /// assert_eq!(removed, Some("42".to_string()));
  /// ```
  pub fn remove_str(&mut self, key: &str) -> Option<String> {
    let removed = self.inner.data.remove(key);
    if removed.is_some() {
      self.modified = true;
    }
    removed
  }

  /// Clears all data from the session.
  ///
  /// Removes all key-value pairs from the session data.
  /// Marks the session as modified.
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  /// session.insert("a", 1).unwrap();
  /// session.insert("b", 2).unwrap();
  /// assert_eq!(session.len(), 2);
  ///
  /// session.clear();
  /// assert!(session.is_empty());
  /// assert!(session.is_modified());
  /// ```
  pub fn clear(&mut self) {
    if !self.inner.data.is_empty() {
      self.inner.data.clear();
      self.modified = true;
    }
  }

  /// Regenerates the session ID.
  ///
  /// Generates a new cryptographically random session ID and replaces
  /// the current one. This is useful for session fixation prevention
  /// after authentication.
  ///
  /// Marks the session as modified.
  ///
  /// # Security Note
  ///
  /// Call this method after successful login to prevent session fixation attacks.
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  /// let old_id = session.id().to_string();
  ///
  /// session.regenerate_id();
  /// assert_ne!(session.id(), old_id);
  /// ```
  pub fn regenerate_id(&mut self) {
    let mut bytes = [0u8; SESSION_ID_LENGTH];
    rand::rng().fill_bytes(&mut bytes);
    self.inner.id = base64::engine::general_purpose::URL_SAFE.encode(bytes);
    self.modified = true;
  }

  /// Updates the session's access timestamp.
  ///
  /// Sets the `accessed` field to the current time.
  /// This should be called when the session is accessed to track activity.
  ///
  /// # Note
  ///
  /// This method does NOT mark the session as modified, as accessing
  /// session data should not necessarily trigger a cookie update.
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  /// use chrono::Duration;
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  /// let original_accessed = session.accessed();
  ///
  /// // Simulate some time passing
  /// session.touch();
  ///
  /// assert!(session.accessed() > original_accessed);
  /// ```
  pub fn touch(&mut self) {
    self.inner.accessed = Utc::now();
  }

  /// Consumes the session and returns the underlying `SessionData`.
  ///
  /// This is useful when you need to access the raw session data
  /// for serialization or custom handling.
  ///
  /// # Returns
  ///
  /// The `SessionData` wrapped by this session
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let session = manager.create_session();
  /// let data = session.into_data();
  ///
  /// println!("Session ID: {}", data.id);
  /// println!("Created: {:?}", data.created);
  /// ```
  pub fn into_data(self) -> SessionData {
    self.inner
  }

  /// Returns a mutable reference to the underlying data map.
  ///
  /// This allows direct manipulation of the session data HashMap.
  /// Marks the session as modified.
  ///
  /// # Returns
  ///
  /// A mutable reference to the session data HashMap
  ///
  /// # Warning
  ///
  /// Use this method with caution. Direct modification of the HashMap
  /// bypasses serialization checks. Prefer [`Session::insert`] when possible.
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let manager = SessionManager::with_random_key();
  /// let mut session = manager.create_session();
  ///
  /// session.data_mut().insert("custom_key".to_string(), "custom_value".to_string());
  /// assert_eq!(session.get_str("custom_key"), Some("custom_value"));
  /// ```
  pub fn data_mut(&mut self) -> &mut HashMap<String, String> {
    self.modified = true;
    &mut self.inner.data
  }
}

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

/// Manages session creation, reading, and cookie generation.
///
/// `SessionManager` handles the lifecycle of sessions including:
/// - Creating new sessions
/// - Reading sessions from cookie values
/// - Writing sessions to cookie headers
/// - Generating deletion cookies for logout
///
/// # Thread Safety
///
/// `SessionManager` is cheap to clone and share across threads.
/// Internally it uses `Arc<SessionConfig>` for efficient sharing.
///
/// # Example
///
/// ```rust
/// use desirable::{SessionManager, SessionConfig};
///
/// // Create a manager with a specific signing key
/// let key = b"your-32-byte-secret-key-here!!!!";
/// let config = SessionConfig::new(key);
/// let manager = SessionManager::new(config);
///
/// // Create a new session
/// let mut session = manager.create_session();
/// session.insert("user_id", 42).unwrap();
///
/// // Generate a Set-Cookie header
/// let _cookie_header = manager.make_cookie_header(&session);
/// // Use in response: response.headers_mut().insert(SET_COOKIE, cookie_header);
///
/// // Later, read the session from a request cookie
/// let cookie_value = manager.write_session(&session);
/// if let Some(loaded) = manager.read_session(&cookie_value).unwrap() {
///     let user_id: Option<i32> = loaded.get("user_id").unwrap();
///     println!("User ID: {:?}", user_id);
/// }
/// ```
///
/// # Cookie Format
///
/// Sessions are stored in cookies using the following format:
///
/// ```text
/// <base64-encoded-session-data>|<hmac-sha256-signature>
/// ```
///
/// This format provides:
/// 1. Tamper detection via HMAC signature
/// 2. Safe transmission via Base64URL encoding
#[derive(Clone, Debug)]
pub struct SessionManager {
  /// The session configuration
  config: Arc<SessionConfig>,
}

impl SessionManager {
  /// Creates a new `SessionManager` with the given configuration.
  ///
  /// The configuration is stored in an `Arc` for efficient cloning.
  ///
  /// # Arguments
  ///
  /// * `config` - The session configuration to use
  ///
  /// # Returns
  ///
  /// A new `SessionManager` instance
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let key = b"your-32-byte-secret-key-here!!!!";
  /// let config = SessionConfig::new(key);
  /// let manager = SessionManager::new(config);
  /// ```
  pub fn new(config: SessionConfig) -> Self {
    Self {
      config: Arc::new(config),
    }
  }

  /// Creates a new `SessionManager` with a randomly generated signing key.
  ///
  /// This is useful for development or when persistence of the signing key
  /// is not required (e.g., single-instance applications).
  ///
  /// # Warning
  ///
  /// Sessions created with this manager cannot be validated after restart
  /// because the signing key will be different.
  ///
  /// # Returns
  ///
  /// A new `SessionManager` with a random 32-byte signing key
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::SessionManager;
  ///
  /// let manager = SessionManager::with_random_key();
  /// ```
  pub fn with_random_key() -> Self {
    Self::new(SessionConfig::default())
  }

  /// Returns a reference to the session configuration.
  ///
  /// # Returns
  ///
  /// A reference to the `SessionConfig`
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let key = b"your-32-byte-secret-key-here!!!!";
  /// let config = SessionConfig::new(key);
  /// let manager = SessionManager::new(config);
  ///
  /// let cookie_name = &manager.config().cookie_name;
  /// ```
  pub fn config(&self) -> &SessionConfig {
    &self.config
  }

  /// Creates a new, empty session.
  ///
  /// The session will have a unique ID and empty data.
  ///
  /// # Returns
  ///
  /// A new `Session` ready for use
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::SessionManager;
  ///
  /// let manager = SessionManager::with_random_key();
  /// let session = manager.create_session();
  ///
  /// assert!(!session.id().is_empty());
  /// assert!(session.is_empty());
  /// ```
  pub fn create_session(&self) -> Session {
    Session::new(SessionData::new())
  }

  /// Reads and validates a session from a cookie value.
  ///
  /// Decodes the cookie value, verifies the HMAC signature, and deserializes
  /// the session data.
  ///
  /// # Arguments
  ///
  /// * `cookie_value` - The raw cookie value (should be Base64URL encoded)
  ///
  /// # Returns
  ///
  /// - `Ok(Some(Session))` if the cookie is valid
  /// - `Ok(None)` if the cookie is empty
  /// - `Err(SessionError)` if the cookie is invalid or tampered
  ///
  /// # Errors
  ///
  /// - `SessionError::InvalidCookie` if the cookie format is invalid
  /// - `SessionError::SignatureMismatch` if the signature verification fails
  /// - `SessionError::Serialization` if JSON deserialization fails
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let key = b"your-32-byte-secret-key-here!!!!";
  /// let manager = SessionManager::new(SessionConfig::new(key));
  ///
  /// // Create and serialize a session
  /// let mut session = manager.create_session();
  /// session.insert("user_id", 42).unwrap();
  /// let cookie = manager.write_session(&session);
  ///
  /// // Later, read the session back
  /// let loaded = manager.read_session(&cookie).unwrap();
  /// assert!(loaded.is_some());
  /// let user_id: Option<i32> = loaded.unwrap().get("user_id").unwrap();
  /// assert_eq!(user_id, Some(42));
  /// ```
  pub fn read_session(&self, cookie_value: &str) -> Result<Option<Session>> {
    if cookie_value.is_empty() {
      return Ok(None);
    }
    let decoded = base64::engine::general_purpose::URL_SAFE
      .decode(cookie_value)
      .map_err(|_| SessionError::InvalidCookie)?;
    let pos = decoded.iter().position(|&c| c == b'|');
    if let Some(idx) = pos {
      let (data_bytes, signature_bytes) = decoded.split_at(idx);
      if signature_bytes.is_empty() || signature_bytes[0] != b'|' {
        return Err(SessionError::InvalidCookie.into());
      }
      let sig = &signature_bytes[1..];
      let mut mac = HmacSha256::new_from_slice(&self.config.signing_key)
        .map_err(|_| SessionError::InvalidCookie)?;
      mac.update(data_bytes);
      mac
        .verify_slice(sig)
        .map_err(|_| SessionError::SignatureMismatch)?;
      let session_data: SessionData =
        serde_json::from_slice(data_bytes).map_err(|_| SessionError::InvalidCookie)?;
      Ok(Some(Session::new(session_data)))
    } else {
      Err(SessionError::InvalidCookie.into())
    }
  }

  /// Serializes a session to a cookie-safe string.
  ///
  /// The session data is serialized to JSON, signed with HMAC-SHA256,
  /// and Base64URL encoded for safe transmission in cookies.
  ///
  /// # Arguments
  ///
  /// * `session` - The session to serialize
  ///
  /// # Returns
  ///
  /// A Base64URL-encoded string of the format `data|signature`
  ///
  /// # Note
  ///
  /// This method does not set any cookie attributes (path, domain, etc.).
  /// Use [`SessionManager::make_cookie_header`] for a complete cookie header.
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let key = b"your-32-byte-secret-key-here!!!!";
  /// let manager = SessionManager::new(SessionConfig::new(key));
  ///
  /// let session = manager.create_session();
  /// let cookie_value = manager.write_session(&session);
  ///
  /// // The value is Base64URL encoded
  /// println!("Cookie value: {}", cookie_value);
  /// ```
  pub fn write_session(&self, session: &Session) -> String {
    let data_bytes = serde_json::to_vec(&session.inner).unwrap_or_default();
    let mut mac = HmacSha256::new_from_slice(&self.config.signing_key).unwrap();
    mac.update(&data_bytes);
    let signature = mac.finalize().into_bytes();
    let mut combined = data_bytes;
    combined.push(b'|');
    combined.extend_from_slice(&signature);
    base64::engine::general_purpose::URL_SAFE.encode(&combined)
  }

  /// Creates a complete Set-Cookie header value for a session.
  ///
  /// This is a convenience method that combines [`SessionManager::write_session`]
  /// with all the configured cookie attributes.
  ///
  /// # Arguments
  ///
  /// * `session` - The session to create a cookie for
  ///
  /// # Returns
  ///
  /// A `HeaderValue` suitable for the `Set-Cookie` header
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let key = b"your-32-byte-secret-key-here!!!!";
  /// let config = SessionConfig::new(key)
  ///     .domain("example.com")
  ///     .path("/api");
  /// let manager = SessionManager::new(config);
  ///
  /// let session = manager.create_session();
  /// let header = manager.make_cookie_header(&session);
  ///
  /// // Use in response:
  /// // response.headers_mut().insert(hyper::header::SET_COOKIE, header);
  /// ```
  pub fn make_cookie_header(&self, session: &Session) -> http::HeaderValue {
    let cookie_value = self.write_session(session);
    let mut builder = cookie::CookieBuilder::new(self.config.cookie_name.clone(), cookie_value)
      .path(self.config.path.clone())
      .http_only(self.config.http_only)
      .same_site(self.config.same_site);
    if let Some(max_age) = self.config.max_age_secs {
      builder = builder.max_age(time::Duration::seconds(max_age));
    }
    if self.config.secure {
      builder = builder.secure(true);
    }
    if let Some(ref domain) = self.config.domain {
      builder = builder.domain(domain.clone());
    }
    builder.build().to_string().parse().unwrap()
  }

  /// Creates a cookie header that will delete the session.
  ///
  /// Sets the cookie's max-age to 0 and value to empty, instructing the
  /// browser to delete the session cookie immediately.
  ///
  /// Use this method when logging out a user to invalidate their session.
  ///
  /// # Returns
  ///
  /// A `HeaderValue` that will delete the session cookie
  ///
  /// # Example
  ///
  /// ```rust
  /// use desirable::{SessionManager, SessionConfig};
  ///
  /// let key = b"your-32-byte-secret-key-here!!!!";
  /// let config = SessionConfig::new(key).domain("example.com");
  /// let manager = SessionManager::new(config);
  ///
  /// let deletion_cookie = manager.make_deletion_cookie();
  ///
  /// // Send this to the client to log them out:
  /// // response.headers_mut().insert(hyper::header::SET_COOKIE, deletion_cookie);
  /// ```
  pub fn make_deletion_cookie(&self) -> http::HeaderValue {
    let mut builder = cookie::CookieBuilder::new(self.config.cookie_name.clone(), "")
      .path(self.config.path.clone())
      .http_only(self.config.http_only)
      .same_site(self.config.same_site)
      .max_age(time::Duration::seconds(0));
    if self.config.secure {
      builder = builder.secure(true);
    }
    if let Some(ref domain) = self.config.domain {
      builder = builder.domain(domain.clone());
    }
    builder.build().to_string().parse().unwrap()
  }

  /// Extracts the session cookie value from a request's headers.
  ///
  /// Parses the Cookie header and extracts the value of the configured
  /// session cookie name.
  ///
  /// # Arguments
  ///
  /// * `headers` - The request headers to extract from
  ///
  /// # Returns
  ///
  /// `Some(cookie_value)` if the session cookie is present,
  /// `None` if the cookie is not found
  ///
  /// # Example
  ///
  /// ```rust,ignore
  /// use desirable::{SessionManager, SessionConfig};
  /// use hyper::header::HeaderMap;
  ///
  /// let key = b"your-32-byte-secret-key-here!!!!";
  /// let manager = SessionManager::new(SessionConfig::new(key));
  ///
  /// // Simulate incoming request headers
  /// let mut headers = HeaderMap::new();
  /// headers.insert(hyper::header::COOKIE, "desirable_session=abc123".parse().unwrap());
  ///
  /// if let Some(value) = manager.get_cookie_value(&headers) {
  ///     println!("Session cookie: {}", value);
  /// }
  /// ```
  pub fn get_cookie_value(&self, headers: &hyper::header::HeaderMap) -> Option<String> {
    headers
      .get(http::header::COOKIE)
      .and_then(|v| v.to_str().ok())
      .and_then(|cookie_str| {
        cookie_str
          .split(';')
          .map(|s| s.trim())
          .find(|s| s.starts_with(&format!("{}=", self.config.cookie_name)))
          .and_then(|s| s.split('=').nth(1).map(|s| s.to_string()))
      })
  }
}

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

  #[test]
  fn test_session_new() {
    let session = Session::new(SessionData::new());
    assert!(!session.id().is_empty());
    assert!(session.is_empty());
    assert!(!session.is_modified());
  }

  #[test]
  fn test_session_insert_get() {
    let mut session = Session::new(SessionData::new());
    session.insert("user_id", 42).unwrap();
    session.insert("name", "Alice").unwrap();
    assert_eq!(session.len(), 2);
    assert!(session.is_modified());

    let user_id: Option<i32> = session.get("user_id").unwrap();
    assert_eq!(user_id, Some(42));

    let name: Option<String> = session.get("name").unwrap();
    assert_eq!(name, Some("Alice".to_string()));
  }

  #[test]
  fn test_session_remove() {
    let mut session = Session::new(SessionData::new());
    session.insert("key", "value").unwrap();
    let removed: Option<String> = session.remove("key").unwrap();
    assert_eq!(removed, Some("value".to_string()));
    assert!(session.is_empty());
  }

  #[test]
  fn test_session_manager_roundtrip() {
    let key = b"this-is-a-32-byte-secret-key-!!!!";
    let manager = SessionManager::new(SessionConfig::new(key));

    let mut session = manager.create_session();
    session.insert("user_id", 123).unwrap();
    session.insert("name", "Bob").unwrap();

    let cookie_value = manager.write_session(&session);
    assert!(!cookie_value.is_empty());

    let loaded = manager.read_session(&cookie_value).unwrap().unwrap();
    assert_eq!(loaded.id(), session.id());
    let user_id: Option<i32> = loaded.get("user_id").unwrap();
    assert_eq!(user_id, Some(123));
  }

  #[test]
  fn test_session_cookie_header() {
    let key = b"this-is-a-32-byte-secret-key-!!!!";
    let config = SessionConfig::new(key).secure(false).http_only(true);
    let manager = SessionManager::new(config);

    let session = manager.create_session();
    let header = manager.make_cookie_header(&session);
    assert!(!header.to_str().unwrap().is_empty());
    assert!(header.to_str().unwrap().contains("HttpOnly"));
  }
}