pkstate 0.1.2

A library for representing, serializing, and deserializing the state of a poker hand
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
//! # pkstate
//!
//! A library for representing, serializing, and deserializing the state of a poker hand.
//!
//! `pkstate` models everything needed to describe a hand in progress or a complete hand history:
//! the game type, forced bets, the players at the table, the community board cards, and the full
//! sequence of actions across every street. All types implement [`serde::Serialize`] and
//! [`serde::Deserialize`], with [`BasicPile`] card collections serialized as human-readable
//! Unicode card strings (e.g. `"A♠ K♥"`).
//!
//! ## Modules
//!
//! - [`act`] — [`act::Action`] enum and [`act::Round`] newtype.
//! - [`game`] — [`game::GameType`] enum and [`game::ForcedBets`] struct.
//! - [`seat`] — [`seat::Seat`] struct representing a player at the table.
//! - [`util`] — Shared serde helpers for [`BasicPile`] serialization.
//!
//! ## Example
//!
//! ```rust
//! use pkstate::{PKState, act::{Action, Round}, game::{ForcedBets, GameType}, seat::Seat};
//! use cardpack::prelude::*;
//!
//! let players = vec![
//!     Seat { id: None, name: "Alice".to_string(), stack: 1_000 },
//!     Seat { id: None, name: "Bob".to_string(),   stack: 1_000 },
//! ];
//!
//! let preflop = Round(vec![
//!     Action::P0Dealt(basic!("A♠ K♠")),
//!     Action::P1Dealt(basic!("7♦ 2♣")),
//!     Action::P0CBR(100),
//!     Action::P1Fold,
//!     Action::P0Wins(100),
//! ]);
//!
//! let state = PKState {
//!     id: Some("example-hand".to_string()),
//!     datetime: None,
//!     game: GameType::NoLimitHoldem,
//!     button: 0,
//!     forced_bets: ForcedBets::new(50, 100),
//!     board: None,
//!     players,
//!     rounds: vec![preflop],
//! };
//!
//! let yaml = serde_yaml_bw::to_string(&state).unwrap();
//! let restored: PKState = serde_yaml_bw::from_str(&yaml).unwrap();
//! assert_eq!(state, restored);
//! ```

#![warn(clippy::pedantic, clippy::unwrap_used, clippy::expect_used)]

use crate::act::Round;
use crate::game::{ForcedBets, GameType};
use crate::seat::Seat;
use crate::util::{deserialize_basic_pile_opt, serialize_basic_pile_opt};
use cardpack::prelude::BasicPile;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

pub mod act;
pub mod game;
pub mod seat;
pub mod util;

#[derive(Serialize, Deserialize, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PKStates(pub Vec<PKState>);

impl From<Vec<PKState>> for PKStates {
    fn from(states: Vec<PKState>) -> Self {
        PKStates(states)
    }
}

/// The complete state of a poker hand.
///
/// `PKState` captures every piece of information needed to represent or replay a hand:
/// which game is being played, who is sitting where, what the forced bets are, what cards
/// are on the board, and the full ordered sequence of actions across every betting round.
///
/// Optional fields (`id`, `datetime`, `board`) are omitted from YAML when [`None`], keeping
/// serialized output concise.
///
/// # Example
///
/// ```rust
/// use pkstate::{PKState, game::{ForcedBets, GameType}, seat::Seat};
///
/// let state = PKState {
///     id: Some("hand-001".to_string()),
///     datetime: None,
///     game: GameType::NoLimitHoldem,
///     button: 0,
///     forced_bets: ForcedBets::new(50, 100),
///     board: None,
///     players: vec![],
///     rounds: vec![],
/// };
/// ```
#[derive(Serialize, Deserialize, Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct PKState {
    /// Optional unique identifier for the hand (e.g. a UUID or slug).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<String>,
    /// Optional UTC timestamp of when the hand was played.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub datetime: Option<DateTime<Utc>>,
    /// The poker variant being played.
    pub game: GameType,
    /// Seat index (0-based) of the dealer button.
    pub button: usize,
    /// The forced bets (blinds, straddles, ante) for this hand.
    pub forced_bets: ForcedBets,
    /// The community board cards. Serialized as a Unicode card string, e.g. `"9♣ 6♦ 5♥ 5♠ 8♠"`.
    #[serde(
        skip_serializing_if = "Option::is_none",
        serialize_with = "serialize_basic_pile_opt",
        deserialize_with = "deserialize_basic_pile_opt",
        default
    )]
    pub board: Option<BasicPile>,
    /// The players seated at the table, in seat order.
    pub players: Vec<Seat>,
    /// The betting rounds (preflop, flop, turn, river, …), each containing an ordered list of
    /// [`Action`](act::Action)s.
    pub rounds: Vec<Round>,
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::act::Action;
    use cardpack::prelude::*;

    #[test]
    fn the_hand() {
        let players = vec![
            Seat {
                id: None,
                name: "Doyle Brunson".to_string(),
                stack: 1_000_000,
            },
            Seat {
                id: None,
                name: "Eli Elezra".to_string(),
                stack: 1_000_000,
            },
            Seat {
                id: None,
                name: "Antonio Esfandari".to_string(),
                stack: 1_000_000,
            },
            Seat {
                id: None,
                name: "Gus Hansen".to_string(),
                stack: 1_000_000,
            },
            Seat {
                id: None,
                name: "Daniel Negreanu".to_string(),
                stack: 1_000_000,
            },
            Seat {
                id: None,
                name: "Cory Zeidman".to_string(),
                stack: 1_000_000,
            },
            Seat {
                id: None,
                name: "Barry Greenstein".to_string(),
                stack: 1_000_000,
            },
            Seat {
                id: None,
                name: "Amnon Filippi".to_string(),
                stack: 1_000_000,
            },
        ];

        let preflop = Round(vec![
            Action::P1CBR(50),
            Action::P2CBR(100),
            Action::P1Dealt(basic!("8♣ 3♥")),
            Action::P2Dealt(basic!("A♦ Q♣")),
            Action::P3Dealt(basic!("5♦ 5♣")),
            Action::P4Dealt(basic!("6♠ 6♥")),
            Action::P5Dealt(basic!("K♠ J♦")),
            Action::P6Dealt(basic!("4♦ 4♣")),
            Action::P7Dealt(basic!("7♣ 2♦")),
            Action::P0Dealt(basic!("T♠ 2♥")),
            Action::P3CBR(2100),
            Action::P4CBR(5000),
            Action::P5Fold,
            Action::P6Fold,
            Action::P7Fold,
            Action::P0Fold,
            Action::P1Fold,
            Action::P2Fold,
            Action::P3CBR(5000),
        ]);

        let flop = Round(vec![
            Action::DealCommon(basic!("9♣ 6♦ 5♥")),
            Action::P3Check,
            Action::P4CBR(8000),
            Action::P3CBR(26000),
            Action::P4CBR(26000),
        ]);

        let turn = Round(vec![
            Action::DealCommon(basic!("5♠")),
            Action::P3CBR(24000),
            Action::P4CBR(24000),
        ]);

        let river = Round(vec![
            Action::DealCommon(basic!("8♠")),
            Action::P3Check,
            Action::P4CBR(65000),
            Action::P3CBR(945000),
            Action::P4CBR(945000),
            Action::P3Wins(1000150),
            Action::P4Loses(1000000),
        ]);

        let rounds = vec![preflop, flop, turn, river];

        let pkstate = PKState {
            id: Some("the_hand".to_string()),
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players,
            rounds,
        };

        let yaml_string =
            serde_yaml_bw::to_string(&pkstate).expect("Failed to serialize PKState to YAML");

        println!("{}", yaml_string);
    }

    #[test]
    fn yaml_serialization_empty() {
        let pkstate = PKState {
            id: None,
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players: vec![],
            rounds: vec![],
        };

        // Serialize to YAML
        let yaml_string =
            serde_yaml_bw::to_string(&pkstate).expect("Failed to serialize PKState to YAML");

        // Verify the YAML contains the expected content
        assert!(
            yaml_string.contains("NoLimitHoldem"),
            "YAML should contain game type"
        );

        // Deserialize from YAML
        let deserialized: PKState =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKState from YAML");

        // Verify the deserialized state matches the original
        assert_eq!(
            pkstate, deserialized,
            "Deserialized PKState should match original"
        );
    }

    #[test]
    fn yaml_deserialization_all_game_types() {
        let game_types = vec![
            GameType::NoLimitHoldem,
            GameType::LimitHoldem,
            GameType::PLO,
            GameType::Razz,
        ];

        for game_type in game_types {
            let pkstate = PKState {
                id: None,
                datetime: None,
                game: game_type,
                button: 0,
                forced_bets: ForcedBets::new(50, 100),
                board: None,
                players: vec![],
                rounds: vec![],
            };

            // Serialize and deserialize
            let yaml_string =
                serde_yaml_bw::to_string(&pkstate).expect("Failed to serialize PKState to YAML");
            let deserialized: PKState = serde_yaml_bw::from_str(&yaml_string)
                .expect("Failed to deserialize PKState from YAML");

            // Verify round-trip preserves the game type
            assert_eq!(
                pkstate.game, deserialized.game,
                "Game type should be preserved in YAML round-trip for {:?}",
                game_type
            );
        }
    }

    #[test]
    fn yaml_serialization() {
        let seat = Seat {
            id: Some("player1".to_string()),
            name: "Alice".to_string(),
            stack: 1000,
        };

        // Serialize to YAML
        let yaml_string =
            serde_yaml_bw::to_string(&seat).expect("Failed to serialize Seat to YAML");

        // Deserialize from YAML
        let deserialized: Seat =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize Seat from YAML");

        // Verify the deserialized seat matches the original
        assert_eq!(
            seat, deserialized,
            "Deserialized Seat should match original"
        );
        assert_eq!(deserialized.name, "Alice");
        assert_eq!(deserialized.stack, 1000);
    }

    #[test]
    fn yaml_serialization_none_id() {
        let seat = Seat {
            id: None,
            name: "Bob".to_string(),
            stack: 500,
        };

        // Serialize to YAML
        let yaml_string =
            serde_yaml_bw::to_string(&seat).expect("Failed to serialize Seat to YAML");

        // Verify that the id field is not included (skipped) in YAML
        assert!(
            !yaml_string.contains("id:"),
            "YAML should skip the id field when None"
        );

        // Deserialize from YAML
        let deserialized: Seat =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize Seat from YAML");

        // Verify the deserialized seat matches the original
        assert_eq!(
            seat, deserialized,
            "Deserialized Seat should match original"
        );
        assert_eq!(deserialized.id, None);
    }

    #[test]
    fn with_players_yaml_serialization() {
        let players = vec![
            Seat {
                id: Some("player1".to_string()),
                name: "Alice".to_string(),
                stack: 1000,
            },
            Seat {
                id: Some("player2".to_string()),
                name: "Bob".to_string(),
                stack: 500,
            },
        ];

        let pkstate = PKState {
            id: Some("game1".to_string()),
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players,
            rounds: vec![],
        };

        // Serialize to YAML
        let yaml_string =
            serde_yaml_bw::to_string(&pkstate).expect("Failed to serialize PKState to YAML");

        // Verify the YAML structure
        assert!(
            yaml_string.contains("id: game1"),
            "YAML should contain game id"
        );
        assert!(
            yaml_string.contains("players:"),
            "YAML should contain players array"
        );
        assert!(
            yaml_string.contains("Alice"),
            "YAML should contain player name"
        );
        assert!(
            yaml_string.contains("Bob"),
            "YAML should contain player name"
        );

        // Deserialize from YAML
        let deserialized: PKState =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKState from YAML");

        // Verify the deserialized state matches the original
        assert_eq!(
            pkstate, deserialized,
            "Deserialized PKState should match original"
        );
        assert_eq!(deserialized.players.len(), 2);
        assert_eq!(deserialized.players[0].name, "Alice");
        assert_eq!(deserialized.players[1].name, "Bob");
    }

    #[test]
    fn basic_pile_to_string() {
        let pile = BasicPile::default();

        // Empty piles have empty string representation which is valid
        // Verify serialization of the pile works
        let yaml_str = serde_yaml_bw::to_string(&pile).expect("Failed to serialize BasicPile");
        println!("BasicPile YAML: {}", yaml_str);
    }

    #[test]
    fn with_board_yaml_serialization() {
        let board = BasicPile::default();

        let pkstate = PKState {
            id: Some("game2".to_string()),
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(25, 50),
            board: Some(board),
            players: vec![],
            rounds: vec![],
        };

        // Serialize to YAML
        let yaml_string = serde_yaml_bw::to_string(&pkstate)
            .expect("Failed to serialize PKState with board to YAML");

        // Verify the board is serialized as a string
        assert!(
            yaml_string.contains("board:"),
            "YAML should contain board field"
        );

        // Deserialize from YAML
        let deserialized: PKState =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKState from YAML");

        // Verify the deserialized state matches the original
        assert_eq!(
            pkstate, deserialized,
            "Deserialized PKState should match original"
        );
        assert!(deserialized.board.is_some(), "Board should be present");
    }

    #[test]
    fn option_none_skipped_in_yaml() {
        let pkstate = PKState {
            id: None,
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players: vec![],
            rounds: vec![
                Round(vec![Action::P2Check, Action::P0CBR(200), Action::P1Fold]),
                Round(vec![Action::P2Check, Action::P0CBR(200), Action::P1Fold]),
            ],
        };

        // Serialize to YAML
        let yaml_string =
            serde_yaml_bw::to_string(&pkstate).expect("Failed to serialize PKState to YAML");

        // Verify that Option::None fields are skipped
        assert!(
            !yaml_string.contains("id:"),
            "YAML should skip id field when None"
        );
        assert!(
            !yaml_string.contains("datetime:"),
            "YAML should skip datetime field when None"
        );
        assert!(
            !yaml_string.contains("board:"),
            "YAML should skip board field when None"
        );

        // Verify that required fields are still present
        assert!(
            yaml_string.contains("game:"),
            "YAML should contain game field"
        );
        assert!(
            yaml_string.contains("forced_bets:"),
            "YAML should contain forced_bets field"
        );
        assert!(
            yaml_string.contains("players:"),
            "YAML should contain players field"
        );
    }

    #[test]
    fn with_datetime_yaml_serialization() {
        use chrono::TimeZone;

        let datetime = Utc.with_ymd_and_hms(2024, 3, 15, 10, 30, 0).unwrap();

        let pkstate = PKState {
            id: Some("game3".to_string()),
            datetime: Some(datetime),
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players: vec![],
            rounds: vec![],
        };

        // Serialize to YAML
        let yaml_string =
            serde_yaml_bw::to_string(&pkstate).expect("Failed to serialize PKState to YAML");

        println!("PKState with datetime YAML:\n{}", yaml_string);

        // Verify the datetime is serialized
        assert!(
            yaml_string.contains("datetime:"),
            "YAML should contain datetime field"
        );

        // Deserialize from YAML
        let deserialized: PKState =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKState from YAML");

        // Verify the deserialized state matches the original
        assert_eq!(
            pkstate, deserialized,
            "Deserialized PKState should match original"
        );
        assert_eq!(
            deserialized.datetime,
            Some(datetime),
            "Datetime should be preserved"
        );
    }

    #[test]
    fn action_serialization_with_basic_pile() {
        // Test DealCommon with BasicPile
        let deal_common = Action::DealCommon(basic!("A♠ K♠ Q♠"));
        let yaml = serde_yaml_bw::to_string(&deal_common).expect("Failed to serialize DealCommon");
        println!("DealCommon YAML:\n{}", yaml);
        let deserialized: Action =
            serde_yaml_bw::from_str(&yaml).expect("Failed to deserialize DealCommon");
        assert_eq!(deal_common, deserialized, "DealCommon should round-trip");

        // Test P0Dealt with BasicPile
        let p0_dealt = Action::P0Dealt(basic!("A♥ K♥"));
        let yaml = serde_yaml_bw::to_string(&p0_dealt).expect("Failed to serialize P0Dealt");
        println!("P0Dealt YAML:\n{}", yaml);
        let deserialized: Action =
            serde_yaml_bw::from_str(&yaml).expect("Failed to deserialize P0Dealt");
        assert_eq!(p0_dealt, deserialized, "P0Dealt should round-trip");

        // Test P5Dealt with BasicPile
        let p5_dealt = Action::P5Dealt(basic!("7♦ 2♣"));
        let yaml = serde_yaml_bw::to_string(&p5_dealt).expect("Failed to serialize P5Dealt");
        println!("P5Dealt YAML:\n{}", yaml);
        let deserialized: Action =
            serde_yaml_bw::from_str(&yaml).expect("Failed to deserialize P5Dealt");
        assert_eq!(p5_dealt, deserialized, "P5Dealt should round-trip");

        // Test actions without BasicPile
        let p0_check = Action::P0Check;
        let yaml = serde_yaml_bw::to_string(&p0_check).expect("Failed to serialize P0Check");
        println!("P0Check YAML:\n{}", yaml);
        let deserialized: Action =
            serde_yaml_bw::from_str(&yaml).expect("Failed to deserialize P0Check");
        assert_eq!(p0_check, deserialized, "P0Check should round-trip");

        let p2_cbr = Action::P2CBR(500);
        let yaml = serde_yaml_bw::to_string(&p2_cbr).expect("Failed to serialize P2CBR");
        println!("P2CBR YAML:\n{}", yaml);
        let deserialized: Action =
            serde_yaml_bw::from_str(&yaml).expect("Failed to deserialize P2CBR");
        assert_eq!(p2_cbr, deserialized, "P2CBR should round-trip");

        let p3_wins = Action::P3Wins(1000);
        let yaml = serde_yaml_bw::to_string(&p3_wins).expect("Failed to serialize P3Wins");
        println!("P3Wins YAML:\n{}", yaml);
        let deserialized: Action =
            serde_yaml_bw::from_str(&yaml).expect("Failed to deserialize P3Wins");
        assert_eq!(p3_wins, deserialized, "P3Wins should round-trip");

        let p7_fold = Action::P7Fold;
        let yaml = serde_yaml_bw::to_string(&p7_fold).expect("Failed to serialize P7Fold");
        println!("P7Fold YAML:\n{}", yaml);
        let deserialized: Action =
            serde_yaml_bw::from_str(&yaml).expect("Failed to deserialize P7Fold");
        assert_eq!(p7_fold, deserialized, "P7Fold should round-trip");
    }

    #[test]
    fn round_serialization_with_dealt_actions() {
        let round = Round(vec![
            Action::P0Dealt(basic!("A♠ A♥")),
            Action::P1Dealt(basic!("K♦ K♣")),
            Action::P2Dealt(basic!("Q♠ Q♥")),
            Action::DealCommon(basic!("J♠ T♠ 9♠")),
            Action::P0CBR(100),
            Action::P1CBR(300),
            Action::P2Fold,
            Action::P0CBR(600),
            Action::P1CBR(600),
        ]);

        let yaml =
            serde_yaml_bw::to_string(&round).expect("Failed to serialize Round with dealt actions");
        println!("Round with dealt actions YAML:\n{}", yaml);

        let deserialized: Round =
            serde_yaml_bw::from_str(&yaml).expect("Failed to deserialize Round with dealt actions");
        assert_eq!(round, deserialized, "Round should round-trip correctly");
    }

    // ====== PKStates YAML Serialization Tests ======
    // These are AI generated tests. I am not a big fan, but they do the work.

    #[test]
    fn pkstates_empty_yaml_serialization() {
        let pkstates = PKStates(vec![]);

        // Serialize to YAML
        let yaml_string = serde_yaml_bw::to_string(&pkstates)
            .expect("Failed to serialize empty PKStates to YAML");

        // Deserialize from YAML
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize empty PKStates");

        // Verify the deserialized PKStates matches the original
        assert_eq!(pkstates, deserialized, "Empty PKStates should round-trip");
        assert_eq!(
            deserialized.0.len(),
            0,
            "Deserialized PKStates should be empty"
        );
    }

    #[test]
    fn pkstates_single_hand_yaml_serialization() {
        let state = PKState {
            id: Some("hand-001".to_string()),
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players: vec![
                Seat {
                    id: Some("p1".to_string()),
                    name: "Alice".to_string(),
                    stack: 1000,
                },
                Seat {
                    id: Some("p2".to_string()),
                    name: "Bob".to_string(),
                    stack: 2000,
                },
            ],
            rounds: vec![Round(vec![
                Action::P0Dealt(basic!("A♠ K♠")),
                Action::P1Dealt(basic!("7♦ 2♣")),
                Action::P0CBR(100),
                Action::P1Fold,
            ])],
        };

        let pkstates = PKStates(vec![state.clone()]);

        // Serialize to YAML
        let yaml_string = serde_yaml_bw::to_string(&pkstates)
            .expect("Failed to serialize single-hand PKStates to YAML");

        // Verify YAML structure
        assert!(yaml_string.contains("- id:"), "YAML should contain hand id");
        assert!(
            yaml_string.contains("NoLimitHoldem"),
            "YAML should contain game type"
        );
        assert!(
            yaml_string.contains("Alice"),
            "YAML should contain player name"
        );

        // Deserialize from YAML
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKStates");

        // Verify round-trip
        assert_eq!(
            pkstates, deserialized,
            "Single-hand PKStates should round-trip"
        );
        assert_eq!(
            deserialized.0.len(),
            1,
            "Deserialized PKStates should have 1 hand"
        );
        assert_eq!(
            deserialized.0[0].id,
            Some("hand-001".to_string()),
            "Hand id should be preserved"
        );
        assert_eq!(
            deserialized.0[0].players.len(),
            2,
            "Hand should have 2 players"
        );
    }

    #[test]
    fn pkstates_multiple_hands_yaml_serialization() {
        let state1 = PKState {
            id: Some("hand-001".to_string()),
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players: vec![
                Seat {
                    id: None,
                    name: "Alice".to_string(),
                    stack: 1000,
                },
                Seat {
                    id: None,
                    name: "Bob".to_string(),
                    stack: 2000,
                },
            ],
            rounds: vec![],
        };

        let state2 = PKState {
            id: Some("hand-002".to_string()),
            datetime: None,
            game: GameType::LimitHoldem,
            button: 1,
            forced_bets: ForcedBets::new(25, 50),
            board: Some(basic!("9♣ 6♦ 5♥")),
            players: vec![
                Seat {
                    id: None,
                    name: "Charlie".to_string(),
                    stack: 5000,
                },
                Seat {
                    id: None,
                    name: "Diana".to_string(),
                    stack: 3000,
                },
                Seat {
                    id: None,
                    name: "Eve".to_string(),
                    stack: 4000,
                },
            ],
            rounds: vec![Round(vec![Action::P0Check, Action::P1CBR(50)])],
        };

        let pkstates = PKStates(vec![state1.clone(), state2.clone()]);

        // Serialize to YAML
        let yaml_string = serde_yaml_bw::to_string(&pkstates)
            .expect("Failed to serialize multi-hand PKStates to YAML");

        println!("Multi-hand PKStates YAML:\n{}", yaml_string);

        // Verify YAML structure
        assert!(
            yaml_string.contains("hand-001"),
            "YAML should contain first hand id"
        );
        assert!(
            yaml_string.contains("hand-002"),
            "YAML should contain second hand id"
        );
        assert!(
            yaml_string.contains("NoLimitHoldem"),
            "YAML should contain NoLimitHoldem game type"
        );
        assert!(
            yaml_string.contains("LimitHoldem"),
            "YAML should contain LimitHoldem game type"
        );
        assert!(
            yaml_string.contains("board:"),
            "YAML should contain board for hand 2"
        );

        // Deserialize from YAML
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKStates");

        // Verify round-trip
        assert_eq!(
            pkstates, deserialized,
            "Multi-hand PKStates should round-trip"
        );
        assert_eq!(
            deserialized.0.len(),
            2,
            "Deserialized PKStates should have 2 hands"
        );

        // Verify first hand
        assert_eq!(
            deserialized.0[0].id,
            Some("hand-001".to_string()),
            "First hand id should be preserved"
        );
        assert_eq!(
            deserialized.0[0].game,
            GameType::NoLimitHoldem,
            "First hand game type should be preserved"
        );
        assert_eq!(
            deserialized.0[0].button, 0,
            "First hand button should be preserved"
        );
        assert_eq!(
            deserialized.0[0].players.len(),
            2,
            "First hand should have 2 players"
        );

        // Verify second hand
        assert_eq!(
            deserialized.0[1].id,
            Some("hand-002".to_string()),
            "Second hand id should be preserved"
        );
        assert_eq!(
            deserialized.0[1].game,
            GameType::LimitHoldem,
            "Second hand game type should be preserved"
        );
        assert_eq!(
            deserialized.0[1].button, 1,
            "Second hand button should be preserved"
        );
        assert_eq!(
            deserialized.0[1].board,
            Some(basic!("9♣ 6♦ 5♥")),
            "Second hand board should be preserved"
        );
        assert_eq!(
            deserialized.0[1].players.len(),
            3,
            "Second hand should have 3 players"
        );
        assert_eq!(
            deserialized.0[1].rounds.len(),
            1,
            "Second hand should have 1 round"
        );
    }

    #[test]
    fn pkstates_with_datetime_yaml_serialization() {
        use chrono::TimeZone;

        let datetime1 = Utc.with_ymd_and_hms(2024, 3, 15, 10, 30, 0).unwrap();
        let datetime2 = Utc.with_ymd_and_hms(2024, 3, 15, 12, 45, 30).unwrap();

        let state1 = PKState {
            id: Some("hand-001".to_string()),
            datetime: Some(datetime1),
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players: vec![],
            rounds: vec![],
        };

        let state2 = PKState {
            id: Some("hand-002".to_string()),
            datetime: Some(datetime2),
            game: GameType::PLO,
            button: 1,
            forced_bets: ForcedBets::new(25, 50),
            board: None,
            players: vec![],
            rounds: vec![],
        };

        let pkstates = PKStates(vec![state1, state2]);

        // Serialize to YAML
        let yaml_string = serde_yaml_bw::to_string(&pkstates)
            .expect("Failed to serialize PKStates with datetimes to YAML");

        println!("PKStates with datetimes YAML:\n{}", yaml_string);

        // Verify datetime is serialized
        assert!(
            yaml_string.contains("datetime:"),
            "YAML should contain datetime fields"
        );

        // Deserialize from YAML
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKStates");

        // Verify round-trip preserves datetimes
        assert_eq!(
            deserialized.0[0].datetime,
            Some(datetime1),
            "First hand datetime should be preserved"
        );
        assert_eq!(
            deserialized.0[1].datetime,
            Some(datetime2),
            "Second hand datetime should be preserved"
        );
    }

    #[test]
    fn pkstates_with_all_game_types_yaml_serialization() {
        let game_types = vec![
            GameType::NoLimitHoldem,
            GameType::LimitHoldem,
            GameType::PLO,
            GameType::Razz,
        ];

        let mut pkstates_vec = Vec::new();

        for (idx, game_type) in game_types.iter().enumerate() {
            let state = PKState {
                id: Some(format!("hand-{:03}", idx)),
                datetime: None,
                game: *game_type,
                button: idx % 2,
                forced_bets: ForcedBets::new(50, 100),
                board: None,
                players: vec![],
                rounds: vec![],
            };
            pkstates_vec.push(state);
        }

        let pkstates = PKStates(pkstates_vec);

        // Serialize to YAML
        let yaml_string = serde_yaml_bw::to_string(&pkstates)
            .expect("Failed to serialize PKStates with all game types to YAML");

        // Verify all game types are serialized
        assert!(
            yaml_string.contains("NoLimitHoldem"),
            "YAML should contain NoLimitHoldem"
        );
        assert!(
            yaml_string.contains("LimitHoldem"),
            "YAML should contain LimitHoldem"
        );
        assert!(yaml_string.contains("PLO"), "YAML should contain PLO");
        assert!(yaml_string.contains("Razz"), "YAML should contain Razz");

        // Deserialize from YAML
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKStates");

        // Verify round-trip preserves all game types
        assert_eq!(deserialized.0.len(), 4, "Should have 4 hands");
        assert_eq!(deserialized.0[0].game, GameType::NoLimitHoldem);
        assert_eq!(deserialized.0[1].game, GameType::LimitHoldem);
        assert_eq!(deserialized.0[2].game, GameType::PLO);
        assert_eq!(deserialized.0[3].game, GameType::Razz);
    }

    #[test]
    fn pkstates_with_complex_actions_yaml_serialization() {
        let round1 = Round(vec![
            Action::P0Dealt(basic!("A♠ K♠")),
            Action::P1Dealt(basic!("Q♦ J♦")),
            Action::P2Dealt(basic!("T♥ 9♥")),
            Action::P0CBR(100),
            Action::P1CBR(300),
            Action::P2Fold,
            Action::P0CBR(600),
            Action::P1CBR(600),
        ]);

        let round2 = Round(vec![
            Action::DealCommon(basic!("K♣ 9♣ 5♠")),
            Action::P0Check,
            Action::P1CBR(500),
            Action::P0CBR(1500),
            Action::P1Fold,
            Action::P0Wins(2500),
        ]);

        let state = PKState {
            id: Some("complex-hand".to_string()),
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players: vec![
                Seat {
                    id: Some("alice".to_string()),
                    name: "Alice".to_string(),
                    stack: 10000,
                },
                Seat {
                    id: Some("bob".to_string()),
                    name: "Bob".to_string(),
                    stack: 8000,
                },
                Seat {
                    id: Some("charlie".to_string()),
                    name: "Charlie".to_string(),
                    stack: 5000,
                },
            ],
            rounds: vec![round1, round2],
        };

        let pkstates = PKStates(vec![state.clone()]);

        // Serialize to YAML
        let yaml_string = serde_yaml_bw::to_string(&pkstates)
            .expect("Failed to serialize PKStates with complex actions to YAML");

        println!("Complex PKStates YAML:\n{}", yaml_string);

        // Verify structure
        assert!(
            yaml_string.contains("P0Dealt"),
            "YAML should contain dealt actions"
        );
        assert!(
            yaml_string.contains("P0CBR") || yaml_string.contains("cbr:"),
            "YAML should contain bet/raise actions"
        );
        assert!(
            yaml_string.contains("P1Fold") || yaml_string.contains("fold:"),
            "YAML should contain fold actions"
        );
        assert!(
            yaml_string.contains("DealCommon"),
            "YAML should contain common card actions"
        );

        // Deserialize from YAML
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize complex PKStates");

        // Verify round-trip
        assert_eq!(pkstates, deserialized, "Complex PKStates should round-trip");
        assert_eq!(
            deserialized.0[0].rounds.len(),
            2,
            "Hand should have 2 rounds"
        );
        assert_eq!(
            deserialized.0[0].players.len(),
            3,
            "Hand should have 3 players"
        );
    }

    #[test]
    fn pkstates_with_varied_stacks_yaml_serialization() {
        let state = PKState {
            id: Some("varied-stacks".to_string()),
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 2,
            forced_bets: ForcedBets::new(10, 20),
            board: None,
            players: vec![
                Seat {
                    id: None,
                    name: "Short Stack".to_string(),
                    stack: 100,
                },
                Seat {
                    id: None,
                    name: "Medium Stack".to_string(),
                    stack: 5000,
                },
                Seat {
                    id: None,
                    name: "Big Stack".to_string(),
                    stack: 50000,
                },
                Seat {
                    id: None,
                    name: "All In".to_string(),
                    stack: 0,
                },
            ],
            rounds: vec![],
        };

        let pkstates = PKStates(vec![state]);

        // Serialize to YAML
        let yaml_string = serde_yaml_bw::to_string(&pkstates)
            .expect("Failed to serialize PKStates with varied stacks to YAML");

        // Verify stacks are preserved
        assert!(
            yaml_string.contains("100"),
            "YAML should contain short stack"
        );
        assert!(
            yaml_string.contains("5000"),
            "YAML should contain medium stack"
        );
        assert!(
            yaml_string.contains("50000"),
            "YAML should contain big stack"
        );
        assert!(
            yaml_string.contains("0"),
            "YAML should contain all-in stack"
        );

        // Deserialize from YAML
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKStates");

        // Verify stacks are preserved
        assert_eq!(
            deserialized.0[0].players[0].stack, 100,
            "Short stack should be preserved"
        );
        assert_eq!(
            deserialized.0[0].players[1].stack, 5000,
            "Medium stack should be preserved"
        );
        assert_eq!(
            deserialized.0[0].players[2].stack, 50000,
            "Big stack should be preserved"
        );
        assert_eq!(
            deserialized.0[0].players[3].stack, 0,
            "All-in stack should be preserved"
        );
    }

    #[test]
    fn pkstates_yaml_serialization_preserves_order() {
        let mut states = Vec::new();

        for i in 0..5 {
            let state = PKState {
                id: Some(format!("hand-{}", i)),
                datetime: None,
                game: GameType::NoLimitHoldem,
                button: i,
                forced_bets: ForcedBets::new(50, 100),
                board: None,
                players: vec![
                    Seat {
                        id: None,
                        name: format!("Player {}", i * 2),
                        stack: 1000 + (i * 100),
                    },
                    Seat {
                        id: None,
                        name: format!("Player {}", i * 2 + 1),
                        stack: 2000 - (i * 50),
                    },
                ],
                rounds: vec![],
            };
            states.push(state);
        }

        let pkstates = PKStates(states);

        // Serialize to YAML
        let yaml_string =
            serde_yaml_bw::to_string(&pkstates).expect("Failed to serialize PKStates to YAML");

        // Deserialize from YAML
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKStates");

        // Verify order is preserved
        assert_eq!(deserialized.0.len(), 5, "Should have 5 hands");

        for i in 0..5 {
            assert_eq!(
                deserialized.0[i].id,
                Some(format!("hand-{}", i)),
                "Hand {} id should be preserved in order",
                i
            );
            assert_eq!(
                deserialized.0[i].button, i,
                "Hand {} button should be preserved in order",
                i
            );
            assert_eq!(
                deserialized.0[i].players[0].name,
                format!("Player {}", i * 2),
                "Hand {} first player name should be preserved in order",
                i
            );
        }
    }

    #[test]
    fn pkstates_from_vec_conversion() {
        let states = vec![
            PKState {
                id: Some("hand-1".to_string()),
                datetime: None,
                game: GameType::NoLimitHoldem,
                button: 0,
                forced_bets: ForcedBets::new(50, 100),
                board: None,
                players: vec![],
                rounds: vec![],
            },
            PKState {
                id: Some("hand-2".to_string()),
                datetime: None,
                game: GameType::LimitHoldem,
                button: 1,
                forced_bets: ForcedBets::new(25, 50),
                board: None,
                players: vec![],
                rounds: vec![],
            },
        ];

        let pkstates: PKStates = states.into();

        // Verify conversion works
        assert_eq!(pkstates.0.len(), 2, "PKStates should have 2 hands");
        assert_eq!(
            pkstates.0[0].id,
            Some("hand-1".to_string()),
            "First hand should be preserved"
        );
        assert_eq!(
            pkstates.0[1].id,
            Some("hand-2".to_string()),
            "Second hand should be preserved"
        );

        // Verify it can be serialized/deserialized
        let yaml_string =
            serde_yaml_bw::to_string(&pkstates).expect("Failed to serialize PKStates to YAML");
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKStates");
        assert_eq!(
            pkstates, deserialized,
            "Converted PKStates should round-trip"
        );
    }

    #[test]
    fn pkstates_partial_none_fields_yaml_serialization() {
        let state1 = PKState {
            id: Some("hand-1".to_string()),
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: None,
            players: vec![],
            rounds: vec![],
        };

        let state2 = PKState {
            id: None,
            datetime: None,
            game: GameType::NoLimitHoldem,
            button: 0,
            forced_bets: ForcedBets::new(50, 100),
            board: Some(basic!("A♠ K♠ Q♠")),
            players: vec![],
            rounds: vec![],
        };

        let pkstates = PKStates(vec![state1, state2]);

        // Serialize to YAML
        let yaml_string =
            serde_yaml_bw::to_string(&pkstates).expect("Failed to serialize PKStates to YAML");

        println!("Partial None fields YAML:\n{}", yaml_string);

        // Deserialize from YAML
        let deserialized: PKStates =
            serde_yaml_bw::from_str(&yaml_string).expect("Failed to deserialize PKStates");

        // Verify first hand
        assert_eq!(
            deserialized.0[0].id,
            Some("hand-1".to_string()),
            "First hand id should be present"
        );
        assert_eq!(
            deserialized.0[0].board, None,
            "First hand board should be None"
        );

        // Verify second hand
        assert_eq!(deserialized.0[1].id, None, "Second hand id should be None");
        assert_eq!(
            deserialized.0[1].board,
            Some(basic!("A♠ K♠ Q♠")),
            "Second hand board should be present"
        );
    }
}