pybevy 0.2.1

PyBevy: A Python Real-Time Engine Built on Bevy
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
//! State system implementation for PyBevy
//!
//! Provides finite state machine functionality using Python Enums as state types.
//! States are app-wide resources that can be used to model game flow (Menu, InGame, Paused, etc.)
//!
//! # Features
//! - State<S> resource - holds current state
//! - NextState<S> resource - queue state transitions
//! - OnEnter/OnExit schedules - lifecycle hooks
//! - in_state() run condition - conditional system execution
//! - DespawnOnExit/DespawnOnEnter - automatic entity cleanup

use std::sync::{Arc, Mutex};

use bevy::ecs::schedule::ScheduleLabel;
use pyo3::{
    exceptions::{PyRuntimeError, PyTypeError, PyValueError},
    prelude::*,
    types::PyType,
};

use crate::ecs::{component::PyComponent, resource::PyResource};

/// Python decorator for marking an Enum as a state machine
///
/// # Example
/// ```python
/// from enum import Enum, auto
/// from pybevy import state
///
/// @state
/// class GameState(Enum):
///     MENU = auto()
///     IN_GAME = auto()
///     PAUSED = auto()
/// ```
#[pyfunction]
pub fn state(py: Python, cls: Bound<PyType>) -> PyResult<Py<PyType>> {
    // Validate: must be Enum subclass
    let enum_type = py.import("enum")?.getattr("Enum")?;
    if !cls.is_subclass(&enum_type)? {
        return Err(PyTypeError::new_err(
            "@state decorator can only be applied to Enum subclasses",
        ));
    }

    // Validate that enum has at least one variant
    let members_dict = cls.getattr("__members__")?;
    let values = members_dict.call_method0("values")?;
    let mut has_members = false;
    for _ in values.try_iter()? {
        has_members = true;
        break;
    }

    if !has_members {
        return Err(PyValueError::new_err(
            "State enum must have at least one variant",
        ));
    }

    // Mark as state type - metadata will be read directly from the class when needed
    cls.setattr("__pybevy_state__", true)?;

    Ok(cls.unbind())
}

/// State<S> resource - holds the current state value
///
/// Access via Res[State[GameState]] in systems
#[pyclass(name = "State", frozen, extends = PyResource)]
pub struct PyState {
    /// Current state value (Python enum member) - uses internal mutability for transitions
    current: Arc<Mutex<Py<PyAny>>>,
    /// Type of the state enum (for validation)
    state_type: Py<PyType>,
}

#[pymethods]
impl PyState {
    /// Create a new State resource
    #[new]
    fn py_new(py: Python, initial_state: Py<PyAny>) -> PyResult<(Self, PyResource)> {
        let state_type = initial_state.bind(py).get_type().unbind();

        // Validate it's a registered state type
        Self::validate_state_type(py, &state_type)?;

        Ok((
            PyState {
                current: Arc::new(Mutex::new(initial_state)),
                state_type,
            },
            PyResource,
        ))
    }

    /// Get the current state value
    fn get(&self, py: Python) -> Py<PyAny> {
        self.current.lock().unwrap().clone_ref(py)
    }

    /// Check if current state equals given state
    fn __eq__(&self, _py: Python, other: &Bound<PyAny>) -> PyResult<bool> {
        // CRITICAL: Cannot use PyO3 methods that require GIL (extract, bind, etc.)
        // because this is called from py.detach() context (app.rs:1192).

        // Check if comparing with the same State instance using raw pointer comparison
        let self_ptr = self as *const Self as *const ();
        let other_ptr = other.as_ptr() as *const ();

        // If comparing the exact same State object (self == other)
        if self_ptr == other_ptr {
            return Ok(true);
        }

        //Otherwise, we cannot safely extract or compare without GIL
        // Return false as a safe default (State comparisons should use 'is' not '==')
        Ok(false)
    }

    fn __repr__(&self, py: Python) -> PyResult<String> {
        let state_str = self.current.lock().unwrap().bind(py).repr()?.to_string();
        Ok(format!("State({})", state_str))
    }
}

impl PyState {
    /// Create a new State resource with given value
    pub fn new(py: Python, state_value: Py<PyAny>) -> PyResult<Py<Self>> {
        let state_type = state_value.bind(py).get_type().unbind();

        // Validate it's a registered state type
        Self::validate_state_type(py, &state_type)?;

        Py::new(
            py,
            (
                PyState {
                    current: Arc::new(Mutex::new(state_value)),
                    state_type,
                },
                PyResource,
            ),
        )
    }

    /// Internal helper to get current state value
    pub fn current_value(&self, py: Python) -> Py<PyAny> {
        self.current.lock().unwrap().clone_ref(py)
    }

    /// Update the state value (used internally during transitions)
    pub fn set_value(&self, new_value: Py<PyAny>) {
        *self.current.lock().unwrap() = new_value;
    }

    fn validate_state_type(py: Python, state_type: &Py<PyType>) -> PyResult<()> {
        let type_bound = state_type.bind(py);

        if !type_bound.hasattr("__pybevy_state__")? {
            return Err(PyTypeError::new_err(format!(
                "Type '{}' is not a valid state type. Did you forget the @state decorator?",
                type_bound.name()?
            )));
        }

        Ok(())
    }
}

/// NextState<S> resource - queue for pending state transitions
///
/// Use ResMut[NextState[GameState]] to queue transitions
#[pyclass(name = "NextState", frozen, extends = PyResource)]
pub struct PyNextState {
    /// Internal state: Unchanged or Pending(value)
    inner: Arc<Mutex<NextStateInner>>,
    /// Type of the state enum
    state_type: Py<PyType>,
    /// Whether the initial OnEnter for the starting state still needs to fire.
    /// Set to true on creation, cleared after the first transition system run.
    /// This matches Bevy's behavior where OnEnter fires for the initial state.
    initial_enter_pending: Arc<Mutex<bool>>,
}

enum NextStateInner {
    Unchanged,
    Pending(Py<PyAny>),
}

#[pymethods]
impl PyNextState {
    /// Queue a state transition
    ///
    /// The transition will be applied during the StateTransition schedule
    fn set(&self, py: Python, state: Py<PyAny>) -> PyResult<()> {
        // Validate state type matches
        let state_bound = state.bind(py);
        let provided_type = state_bound.get_type();

        if !provided_type.is(&self.state_type.bind(py)) {
            return Err(PyTypeError::new_err(format!(
                "State type mismatch: expected {}, got {}",
                self.state_type.bind(py).name()?,
                provided_type.name()?
            )));
        }

        *self.inner.lock().unwrap() = NextStateInner::Pending(state);
        Ok(())
    }

    /// Cancel any pending transition
    fn reset(&self) -> PyResult<()> {
        *self.inner.lock().unwrap() = NextStateInner::Unchanged;
        Ok(())
    }

    /// Check if a transition is pending
    fn is_pending(&self) -> bool {
        matches!(*self.inner.lock().unwrap(), NextStateInner::Pending(_))
    }

    /// Get the pending state without consuming it (for inspection)
    fn peek_pending(&self, py: Python) -> Option<Py<PyAny>> {
        match &*self.inner.lock().unwrap() {
            NextStateInner::Pending(state) => Some(state.clone_ref(py)),
            NextStateInner::Unchanged => None,
        }
    }

    fn __repr__(&self, py: Python) -> PyResult<String> {
        let inner = self.inner.lock().unwrap();
        match &*inner {
            NextStateInner::Unchanged => Ok("NextState(Unchanged)".to_string()),
            NextStateInner::Pending(state) => {
                let state_str = state.bind(py).repr()?.to_string();
                Ok(format!("NextState(Pending({}))", state_str))
            }
        }
    }
}

impl PyNextState {
    /// Create a new NextState resource (starts as Unchanged)
    pub fn new(py: Python, state_type: Py<PyType>) -> PyResult<Py<Self>> {
        PyState::validate_state_type(py, &state_type)?;

        Py::new(
            py,
            (
                PyNextState {
                    inner: Arc::new(Mutex::new(NextStateInner::Unchanged)),
                    state_type,
                    initial_enter_pending: Arc::new(Mutex::new(true)),
                },
                PyResource,
            ),
        )
    }

    /// Take the pending state if any (used internally during transitions)
    pub fn take_pending(&self) -> Option<Py<PyAny>> {
        let mut inner = self.inner.lock().unwrap();
        match std::mem::replace(&mut *inner, NextStateInner::Unchanged) {
            NextStateInner::Pending(state) => Some(state),
            NextStateInner::Unchanged => None,
        }
    }

    /// Check and clear the initial enter pending flag.
    /// Returns true if the initial OnEnter still needs to fire.
    pub fn take_initial_enter_pending(&self) -> bool {
        let mut pending = self.initial_enter_pending.lock().unwrap();
        if *pending {
            *pending = false;
            true
        } else {
            false
        }
    }
}

/// Schedule label for systems that run when entering a state
#[pyclass(name = "OnEnterSchedule", frozen)]
pub struct PyOnEnterSchedule {
    state_value: Py<PyAny>,
}

#[pymethods]
impl PyOnEnterSchedule {
    fn __repr__(&self, py: Python) -> PyResult<String> {
        let state_str = self.state_value.bind(py).repr()?.to_string();
        Ok(format!("OnEnter({})", state_str))
    }

    fn __hash__(&self, py: Python) -> PyResult<u64> {
        Ok(self.state_value.bind(py).hash()? as u64)
    }

    fn __eq__(&self, py: Python, other: &Bound<PyAny>) -> PyResult<bool> {
        // CRITICAL: Use 'is' semantics (pointer equality) instead of Python's __eq__
        // to avoid deadlock when called from systems in py.detach() context (app.rs:1192)
        if let Ok(other_schedule) = other.extract::<PyRef<Self>>() {
            Ok(self
                .state_value
                .bind(py)
                .is(&other_schedule.state_value.bind(py)))
        } else {
            Ok(false)
        }
    }
}

/// Schedule label for systems that run when exiting a state
#[pyclass(name = "OnExitSchedule", frozen)]
pub struct PyOnExitSchedule {
    state_value: Py<PyAny>,
}

#[pymethods]
impl PyOnExitSchedule {
    fn __repr__(&self, py: Python) -> PyResult<String> {
        let state_str = self.state_value.bind(py).repr()?.to_string();
        Ok(format!("OnExit({})", state_str))
    }

    fn __hash__(&self, py: Python) -> PyResult<u64> {
        Ok(self.state_value.bind(py).hash()? as u64)
    }

    fn __eq__(&self, py: Python, other: &Bound<PyAny>) -> PyResult<bool> {
        // CRITICAL: Use 'is' semantics (pointer equality) instead of Python's __eq__
        // to avoid deadlock when called from systems in py.detach() context (app.rs:1192)
        if let Ok(other_schedule) = other.extract::<PyRef<Self>>() {
            Ok(self
                .state_value
                .bind(py)
                .is(&other_schedule.state_value.bind(py)))
        } else {
            Ok(false)
        }
    }
}

/// Schedule label for systems that run during state transitions
#[pyclass(name = "OnTransitionSchedule", frozen)]
pub struct PyOnTransitionSchedule {
    exited: Py<PyAny>,
    entered: Py<PyAny>,
}

#[pymethods]
impl PyOnTransitionSchedule {
    fn __repr__(&self, py: Python) -> PyResult<String> {
        let exited_str = self.exited.bind(py).repr()?.to_string();
        let entered_str = self.entered.bind(py).repr()?.to_string();
        Ok(format!("OnTransition({} -> {})", exited_str, entered_str))
    }

    fn __hash__(&self, py: Python) -> PyResult<u64> {
        // Combine hashes of both states
        let hash1 = self.exited.bind(py).hash()? as u64;
        let hash2 = self.entered.bind(py).hash()? as u64;
        Ok(hash1.wrapping_mul(31).wrapping_add(hash2))
    }

    fn __eq__(&self, py: Python, other: &Bound<PyAny>) -> PyResult<bool> {
        // CRITICAL: Use 'is' semantics (pointer equality) instead of Python's __eq__
        // to avoid deadlock when called from systems in py.detach() context (app.rs:1192)
        if let Ok(other_schedule) = other.extract::<PyRef<Self>>() {
            let exited_eq = self.exited.bind(py).is(&other_schedule.exited.bind(py));
            let entered_eq = self.entered.bind(py).is(&other_schedule.entered.bind(py));
            Ok(exited_eq && entered_eq)
        } else {
            Ok(false)
        }
    }
}

/// Bevy schedule labels for state transitions
/// These implement Bevy's ScheduleLabel trait to integrate with the schedule system

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum ScheduleKind {
    Enter,
    Exit,
}

/// Rust-side schedule label for OnEnter/OnExit schedules
/// Uses hash-based approach for simplicity (as recommended in design doc)
#[derive(ScheduleLabel, Debug, Clone, PartialEq, Eq, Hash)]
pub struct StateScheduleLabel {
    kind: ScheduleKind,
    state_hash: u64,
}

impl StateScheduleLabel {
    pub fn on_enter(state_hash: u64) -> Self {
        Self {
            kind: ScheduleKind::Enter,
            state_hash,
        }
    }

    pub fn on_exit(state_hash: u64) -> Self {
        Self {
            kind: ScheduleKind::Exit,
            state_hash,
        }
    }
}

/// Transition schedule label (for OnTransition)
#[derive(ScheduleLabel, Debug, Clone, PartialEq, Eq, Hash)]
pub struct TransitionScheduleLabel {
    exit_hash: u64,
    enter_hash: u64,
}

impl TransitionScheduleLabel {
    pub fn new(exit_hash: u64, enter_hash: u64) -> Self {
        Self {
            exit_hash,
            enter_hash,
        }
    }
}

/// Helper methods for Python schedule types to get their Bevy labels
impl PyOnEnterSchedule {
    pub fn to_bevy_label(&self, py: Python) -> PyResult<StateScheduleLabel> {
        let hash = self.state_value.bind(py).hash()? as u64;
        Ok(StateScheduleLabel::on_enter(hash))
    }
}

impl PyOnExitSchedule {
    pub fn to_bevy_label(&self, py: Python) -> PyResult<StateScheduleLabel> {
        let hash = self.state_value.bind(py).hash()? as u64;
        Ok(StateScheduleLabel::on_exit(hash))
    }
}

impl PyOnTransitionSchedule {
    pub fn to_bevy_label(&self, py: Python) -> PyResult<TransitionScheduleLabel> {
        let exit_hash = self.exited.bind(py).hash()? as u64;
        let enter_hash = self.entered.bind(py).hash()? as u64;
        Ok(TransitionScheduleLabel::new(exit_hash, enter_hash))
    }
}

/// Create an OnEnter schedule label
///
/// # Example
/// ```python
/// app.add_systems(OnEnter(GameState.MENU), setup_menu)
/// ```
#[pyfunction]
#[pyo3(name = "OnEnter")]
pub fn on_enter(py: Python, state: Py<PyAny>) -> PyResult<Py<PyOnEnterSchedule>> {
    // Validate state type
    let state_type = state.bind(py).get_type().unbind();
    PyState::validate_state_type(py, &state_type)?;

    Py::new(py, PyOnEnterSchedule { state_value: state })
}

/// Create an OnExit schedule label
///
/// # Example
/// ```python
/// app.add_systems(OnExit(GameState.MENU), cleanup_menu)
/// ```
#[pyfunction]
#[pyo3(name = "OnExit")]
pub fn on_exit(py: Python, state: Py<PyAny>) -> PyResult<Py<PyOnExitSchedule>> {
    // Validate state type
    let state_type = state.bind(py).get_type().unbind();
    PyState::validate_state_type(py, &state_type)?;

    Py::new(py, PyOnExitSchedule { state_value: state })
}

/// Create an OnTransition schedule label
///
/// # Example
/// ```python
/// app.add_systems(OnTransition(GameState.MENU, GameState.IN_GAME), transition_effect)
/// ```
#[pyfunction]
#[pyo3(name = "OnTransition")]
pub fn on_transition(
    py: Python,
    exited: Py<PyAny>,
    entered: Py<PyAny>,
) -> PyResult<Py<PyOnTransitionSchedule>> {
    // Validate both states are same type
    let exited_type = exited.bind(py).get_type();
    let entered_type = entered.bind(py).get_type();

    if !exited_type.is(&entered_type) {
        return Err(PyTypeError::new_err(
            "OnTransition requires both states to be the same type",
        ));
    }

    PyState::validate_state_type(py, &exited_type.unbind())?;

    Py::new(py, PyOnTransitionSchedule { exited, entered })
}

/// Component for entities that should despawn when exiting a state
///
/// # Example
/// ```python
/// commands.spawn((
///     Player(),
///     DespawnOnExit(GameState.IN_GAME)
/// ))
/// ```
#[pyclass(name = "DespawnOnExit", frozen, extends = PyComponent)]
pub struct PyDespawnOnExit {
    state_value: Py<PyAny>,
}

#[pymethods]
impl PyDespawnOnExit {
    #[new]
    fn new(py: Python, state: Py<PyAny>) -> PyResult<(Self, PyComponent)> {
        // Validate state type
        let state_type = state.bind(py).get_type().unbind();
        PyState::validate_state_type(py, &state_type)?;

        Ok((PyDespawnOnExit { state_value: state }, PyComponent))
    }

    fn __repr__(&self, py: Python) -> PyResult<String> {
        let state_str = self.state_value.bind(py).repr()?.to_string();
        Ok(format!("DespawnOnExit({})", state_str))
    }

    /// Get the state value this component is associated with
    fn state_value(&self, py: Python) -> Py<PyAny> {
        self.state_value.clone_ref(py)
    }
}

/// Component for entities that should despawn when entering a state
///
/// # Example
/// ```python
/// commands.spawn((
///     MenuUI(),
///     DespawnOnEnter(GameState.IN_GAME)
/// ))
/// ```
#[pyclass(name = "DespawnOnEnter", frozen, extends = PyComponent)]
pub struct PyDespawnOnEnter {
    state_value: Py<PyAny>,
}

#[pymethods]
impl PyDespawnOnEnter {
    #[new]
    fn new(py: Python, state: Py<PyAny>) -> PyResult<(Self, PyComponent)> {
        // Validate state type
        let state_type = state.bind(py).get_type().unbind();
        PyState::validate_state_type(py, &state_type)?;

        Ok((PyDespawnOnEnter { state_value: state }, PyComponent))
    }

    fn __repr__(&self, py: Python) -> PyResult<String> {
        let state_str = self.state_value.bind(py).repr()?.to_string();
        Ok(format!("DespawnOnEnter({})", state_str))
    }

    /// Get the state value this component is associated with
    fn state_value(&self, py: Python) -> Py<PyAny> {
        self.state_value.clone_ref(py)
    }
}

/// Run condition that checks if current state matches given state
///
/// # Example
/// ```python
/// app.add_systems(Update, menu_system.run_if(in_state(GameState.MENU)))
/// ```
#[pyfunction]
pub fn in_state(py: Python, state: Py<PyAny>) -> PyResult<Py<PyAny>> {
    // Validate that the state is from a @state decorated enum
    let state_type = state.bind(py).get_type().unbind();
    PyState::validate_state_type(py, &state_type)?;

    // Create a Python function that checks if current State == state
    // The function will have signature: (current: Res[State]) -> bool
    use std::ffi::CString;

    use pyo3::types::PyDict;

    // Create globals dict with required imports
    let globals = PyDict::new(py);
    let ecs_module = py.import("pybevy.ecs")?;
    globals.set_item("Res", ecs_module.getattr("Res")?)?;
    globals.set_item("State", ecs_module.getattr("State")?)?;

    let locals = PyDict::new(py);
    locals.set_item("target_state", state)?;

    let code = CString::new(
        r#"
def _make_in_state_condition(target):
    """Factory that creates a condition checking for a specific state."""
    def in_state_condition(current: Res[State]) -> bool:
        """Check if current State matches the target state."""
        return current.get() == target
    # Set a meaningful name for debugging
    in_state_condition.__name__ = f"in_state({target})"
    return in_state_condition

result = _make_in_state_condition(target_state)
"#,
    )?;

    py.run(&code, Some(&globals), Some(&locals))?;
    let condition = locals
        .get_item("result")?
        .ok_or_else(|| PyRuntimeError::new_err("Failed to create in_state condition"))?;

    Ok(condition.unbind())
}

/// Apply pending state transitions for all registered state types
/// System function that processes state transitions for a specific state type
///
/// This is called automatically in the StateTransition schedule to handle
/// NextState -> State updates and trigger OnExit/OnEnter schedules.
///
/// Returns true if a transition occurred, false otherwise.
fn apply_transition_for_state(
    py: Python,
    world: &mut bevy::ecs::world::World,
    state_type_name: &str,
) -> PyResult<bool> {
    use bevy::ecs::schedule::Schedules;

    use crate::ecs::resource_type::PyResourceStorage;

    // Get the NextState resource for this state type
    // We need to find it by matching the Python type name stored in the resource
    let mut next_state_opt: Option<Py<PyNextState>> = None;
    let mut state_opt: Option<Py<PyState>> = None;

    // Scan resources to find State<T> and NextState<T> for this type
    {
        let resource_storage = world.resource::<PyResourceStorage>();

        for (_, resource_py) in resource_storage.resources.iter() {
            let resource = resource_py.bind(py);

            // Check if this is NextState for our type
            if let Ok(next_state) = resource.cast::<PyNextState>() {
                let type_name = next_state
                    .borrow()
                    .state_type
                    .bind(py)
                    .name()
                    .unwrap()
                    .to_string();
                if type_name == state_type_name {
                    next_state_opt = Some(
                        resource_py
                            .clone_ref(py)
                            .extract::<Py<PyNextState>>(py)
                            .unwrap(),
                    );
                }
            }

            // Check if this is State for our type
            if let Ok(state) = resource.cast::<PyState>() {
                let type_name = state
                    .borrow()
                    .state_type
                    .bind(py)
                    .name()
                    .unwrap()
                    .to_string();
                if type_name == state_type_name {
                    state_opt = Some(
                        resource_py
                            .clone_ref(py)
                            .extract::<Py<PyState>>(py)
                            .unwrap(),
                    );
                }
            }
        }
    }

    let next_state_py = match next_state_opt {
        Some(ns) => ns,
        None => return Ok(false), // No NextState resource for this type
    };

    let state_py = match state_opt {
        Some(s) => s,
        None => return Ok(false), // No State resource for this type
    };

    // Check if there's a pending transition and take it
    let next_state_borrow = next_state_py.bind(py).borrow();
    let pending_transition = next_state_borrow.take_pending();
    let initial_enter =
        pending_transition.is_none() && next_state_borrow.take_initial_enter_pending();
    drop(next_state_borrow); // Drop borrow

    // If this is the initial enter (no explicit transition queued), just fire OnEnter
    // for the current state. This matches Bevy's behavior where OnEnter fires for
    // the state set via insert_state() on the first frame.
    if initial_enter {
        let current_state = {
            let state = state_py.bind(py).borrow();
            state.current_value(py)
        };
        let hash = current_state.bind(py).hash()? as u64;
        let enter_label = StateScheduleLabel::on_enter(hash);
        let has_enter_schedule = world.resource::<Schedules>().contains(enter_label.clone());
        if has_enter_schedule {
            world.try_run_schedule(enter_label).ok();
        }
        return Ok(true);
    }

    let pending_transition = match pending_transition {
        Some(new_state) => new_state,
        None => return Ok(false), // No pending transition
    };

    // Get current state
    let current_state = {
        let state = state_py.bind(py).borrow();
        state.current_value(py)
    };

    // Get hash values for schedule lookup
    let old_hash = current_state.bind(py).hash()? as u64;
    let new_hash = pending_transition.bind(py).hash()? as u64;

    // Run OnExit(old_state) schedule
    let exit_label = StateScheduleLabel::on_exit(old_hash);
    let has_exit_schedule = world.resource::<Schedules>().contains(exit_label.clone());
    if has_exit_schedule {
        world.try_run_schedule(exit_label).ok();
    }

    // Update State<T> resource
    {
        let state = state_py.bind(py).borrow();
        state.set_value(pending_transition.clone_ref(py));
    }

    // Run OnTransition(old_state -> new_state) schedule
    let transition_label = TransitionScheduleLabel::new(old_hash, new_hash);
    let has_transition_schedule = world
        .resource::<Schedules>()
        .contains(transition_label.clone());
    if has_transition_schedule {
        world.try_run_schedule(transition_label).ok();
    }

    // Run OnEnter(new_state) schedule
    let enter_label = StateScheduleLabel::on_enter(new_hash);
    let has_enter_schedule = world.resource::<Schedules>().contains(enter_label.clone());
    if has_enter_schedule {
        world.try_run_schedule(enter_label).ok();
    }

    Ok(true)
}

/// System that checks all registered state types and applies pending transitions.
///
/// This is automatically added to the PreUpdate schedule when
/// init_state() or insert_state() is called. It runs OnExit/OnTransition/OnEnter
/// schedules when a pending transition is found.
///
/// State types are discovered dynamically by scanning the world's PyResourceStorage
/// for State<T> and NextState<T> resources.
pub fn apply_state_transitions(py: Python, world: &mut bevy::ecs::world::World) -> PyResult<()> {
    use std::{
        collections::HashSet,
        sync::atomic::{AtomicBool, Ordering},
    };

    use crate::ecs::resource_type::PyResourceStorage;

    // Guard against re-entrant calls (world.run_schedule for OnEnter/OnExit
    // can trigger the parent schedule to re-run)
    static PROCESSING: AtomicBool = AtomicBool::new(false);
    if PROCESSING.swap(true, Ordering::SeqCst) {
        return Ok(()); // Already processing, skip
    }
    struct ProcessingGuard;
    impl Drop for ProcessingGuard {
        fn drop(&mut self) {
            PROCESSING.store(false, Ordering::SeqCst);
        }
    }
    let _guard = ProcessingGuard;

    // Discover state type names by scanning PyResourceStorage for NextState<T> resources
    let mut state_type_names = HashSet::new();
    {
        let resource_storage = world.resource::<PyResourceStorage>();
        for (_, resource_py) in resource_storage.resources.iter() {
            let resource = resource_py.bind(py);
            // Check if this is NextState for any type
            if let Ok(next_state) = resource.cast::<PyNextState>() {
                let type_name = next_state
                    .borrow()
                    .state_type
                    .bind(py)
                    .name()
                    .unwrap()
                    .to_string();
                state_type_names.insert(type_name);
            }
        }
    }

    // Apply transitions for each discovered state type
    for type_name in state_type_names {
        apply_transition_for_state(py, world, &type_name)?;
    }

    Ok(())
}