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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use crate::core::heuristics::Heuristic;
use crate::core::logic::{CardDatabase, GameState};
#[cfg(feature = "extension-module")]
use pyo3::prelude::*;
use rand::prelude::*;
use rand::rngs::SmallRng;
use rand::SeedableRng;
// use crate::core::logic::{Phase, ActionReceiver};
#[cfg(feature = "nn")]
#[cfg(feature = "nn")]
use crate::core::logic::ai_encoding::GameStateEncoding;
#[cfg(feature = "nn")]
use ort::session::Session;
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use smallvec::SmallVec;
use std::collections::HashMap;
use std::f32;
#[cfg(feature = "nn")]
use std::sync::{Arc, Mutex};
use std::time::Duration;

#[derive(Default, Clone, Copy)]
pub struct MCTSProfiler {
    pub determinization: Duration,
    pub selection: Duration,
    pub expansion: Duration,
    pub simulation: Duration,
    pub backpropagation: Duration,
}

// DeckStats and EvalMode moved to heuristics.rs

impl MCTSProfiler {
    pub fn merge(&mut self, other: &Self) {
        self.determinization += other.determinization;
        self.selection += other.selection;
        self.expansion += other.expansion;
        self.simulation += other.simulation;
        self.backpropagation += other.backpropagation;
    }

    pub fn print(&self, total: Duration) {
        let total_secs = total.as_secs_f64();
        if total_secs == 0.0 {
            return;
        }
        println!("[MCTS Profile] Breakdown:");
        let items = [
            ("Determinization", self.determinization),
            ("Selection", self.selection),
            ("Expansion", self.expansion),
            ("Simulation", self.simulation),
            ("Backpropagation", self.backpropagation),
        ];

        for (name, dur) in items {
            let secs = dur.as_secs_f64();
            println!(
                "  - {:<16}: {:>8.3}s ({:>6.1}%)",
                name,
                secs,
                (secs / total_secs) * 100.0
            );
        }
    }
}

struct Node {
    visit_count: u32,
    value_sum: f32,
    player_just_moved: u8,
    prior_prob: f32, // AlphaZero Policy P(s,a)
    untried_actions: SmallVec<[i32; 32]>,
    children: SmallVec<[(i32, usize); 16]>, // (Action, NodeIndex in Arena)
    parent: Option<usize>,
    parent_action: i32,
}

pub struct MCTS {
    nodes: Vec<Node>,
    rng: SmallRng,
    unseen_buffer: SmallVec<[i32; 64]>,
    legal_buffer: SmallVec<[i32; 32]>,
    reusable_state: GameState,
    pub evaluator:
        Option<std::sync::Arc<Box<dyn crate::core::alphazero_evaluator::AlphaZeroEvaluator>>>,
    pub cpu_batch_size: usize,
    pub leaf_batch_size: usize,
    pub exploration_weight: f32,
}

#[cfg_attr(feature = "extension-module", pyclass)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SearchHorizon {
    GameEnd(),
    TurnEnd(),
    Limited(u32),
}

// EvalMode moved to heuristics.rs

impl MCTS {
    pub fn new() -> Self {
        Self {
            nodes: Vec::with_capacity(256), // 128 sims → ~128 nodes max; was 16384 = 7.2MB per tree
            rng: SmallRng::from_os_rng(),
            unseen_buffer: SmallVec::with_capacity(64),
            legal_buffer: SmallVec::with_capacity(32),
            reusable_state: GameState::default(),
            evaluator: None,
            cpu_batch_size: 16,
            leaf_batch_size: 512,
            exploration_weight: 1.41,
        }
    }

    pub fn with_evaluator(
        eval: std::sync::Arc<Box<dyn crate::core::alphazero_evaluator::AlphaZeroEvaluator>>,
        batch_size: usize,
    ) -> Self {
        let mut mcts = Self::new();
        mcts.evaluator = Some(eval);
        mcts.cpu_batch_size = batch_size;
        mcts
    }

    pub fn get_tree_size(&self) -> usize {
        self.nodes.len()
    }

    pub fn get_max_depth(&self) -> usize {
        let mut max_d = 0;
        for i in 0..self.nodes.len() {
            let mut d = 0;
            let mut curr = i;
            while let Some(p) = self.nodes[curr].parent {
                d += 1;
                curr = p;
            }
            if d > max_d {
                max_d = d;
            }
        }
        max_d
    }

    pub fn search_parallel(
        &self,
        root_state: &GameState,
        db: &CardDatabase,
        num_sims: usize,
        timeout_sec: f32,
        horizon: SearchHorizon,
        heuristic: &dyn Heuristic,
        shuffle_self: bool,
    ) -> Vec<(i32, f32, u32)> {
        #[cfg(feature = "parallel")]
        let mut _num_threads = rayon::current_num_threads().max(1);
        #[cfg(not(feature = "parallel"))]
        let _num_threads = 1;
        let num_threads = _num_threads;

        let start_overall = std::time::Instant::now();
        let leaf_batch_size = self.leaf_batch_size;
        let sims_per_thread = if num_sims > 0 {
            (num_sims + num_threads - 1) / num_threads
        } else {
            0
        };

        // Collect results
        #[cfg(feature = "parallel")]
        let results: Vec<(Vec<(i32, f32, u32)>, MCTSProfiler)> = {
            let pool = rayon::ThreadPoolBuilder::new()
                .stack_size(16 * 1024 * 1024)
                .num_threads(num_threads)
                .build()
                .unwrap_or_else(|_| {
                    rayon::ThreadPoolBuilder::default()
                        .num_threads(num_threads)
                        .build()
                        .unwrap()
                });

            pool.install(|| {
                (0..num_threads)
                    .into_par_iter()
                    .map(|_| {
                        let mut mcts = MCTS::new();
                        mcts.leaf_batch_size = leaf_batch_size;
                        mcts.search_custom(
                            root_state,
                            db,
                            sims_per_thread,
                            timeout_sec,
                            horizon,
                            heuristic,
                            shuffle_self,
                            true,
                        )
                    })
                    .collect()
            })
        };

        #[cfg(not(feature = "parallel"))]
        let results: Vec<(Vec<(i32, f32, u32)>, MCTSProfiler)> = vec![{
            let mut mcts = MCTS::new();
            mcts.leaf_batch_size = leaf_batch_size;
            mcts.search_custom(
                root_state,
                db,
                num_sims,
                timeout_sec,
                horizon,
                heuristic,
                shuffle_self,
                true,
            )
        }];

        // Merge results
        let mut agg_map: HashMap<i32, (f32, u32)> = HashMap::new();
        let mut total_visits = 0;
        let mut agg_profile = MCTSProfiler::default();

        for (res, profile) in results {
            agg_profile.merge(&profile);
            for (action, score, visits) in res {
                let entry = agg_map.entry(action).or_insert((0.0, 0));
                let total_value = score * visits as f32;
                entry.0 += total_value;
                entry.1 += visits;
                total_visits += visits;
            }
        }

        // Logging Speed
        let duration = start_overall.elapsed();
        let _sims_per_sec = total_visits as f64 / duration.as_secs_f64();
        if total_visits > 100 {
            // println!("[MCTS] Completed {} sims in {:.3}s ({:.0} sims/s)", total_visits, duration.as_secs_f64(), sims_per_sec);
            // agg_profile.print(duration);
        }

        let mut stats: Vec<(i32, f32, u32)> = agg_map
            .into_iter()
            .map(|(action, (sum_val, visits))| {
                if visits > 0 {
                    (action, sum_val / visits as f32, visits)
                } else {
                    (action, 0.0, 0)
                }
            })
            .collect();

        stats.sort_by_key(|&(_, _, v)| std::cmp::Reverse(v));
        stats
    }

    fn root_action_tiebreak(
        root_state: &GameState,
        db: &CardDatabase,
        action: i32,
    ) -> (i32, i32) {
        let mut sim = root_state.clone();
        if sim.step(db, action).is_err() {
            return (i32::MIN, i32::MIN);
        }
        let me = root_state.current_player as usize;
        let opp = 1 - me;
        let live_diff = sim.core.players[me].success_lives.len() as i32
            - sim.core.players[opp].success_lives.len() as i32;
        let score_diff = sim.core.players[me].score as i32 - sim.core.players[opp].score as i32;
        (live_diff, score_diff)
    }

    pub fn search_parallel_mode(
        &self,
        root_state: &GameState,
        db: &CardDatabase,
        num_sims: usize,
        timeout_sec: f32,
        horizon: SearchHorizon,
        eval_mode: crate::core::heuristics::EvalMode,
        heuristic: &dyn Heuristic,
    ) -> Vec<(i32, f32, u32)> {
        let start_overall = std::time::Instant::now();

        #[cfg(feature = "parallel")]
        let mut _num_threads = rayon::current_num_threads().max(1);
        #[cfg(not(feature = "parallel"))]
        let mut _num_threads = 1;

        let num_threads = _num_threads;

        let leaf_batch_size = self.leaf_batch_size;
        let sims_per_thread = if num_sims > 0 {
            (num_sims + num_threads - 1) / num_threads
        } else {
            0
        };

        // Collect results
        #[cfg(feature = "parallel")]
        let results: Vec<(Vec<(i32, f32, u32)>, MCTSProfiler)> = (0..num_threads)
            .into_par_iter()
            .map(|_| {
                let mut mcts = MCTS::new();
                mcts.leaf_batch_size = leaf_batch_size;
                mcts.search_mode(
                    root_state,
                    db,
                    sims_per_thread,
                    timeout_sec,
                    horizon,
                    eval_mode,
                    heuristic,
                )
            })
            .collect();

        #[cfg(not(feature = "parallel"))]
        let results: Vec<(Vec<(i32, f32, u32)>, MCTSProfiler)> = vec![{
            let mut mcts = MCTS::new();
            mcts.leaf_batch_size = leaf_batch_size;
            mcts.search_mode(
                root_state,
                db,
                num_sims,
                timeout_sec,
                horizon,
                eval_mode,
                heuristic,
            )
        }];

        // Merge results
        let mut agg_map: HashMap<i32, (f32, u32)> = HashMap::new();
        let mut total_visits = 0;
        let mut agg_profile = MCTSProfiler::default();

        for (res, profile) in results {
            agg_profile.merge(&profile);
            for (action, score, visits) in res {
                let entry = agg_map.entry(action).or_insert((0.0, 0));
                let total_value = score * visits as f32;
                entry.0 += total_value;
                entry.1 += visits;
                total_visits += visits;
            }
        }

        // Logging Speed
        let duration = start_overall.elapsed();
        let _sims_per_sec = total_visits as f64 / duration.as_secs_f64();
        if total_visits > 100 {
            // println!("[MCTS Mode] Completed {} sims in {:.3}s ({:.0} sims/s)", total_visits, duration.as_secs_f64(), sims_per_sec);
            // agg_profile.print(duration);
        }

        let mut stats: Vec<(i32, f32, u32)> = agg_map
            .into_iter()
            .map(|(action, (sum_val, visits))| {
                if visits > 0 {
                    (action, sum_val / visits as f32, visits)
                } else {
                    (action, 0.0, 0)
                }
            })
            .collect();

        stats.sort_by_key(|&(_, _, v)| std::cmp::Reverse(v));
        stats
    }

    pub fn search(
        &mut self,
        root_state: &GameState,
        db: &CardDatabase,
        num_sims: usize,
        timeout_sec: f32,
        horizon: SearchHorizon,
        heuristic: &dyn Heuristic,
    ) -> (Vec<(i32, f32, u32)>, MCTSProfiler) {
        self.search_custom(
            root_state,
            db,
            num_sims,
            timeout_sec,
            horizon,
            heuristic,
            false,
            false,
        )
    }

    pub fn search_custom(
        &mut self,
        root_state: &GameState,
        db: &CardDatabase,
        num_sims: usize,
        timeout_sec: f32,
        horizon: SearchHorizon,
        heuristic: &dyn Heuristic,
        shuffle_self: bool,
        enable_rollout: bool,
    ) -> (Vec<(i32, f32, u32)>, MCTSProfiler) {
        use crate::core::heuristics::calculate_deck_expectations;
        let p0_stats = calculate_deck_expectations(&root_state.players[0].deck, db);

        let mut p1_unseen = root_state.players[1].hand.clone();
        p1_unseen.extend(root_state.players[1].deck.iter().cloned());
        let p1_stats = calculate_deck_expectations(&p1_unseen, db);

        self.run_mcts_config(
            root_state,
            db,
            num_sims,
            timeout_sec,
            horizon,
            shuffle_self,
            enable_rollout,
            |state, _db| {
                if state.is_terminal() {
                    match state.get_winner() {
                        0 => 1.0,
                        1 => 0.0,
                        _ => 0.5,
                    }
                } else {
                    heuristic.evaluate(
                        state,
                        db,
                        root_state.players[0].score,
                        root_state.players[1].score,
                        crate::core::heuristics::EvalMode::Normal,
                        Some(p0_stats),
                        Some(p1_stats),
                    )
                }
            },
        )
    }

    pub fn search_mode(
        &mut self,
        root_state: &GameState,
        db: &CardDatabase,
        num_sims: usize,
        timeout_sec: f32,
        horizon: SearchHorizon,
        eval_mode: crate::core::heuristics::EvalMode,
        heuristic: &dyn Heuristic,
    ) -> (Vec<(i32, f32, u32)>, MCTSProfiler) {
        use crate::core::heuristics::{calculate_deck_expectations, EvalMode};
        // Pre-calculate deck expectations for optimization
        let p0_stats = calculate_deck_expectations(&root_state.players[0].deck, db);

        let mut p1_unseen = root_state.players[1].hand.clone();
        p1_unseen.extend(root_state.players[1].deck.iter().cloned());
        let p1_stats = calculate_deck_expectations(&p1_unseen, db);

        self.run_mcts_config(
            root_state,
            db,
            num_sims,
            timeout_sec,
            horizon,
            eval_mode == EvalMode::Blind,
            true,
            |state, _db| {
                if state.is_terminal() {
                    if eval_mode == EvalMode::Solitaire {
                        match state.get_winner() {
                            0 => 1.0,
                            1 => 0.0,
                            _ => 0.5,
                        }
                    } else {
                        match state.get_winner() {
                            0 => 1.0,
                            1 => 0.0,
                            _ => 0.5,
                        }
                    }
                } else {
                    heuristic.evaluate(
                        state,
                        db,
                        root_state.players[0].score,
                        root_state.players[1].score,
                        eval_mode,
                        Some(p0_stats),
                        Some(p1_stats),
                    )
                }
            },
        )
    }

    pub fn run_mcts_config<F>(
        &mut self,
        root_state: &GameState,
        db: &CardDatabase,
        num_sims: usize,
        timeout_sec: f32,
        horizon: SearchHorizon,
        shuffle_self: bool,
        enable_rollout: bool,
        mut eval_fn: F,
    ) -> (Vec<(i32, f32, u32)>, MCTSProfiler)
    where
        F: FnMut(&GameState, &CardDatabase) -> f32,
    {
        self.nodes.clear();
        // Reserve exactly enough for the entire search upfront.
        // Each simulation adds ≤1 node, so this is a tight upper bound.
        // Prevents Vec from doubling (8192→16384 = 7.2MB) when system RAM is low.
        if num_sims > 0 {
            self.nodes.reserve(num_sims + 1);
        }
        let start_time = std::time::Instant::now();
        let timeout = if timeout_sec > 0.0 {
            Some(std::time::Duration::from_secs_f32(timeout_sec))
        } else {
            None
        };
        let root_start_turn = root_state.turn;
        let mut profiler = MCTSProfiler::default();

        // Root Node
        let mut legal_indices: SmallVec<[i32; 32]> = SmallVec::with_capacity(32);
        root_state.generate_legal_actions(
            db,
            root_state.current_player as usize,
            &mut legal_indices,
        );

        if legal_indices.is_empty() {
            return (vec![(0, 0.5, 0)], profiler);
        }
        if legal_indices.len() == 1 {
            return (vec![(legal_indices[0], 0.5, 1)], profiler);
        }

        self.nodes.push(Node {
            visit_count: 0,
            value_sum: 0.0,
            player_just_moved: 1 - root_state.current_player,
            prior_prob: 1.0,
            untried_actions: legal_indices,
            children: SmallVec::new(),
            parent: None,
            parent_action: 0,
        });

        let mut sims_done = 0;
        loop {
            if num_sims > 0 && sims_done >= num_sims {
                break;
            }
            if let Some(to) = timeout {
                if start_time.elapsed() >= to {
                    break;
                }
            }
            if num_sims == 0 && timeout.is_none() {
                break;
            } // Safety

            let is_trace = num_sims == 1
                && std::env::var("RABUKA_MCTS_TRACE")
                    .map(|value| value == "1")
                    .unwrap_or(false);
            let t_setup = std::time::Instant::now();

            // --- BRANCH: AlphaZero Batch or Sequential ---
            if let Some(ref evaluator) = self.evaluator {
                let remaining_sims = num_sims.saturating_sub(sims_done).max(1);
                let batch_size = self.cpu_batch_size.max(1).min(remaining_sims);
                let mut batch_leaf_indices = Vec::with_capacity(batch_size);
                let mut batch_leaf_states = Vec::with_capacity(batch_size);

                for _ in 0..batch_size {
                    // 1. Setup (Determinization)
                    self.reusable_state.copy_from(&root_state);
                    let state = &mut self.reusable_state;
                    state.ui.silent = true;
                    // (Determinization logic duplicated for now within batch loop)
                    let me = root_state.current_player as usize;
                    let opp = 1 - me;
                    let opp_hand_len = state.players[opp].hand.len();
                    self.unseen_buffer.clear();
                    self.unseen_buffer
                        .extend_from_slice(&state.players[opp].hand);
                    self.unseen_buffer
                        .extend_from_slice(&state.players[opp].deck);
                    self.unseen_buffer.shuffle(&mut self.rng);
                    state.players[opp]
                        .hand
                        .copy_from_slice(&self.unseen_buffer[0..opp_hand_len]);
                    state.players[opp]
                        .deck
                        .copy_from_slice(&self.unseen_buffer[opp_hand_len..]);

                    // 2. Selection
                    let mut node_idx = 0;
                    while self.nodes[node_idx].untried_actions.is_empty()
                        && !self.nodes[node_idx].children.is_empty()
                    {
                        node_idx = Self::select_child_logic(
                            &self.nodes,
                            node_idx,
                            self.exploration_weight,
                        );
                        let action = self.nodes[node_idx].parent_action;
                        let _ = state.step(db, action);
                    }

                    // 3. Expansion
                    if !self.nodes[node_idx].untried_actions.is_empty() {
                        let idx = self
                            .rng
                            .random_range(0..self.nodes[node_idx].untried_actions.len());
                        let action = self.nodes[node_idx].untried_actions.swap_remove(idx);
                        let actor = state.current_player;
                        let _ = state.step(db, action);
                        state.sync_stat_caches(0, db);
                        state.sync_stat_caches(1, db);

                        self.legal_buffer.clear();
                        state.generate_legal_actions(
                            db,
                            state.current_player as usize,
                            &mut self.legal_buffer,
                        );
                        let new_legal_indices = self.legal_buffer.clone();
                        let prob = 1.0 / (new_legal_indices.len().max(1) as f32);

                        let new_idx = self.nodes.len();
                        self.nodes.push(Node {
                            visit_count: 0,
                            value_sum: 0.0,
                            player_just_moved: actor,
                            prior_prob: prob,
                            untried_actions: new_legal_indices,
                            children: SmallVec::new(),
                            parent: Some(node_idx),
                            parent_action: action,
                        });
                        self.nodes[node_idx].children.push((action, new_idx));
                        node_idx = new_idx;
                    }

                    batch_leaf_indices.push(node_idx);
                    batch_leaf_states.push(state.clone());
                }

                // 4. Collective Inference
                let outputs = evaluator.evaluate_batch(&batch_leaf_states, db);

                // 5. Apply results & Backprop
                for (i, output) in outputs.into_iter().enumerate() {
                    let leaf_idx = batch_leaf_indices[i];

                    // Update Policy Priors for children (if expanded)
                    if !self.nodes[leaf_idx].children.is_empty() {
                        // Assuming ACTION_SPACE is 8192
                        for j in 0..self.nodes[leaf_idx].children.len() {
                            let (action, child_idx) = self.nodes[leaf_idx].children[j];
                            if (action as usize) < output.policy.len() {
                                self.nodes[child_idx].prior_prob = output.policy[action as usize];
                            }
                        }
                    }

                    // Hybrid Reward: NN Value + Heuristic Weighting
                    let leaf_state: &GameState = &batch_leaf_states[i];
                    let reward_p0 = if leaf_state.is_terminal() {
                        match leaf_state.get_winner() {
                            0 => 1.0,
                            1 => 0.0,
                            _ => 0.5,
                        }
                    } else {
                        let current_player_win_prob = ((output.value.clamp(-1.0, 1.0) + 1.0) * 0.5).clamp(0.0, 1.0);
                        if leaf_state.current_player == 0 {
                            current_player_win_prob
                        } else {
                            1.0 - current_player_win_prob
                        }
                    };

                    // Backprop
                    let mut curr = Some(leaf_idx);
                    while let Some(idx) = curr {
                        let node_p_moved = self.nodes[idx].player_just_moved;
                        let node = &mut self.nodes[idx];
                        node.visit_count += 1;
                        if node_p_moved == 0 {
                            node.value_sum += reward_p0;
                        } else {
                            node.value_sum += 1.0 - reward_p0;
                        }
                        curr = node.parent;
                    }
                }
                sims_done += batch_size;
            } else {
                sims_done += 1;
                let mut node_idx = 0;

                // 1. Setup & Determinization (Sequential)
                self.reusable_state.copy_from(&root_state);
                let state = &mut self.reusable_state;
                state.ui.silent = true;

                let me = root_state.current_player as usize;
                let opp = 1 - me;
                let opp_hand_len = state.players[opp].hand.len();

                self.unseen_buffer.clear();
                self.unseen_buffer
                    .extend_from_slice(&state.players[opp].hand);
                self.unseen_buffer
                    .extend_from_slice(&state.players[opp].deck);
                self.unseen_buffer.shuffle(&mut self.rng);

                state.players[opp]
                    .hand
                    .copy_from_slice(&self.unseen_buffer[0..opp_hand_len]);
                state.players[opp]
                    .deck
                    .copy_from_slice(&self.unseen_buffer[opp_hand_len..]);

                if shuffle_self {
                    let mut my_deck = state.players[me].deck.clone();
                    my_deck.shuffle(&mut self.rng);
                    state.players[me].deck = my_deck;
                }

                // --- SEQUENTIAL PATH (Original logic) ---
                profiler.determinization += t_setup.elapsed();

                // 2. Selection
                let t_selection = std::time::Instant::now();
                while self.nodes[node_idx].untried_actions.is_empty()
                    && !self.nodes[node_idx].children.is_empty()
                {
                    node_idx =
                        Self::select_child_logic(&self.nodes, node_idx, self.exploration_weight);
                    let action = self.nodes[node_idx].parent_action;
                    if is_trace {
                        println!("[MCTS TRACE] Selection: Action {}", action);
                    }
                    let _ = state.step(db, action);
                }
                profiler.selection += t_selection.elapsed();

                // 3. Expansion
                let t_expansion = std::time::Instant::now();
                if !self.nodes[node_idx].untried_actions.is_empty() {
                    let idx = self
                        .rng
                        .random_range(0..self.nodes[node_idx].untried_actions.len());
                    let action = self.nodes[node_idx].untried_actions.swap_remove(idx);
                    if is_trace {
                        println!("[MCTS TRACE] Expansion: Action {}", action);
                    }

                    let actor = state.current_player;
                    let _ = state.step(db, action);

                    state.sync_stat_caches(0, db);
                    state.sync_stat_caches(1, db);

                    self.legal_buffer.clear();
                    let mut is_capped = false;
                    if let SearchHorizon::TurnEnd() = horizon {
                        if state.turn > root_start_turn {
                            is_capped = true;
                        }
                    }
                    if !is_capped {
                        state.generate_legal_actions(
                            db,
                            state.current_player as usize,
                            &mut self.legal_buffer,
                        );
                    }

                    let new_legal_indices = self.legal_buffer.clone();
                    let prob = 1.0 / (new_legal_indices.len().max(1) as f32);
                    let new_node = Node {
                        visit_count: 0,
                        value_sum: 0.0,
                        player_just_moved: actor,
                        prior_prob: prob,
                        untried_actions: new_legal_indices,
                        children: SmallVec::new(),
                        parent: Some(node_idx),
                        parent_action: action,
                    };

                    let new_idx = self.nodes.len();
                    self.nodes.push(new_node);
                    self.nodes[node_idx].children.push((action, new_idx));
                    node_idx = new_idx;
                }
                profiler.expansion += t_expansion.elapsed();

                // 4. Simulation
                let t_simulation = std::time::Instant::now();
                let reward_p0 = {
                    // Standard CPU Rollout
                    let mut depth = 0;
                    if enable_rollout {
                        while !state.is_terminal() && depth < 200 {
                            if let SearchHorizon::Limited(max_depth) = horizon {
                                if depth >= max_depth as usize {
                                    break;
                                }
                            }
                            if let SearchHorizon::TurnEnd() = horizon {
                                if state.turn > root_start_turn {
                                    break;
                                }
                            }
                            state.generate_legal_actions(
                                db,
                                state.current_player as usize,
                                &mut self.legal_buffer,
                            );
                            if self.legal_buffer.is_empty() {
                                break;
                            }
                            let chunk_action = *self.legal_buffer.choose(&mut self.rng).unwrap();
                            let _ = state.step(db, chunk_action);
                            depth += 1;
                        }
                    }
                    eval_fn(&state, db)
                };
                profiler.simulation += t_simulation.elapsed();

                // 5. Backpropagation
                let _t_backprop = std::time::Instant::now();
                let mut curr = Some(node_idx);
                let batch_weight = 1.0;
                let batch_visits = 1;

                while let Some(idx) = curr {
                    let node_p_moved = self.nodes[idx].player_just_moved;
                    let node = &mut self.nodes[idx];
                    node.visit_count += batch_visits as u32;
                    if node_p_moved == 0 {
                        node.value_sum += reward_p0 * batch_weight;
                    } else {
                        node.value_sum += (1.0 - reward_p0) * batch_weight;
                    }
                    curr = node.parent;
                }
                sims_done += 1;
            }
        }

        let mut stats: Vec<(i32, f32, u32)> = self.nodes[0]
            .children
            .iter()
            .map(|&(act, idx)| {
                let child = &self.nodes[idx];
                (
                    act,
                    child.value_sum / child.visit_count as f32,
                    child.visit_count,
                )
            })
            .collect();

        stats.sort_by(|left, right| {
            let left_tiebreak = Self::root_action_tiebreak(root_state, db, left.0);
            let right_tiebreak = Self::root_action_tiebreak(root_state, db, right.0);
            right
                .2
                .cmp(&left.2)
                .then_with(|| right.1.partial_cmp(&left.1).unwrap_or(std::cmp::Ordering::Equal))
                .then_with(|| right_tiebreak.0.cmp(&left_tiebreak.0))
                .then_with(|| right_tiebreak.1.cmp(&left_tiebreak.1))
        });
        (stats, profiler)
    }

    fn select_child_logic(nodes: &[Node], node_idx: usize, exploration_weight: f32) -> usize {
        let node = &nodes[node_idx];
        let mut best_score = f32::NEG_INFINITY;
        let mut best_child = 0;

        let total_visits_sqrt = (node.visit_count as f32).sqrt();

        for &(_, child_idx) in &node.children {
            let child = &nodes[child_idx];

            // Average Value (Q). If no visits, evaluate slightly optimistically (0.5)
            let exploit = if child.visit_count > 0 {
                child.value_sum / child.visit_count as f32
            } else {
                0.5
            };

            // PUCT Exploration (AlphaZero)
            let explore = exploration_weight
                * child.prior_prob
                * (total_visits_sqrt / (1.0 + child.visit_count as f32));

            let score = exploit + explore;

            if score > best_score {
                best_score = score;
                best_child = child_idx;
            }
        }
        best_child
    }

    #[allow(dead_code)]
    fn select_child(&self, node_idx: usize) -> usize {
        Self::select_child_logic(&self.nodes, node_idx, self.exploration_weight)
    }

    // Evaluation functions moved to heuristics.rs
}

#[cfg(feature = "nn")]
pub struct HybridMCTS {
    pub session: Arc<Mutex<Session>>,
    pub neural_weight: f32,
    pub skip_rollout: bool,
    pub rng: SmallRng,
}

#[cfg(feature = "nn")]
impl HybridMCTS {
    pub fn new(session: Arc<Mutex<Session>>, neural_weight: f32, skip_rollout: bool) -> Self {
        Self {
            session,
            neural_weight,
            skip_rollout,
            rng: SmallRng::from_os_rng(),
        }
    }

    pub fn get_suggestions(
        &mut self,
        state: &GameState,
        db: &CardDatabase,
        num_sims: usize,
        timeout_sec: f32,
    ) -> Vec<(i32, f32, u32)> {
        let start = std::time::Instant::now();
        let (stats, profile) = self.search(state, db, num_sims, timeout_sec);
        let duration = start.elapsed();
        if num_sims > 10 {
            profile.print(duration);
        }
        stats
    }

    pub fn search(
        &mut self,
        root_state: &GameState,
        db: &CardDatabase,
        num_sims: usize,
        timeout_sec: f32,
    ) -> (Vec<(i32, f32, u32)>, MCTSProfiler) {
        let session_arc = self.session.clone();
        let neural_weight = self.neural_weight;
        let mut mcts = MCTS::new();

        mcts.run_mcts_config(
            root_state,
            db,
            num_sims,
            timeout_sec,
            SearchHorizon::GameEnd(),
            false,
            !self.skip_rollout,
            |state: &GameState, db: &CardDatabase| {
                if state.is_terminal() {
                    return match state.get_winner() {
                        0 => 1.0,
                        1 => 0.0,
                        _ => 0.5,
                    };
                }

                // Normal Heuristic Baseline
                use crate::core::heuristics::{EvalMode, OriginalHeuristic};
                let h = OriginalHeuristic::default();
                let h_val = h.evaluate(
                    state,
                    db,
                    root_state.players[0].score,
                    root_state.players[1].score,
                    EvalMode::Normal,
                    None,
                    None,
                );

                // NN Evaluation
                let input_vec = state.encode_state(db);
                let mut session = session_arc.lock().unwrap();

                let input_shape = [1, input_vec.len()];

                // Try to create input tensor and run using (shape, vec) which is version-agnostic
                if let Ok(input_tensor) = ort::value::Value::from_array((input_shape, input_vec)) {
                    if let Ok(outputs) = session.run(ort::inputs![input_tensor]) {
                        // Try to get value output. AlphaNet has 'output_1' or index 1
                        let val_opt = outputs.get("output_1").or_else(|| outputs.get("value"));

                        if let Some(v_val) = val_opt {
                            if let Ok((_, v_slice)) = v_val.try_extract_tensor::<f32>() {
                                let nn_val = v_slice[0];
                                let nn_norm = (nn_val * 0.5 + 0.5).clamp(0.0, 1.0) as f32;
                                return h_val * (1.0 - neural_weight) + nn_norm * neural_weight;
                            }
                        } else if outputs.len() > 1 {
                            // Fallback to index if names missing
                            if let Ok((_, v_slice)) = outputs[1].try_extract_tensor::<f32>() {
                                let nn_val = v_slice[0];
                                let nn_norm = (nn_val * 0.5 + 0.5).clamp(0.0, 1.0) as f32;
                                return h_val * (1.0 - neural_weight) + nn_norm * neural_weight;
                            }
                        }
                    }
                }

                h_val
            },
        )
    }
}