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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
use std::collections::HashMap;

use crate::ability::resolver::AbilityResolver;
use crate::ability::types::Choice;
use crate::card::{Ability, AbilityEffect};
use crate::game_state::AbilityTrigger;

/// Unique identifier for an ability instance in the queue
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AbilityId(pub String);

impl AbilityId {
    pub fn new(card_no: &str, ability_index: usize, trigger_type: &str) -> Self {
        AbilityId(format!("{}_{}_{}", card_no, ability_index, trigger_type))
    }
}

/// Current state of ability queue processing
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueueState {
    /// Queue is idle, ready to process next ability
    Idle,
    /// Waiting for player to choose which auto ability resolves first (Rule 9.5.3)
    WaitingForAutoAbilityChoice { choice: Choice },
    /// Currently paying cost for an ability
    PayingCost { entry_index: usize },
    /// Waiting for user choice (cost payment, target selection, etc.)
    WaitingForChoice { entry_index: usize, choice: Choice },
    /// Executing the effect of an ability
    ExecutingEffect { entry_index: usize },
    /// Ability completed, will transition to Idle
    Completed { entry_index: usize },
}

/// Entry in the ability queue
#[derive(Debug, Clone)]
pub struct AbilityQueueEntry {
    pub id: AbilityId,
    pub card_no: String,
    pub player_id: String,
    pub ability: Ability,
    pub ability_index: usize,
    pub card_id: Option<i16>,
    pub trigger_type: AbilityTrigger,
    /// Whether this ability has been completed
    pub completed: bool,
    /// Whether the cost has been fully paid (for re-entry after cost choice)
    pub cost_paid: bool,
    /// Stored choice result for resumption
    pub cost_paid_index: usize,
    pub pending_choice_result: Option<crate::ability::types::ChoiceResult>,
    /// Discriminator for routing choice results to the correct handler.
    pub choice_card_no: Option<crate::ability::types::ChoiceRoute>,
    /// JSON-serialized options for choice/choice_string discriminators
    pub conditional_choice: Option<String>,
    /// Whether the effect has started executing (prevents re-processing)
    pub effect_started: bool,
    /// Result of the optional cost evaluation.
    /// None = not evaluated, Some(true) = paid, Some(false) = skipped.
    pub optional_cost_result: Option<bool>,
    /// Player who must make the pending choice (if different from activating player)
    pub choice_player_id: Option<String>,
    /// Card IDs that triggered this each_time auto ability (snapshot of
    /// recently_moved_cards at enqueue time). Used by source:"those_cards"
    /// to resolve to only the trigger cards, not the full discard pile.
    pub trigger_moved_cards: Option<Vec<i16>>,
    /// Snapshot of batch_movements at enqueue time so the "moves" and energy
    /// conditions can see what triggered the ability even after
    /// clear_effect_tracking() clears the global batch_movements.
    pub snapshot_movements: Vec<crate::types::MovementEvent>,
    pub snapshot_energy_placed_by_effect: bool,
    pub snapshot_energy_placed_by_player: Option<String>,
    /// Actions queued for sequential execution after a choice round-trip.
    pub pending_actions: Vec<AbilityEffect>,
    /// Persistent ability resolver — stays alive across choice round-trips
    /// instead of being destroyed and recreated. Eliminates manual save/restore.
    pub resolver: Option<AbilityResolver>,
    /// For each_time triggers: the stage member card ID whose resolution
    /// caused this each_time ability to fire. Used by effects like
    /// "gain all-heart" to target the correct member.
    pub triggering_member_id: Option<i16>,
    /// The original effect text of a choice-type ability, stored so the frontend
    /// can use it as the choice prompt (separate from the option labels).
    pub choice_effect_text: Option<String>,
    /// Cache for condition results that should be evaluated once and reused.
    /// Keyed by condition text. Populated by the first evaluation; subsequent
    /// evaluations return the cached value. The parser sets `"cache": true` on
    /// conditions that depend on mutable state (e.g. revealed_cards).
    pub condition_cache: HashMap<String, bool>,
}

/// Unified ability queue with proper state management
#[derive(Debug, Clone)]
pub struct AbilityQueue {
    entries: Vec<AbilityQueueEntry>,
    state: QueueState,
    current_index: usize,
    /// Set by the resolver when a new pending choice is about to be stored.
    /// The web handler checks this after execute_main_phase_action to decide
    /// whether to push an additional history snapshot at the choice boundary.
    pub snapshot_requested: bool,
}

impl AbilityQueue {
    pub fn new() -> Self {
        AbilityQueue {
            entries: Vec::new(),
            state: QueueState::Idle,
            current_index: 0,
            snapshot_requested: false,
        }
    }

    /// Check if queue is idle (no ability being processed)
    pub fn is_idle(&self) -> bool {
        matches!(self.state, QueueState::Idle)
    }

    /// Check if queue is waiting for user choice
    pub fn is_waiting_for_choice(&self) -> Option<&Choice> {
        match &self.state {
            QueueState::WaitingForAutoAbilityChoice { choice } => Some(choice),
            QueueState::WaitingForChoice { choice, .. } => Some(choice),
            _ => None,
        }
    }

    /// Get current ability being processed
    pub fn current_entry(&self) -> Option<&AbilityQueueEntry> {
        match &self.state {
            QueueState::PayingCost { entry_index }
            | QueueState::WaitingForChoice { entry_index, .. }
            | QueueState::ExecutingEffect { entry_index }
            | QueueState::Completed { entry_index } => self.entries.get(*entry_index),
            QueueState::Idle | QueueState::WaitingForAutoAbilityChoice { .. } => None,
        }
    }

    pub fn current_entry_mut(&mut self) -> Option<&mut AbilityQueueEntry> {
        let idx = match &self.state {
            QueueState::PayingCost { entry_index }
            | QueueState::WaitingForChoice { entry_index, .. }
            | QueueState::ExecutingEffect { entry_index }
            | QueueState::Completed { entry_index } => *entry_index,
            QueueState::Idle | QueueState::WaitingForAutoAbilityChoice { .. } => return None,
        };
        self.entries.get_mut(idx)
    }

    pub fn get_entry(&self, index: usize) -> Option<&AbilityQueueEntry> {
        self.entries.get(index)
    }

    /// Number of entries pending or completed
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Iterate over entries.
    pub fn iter(&self) -> impl Iterator<Item = &AbilityQueueEntry> {
        self.entries.iter()
    }

    /// Check if an entry with this ID already exists (completed or not)
    pub fn has_entry_with_id(&self, id: &AbilityId) -> bool {
        self.entries.iter().any(|e| e.id == *id)
    }

    /// Add ability to queue
    pub fn enqueue(&mut self, entry: AbilityQueueEntry) {
        self.entries.push(entry);
    }

    /// Start processing next ability in queue
    /// Returns true if an ability was started, false if queue is empty
    pub fn start_next(&mut self) -> bool {
        // Don't start new ability if currently processing one
        if !matches!(self.state, QueueState::Idle) {
            return false;
        }

        // Find next uncompleted entry
        while self.current_index < self.entries.len() {
            let entry = &self.entries[self.current_index];
            if !entry.completed {
                self.state = QueueState::PayingCost {
                    entry_index: self.current_index,
                };
                return true;
            }
            self.current_index += 1;
        }
        false
    }

    /// Pause for user choice during ability execution
    pub fn pause_for_choice(&mut self, choice: Choice) {
        let choice_clone = choice.clone();
        match &mut self.state {
            QueueState::PayingCost { entry_index }
            | QueueState::ExecutingEffect { entry_index } => {
                let idx = *entry_index;
                // G1/G3: route choice to opponent when spawn context says opponent
                let opponent_id: Option<String> = self.entries.get(idx).and_then(|entry| {
                    if entry.choice_player_id.is_some() {
                        return None;
                    }
                    let is_opponent_choice = match &choice {
                        crate::ability::types::Choice::SelectCard {
                            target_player_id: Some(tpid),
                            ..
                        } if tpid == "opponent" => true,
                        _ => false,
                    };
                    if !is_opponent_choice {
                        return None;
                    }
                    let is_spawn_opponent = entry
                        .resolver
                        .as_ref()
                        .and_then(|r| r.spawn_context.target.as_deref())
                        == Some("opponent");
                    if !is_spawn_opponent {
                        return None;
                    }
                    let opp = if entry.player_id == "p1" { "p2" } else { "p1" };
                    Some(opp.to_string())
                });
                if let Some(pid) = opponent_id {
                    if let Some(entry) = self.entries.get_mut(idx) {
                        entry.choice_player_id = Some(pid);
                    }
                }
                // Universal default: every paused choice gets a choice_player_id
                if let Some(entry) = self.entries.get(idx) {
                    if entry.choice_player_id.is_none() {
                        if let Some(entry) = self.entries.get_mut(idx) {
                            entry.choice_player_id = Some(entry.player_id.clone());
                        }
                    }
                }
                self.state = QueueState::WaitingForChoice {
                    entry_index: idx,
                    choice: choice_clone,
                };
            }
            QueueState::Idle | QueueState::Completed { .. } => {
                // Store the choice directly without an entry
                let dummy_entry = AbilityQueueEntry {
                    id: AbilityId::new("", 0, "choice"),
                    card_no: String::new(),
                    player_id: String::new(),
                    ability: Ability {
                        full_text: String::new(),
                        triggerless_text: String::new(),
                        triggers: None,
                        use_limit: None,
                        is_null: false,
                        cost: None,
                        effect: None,
                        keywords: None,
                    },
                    ability_index: 0,
                    card_id: None,
                    trigger_type: AbilityTrigger::Auto,
                    completed: false,
                    cost_paid: false,
                    cost_paid_index: 0,
                    pending_choice_result: None,
                    choice_card_no: None,
                    conditional_choice: None,
                    effect_started: false,
                    optional_cost_result: None,
                    choice_player_id: None,
                    pending_actions: Vec::new(),
                    resolver: None,
                    trigger_moved_cards: None,
                    triggering_member_id: None,
                    snapshot_movements: Vec::new(),
                    snapshot_energy_placed_by_effect: false,
                    snapshot_energy_placed_by_player: None,
                    choice_effect_text: None,
                    condition_cache: HashMap::new(),
                };
                self.entries.push(dummy_entry);
                self.state = QueueState::WaitingForChoice {
                    entry_index: self.entries.len() - 1,
                    choice: choice_clone,
                };
            }
            QueueState::WaitingForChoice { .. }
            | QueueState::WaitingForAutoAbilityChoice { .. } => {}
        }
    }

    pub fn pause_for_auto_ability_choice(&mut self, choice: Choice) {
        self.snapshot_requested = true;
        self.state = QueueState::WaitingForAutoAbilityChoice { choice };
    }

    /// Resume after user provides choice result
    pub fn resume_with_choice(&mut self, result: crate::ability::types::ChoiceResult) {
        match &self.state {
            QueueState::WaitingForAutoAbilityChoice { .. } => {
                self.state = QueueState::Idle;
            }
            QueueState::WaitingForChoice { entry_index, .. } => {
                if let Some(entry) = self.entries.get_mut(*entry_index) {
                    entry.pending_choice_result = Some(result);
                }
                self.state = QueueState::ExecutingEffect {
                    entry_index: *entry_index,
                };
            }
            _ => {}
        }
    }

    /// Mark current ability as completed and move to idle state
    pub fn complete_current(&mut self) {
        match &self.state {
            QueueState::PayingCost { entry_index }
            | QueueState::WaitingForChoice { entry_index, .. }
            | QueueState::ExecutingEffect { entry_index }
            | QueueState::Completed { entry_index } => {
                if let Some(entry) = self.entries.get_mut(*entry_index) {
                    entry.completed = true;
                }
            }
            _ => {}
        }
        self.state = QueueState::Idle;
        self.current_index += 1;
    }

    /// Skip remaining abilities for a specific card (e.g., after optional cost skip)
    /// Clear completed entries to free memory
    pub fn clear_completed(&mut self) {
        self.entries.retain(|e| !e.completed);
        if self.current_index > self.entries.len() {
            self.current_index = 0;
        }
    }

    /// Push a temporary context entry for constant ability evaluation.
    /// This gives resolve_target_player (which reads ability_master_id)
    /// the correct player context when evaluating success zone constants
    /// outside the normal ability queue flow.
    pub fn push_constant_context(&mut self, player_id: String) {
        let idx = self.entries.len();
        self.entries.push(AbilityQueueEntry {
            id: AbilityId::new("", 0, "const_eval"),
            card_no: String::new(),
            player_id,
            ability: Ability {
                full_text: String::new(),
                triggerless_text: String::new(),
                triggers: None,
                use_limit: None,
                is_null: true,
                cost: None,
                effect: None,
                keywords: None,
            },
            ability_index: 0,
            card_id: None,
            trigger_type: AbilityTrigger::Auto,
            completed: false,
            cost_paid: false,
            cost_paid_index: 0,
            pending_choice_result: None,
            choice_card_no: None,
            conditional_choice: None,
            effect_started: false,
            optional_cost_result: None,
            choice_player_id: None,
            pending_actions: Vec::new(),
            resolver: None,
            trigger_moved_cards: None,
            triggering_member_id: None,
            snapshot_movements: Vec::new(),
            snapshot_energy_placed_by_effect: false,
            snapshot_energy_placed_by_player: None,
            choice_effect_text: None,
            condition_cache: HashMap::new(),
        });
        self.state = QueueState::ExecutingEffect { entry_index: idx };
    }

    /// Pop the temporary constant evaluation context and restore idle.
    pub fn pop_constant_context(&mut self) {
        self.entries.pop();
        self.state = QueueState::Idle;
    }

    /// Get queue state for debugging
    pub fn get_state(&self) -> &QueueState {
        &self.state
    }

    /// Get all pending entries
    pub fn pending_entries(&self) -> Vec<&AbilityQueueEntry> {
        self.entries.iter().filter(|e| !e.completed).collect()
    }

    /// Store deferred sequential commands on the current entry.
    /// Replace pending actions on the current entry.
    pub fn set_pending_actions(&mut self, actions: Vec<AbilityEffect>) {
        if let Some(entry) = self.current_entry_mut() {
            entry.pending_actions = actions;
        }
    }

    /// Append actions to the existing pending actions.
    pub fn save_pending_actions(&mut self, actions: Vec<AbilityEffect>) {
        if actions.is_empty() {
            return;
        }
        if let Some(entry) = self.current_entry_mut() {
            entry.pending_actions.extend(actions);
        }
    }

    /// Drain and return pending actions from the current entry.
    pub fn take_pending_actions(&mut self) -> Vec<AbilityEffect> {
        if let Some(entry) = self.current_entry_mut() {
            std::mem::take(&mut entry.pending_actions)
        } else {
            Vec::new()
        }
    }

    /// Check if the current entry has pending actions.
    pub fn has_pending_actions(&self) -> bool {
        self.current_entry()
            .is_some_and(|e| !e.pending_actions.is_empty())
    }

    /// Move the entry at `from_index` to the front of the queue (position 0).
    /// Used by Rule 9.5.3 auto-ability ordering: player picks which standby ability
    /// resolves first, and this moves it to the head of the queue.
    pub fn promote_entry(&mut self, from_index: usize) {
        let absolute = self.current_index + from_index;
        if absolute >= self.entries.len() || from_index == 0 {
            return;
        }
        let entry = self.entries.remove(absolute);
        self.entries.insert(0, entry);
        if self.current_index > absolute {
            self.current_index = self.current_index.saturating_sub(1);
        } else {
            self.current_index = 0;
        }
    }
    /// Check if an entry exists and is not completed.
    pub fn is_entry_available(&self, index: usize) -> bool {
        index < self.entries.len() && !self.entries[index].completed
    }

    /// Get the player_id of an entry.
    pub fn entry_player_id(&self, index: usize) -> Option<&str> {
        self.entries.get(index).map(|e| e.player_id.as_str())
    }

    /// Move an entry at an absolute index to position 0 and reset current_index.
    /// Unlike promote_entry which uses relative-from-current_index, this uses
    /// the direct absolute index in the entries array.
    pub fn promote_entry_by_abs(&mut self, absolute: usize) {
        if absolute >= self.entries.len() || absolute == 0 {
            if absolute == 0 {
                self.current_index = 0;
            }
            return;
        }
        let entry = self.entries.remove(absolute);
        self.entries.insert(0, entry);
        self.current_index = 0;
    }

    /// Set the current index to a specific entry without reordering the queue
    /// (unlike promote_entry_by_abs which removes/inserts). Used by depth-first
    /// drain loops to process newly-queued entries in-place.
    pub fn set_current_entry(&mut self, absolute: usize) {
        if absolute < self.entries.len() {
            self.current_index = absolute;
        }
    }

    /// Take the resolver out of the current entry (for use with game_state).
    pub fn take_resolver(&mut self) -> Option<AbilityResolver> {
        self.current_entry_mut().and_then(|e| e.resolver.take())
    }

    /// Put the resolver back into the current entry.
    pub fn set_resolver(&mut self, resolver: AbilityResolver) {
        if let Some(entry) = self.current_entry_mut() {
            entry.resolver = Some(resolver);
        }
    }

    /// Check if the current entry has a resolver (i.e. ability execution is in progress).
    pub fn has_resolver(&self) -> bool {
        self.current_entry()
            .and_then(|e| e.resolver.as_ref())
            .is_some()
    }

    /// Dump the current queue state as a multi-line string (for test debug).
    pub fn dump_state(&self) -> String {
        let mut s = String::new();
        s.push_str(&format!("state={:?}", self.state));
        s.push('\n');
        s.push_str(&format!("current_index={}", self.current_index));
        s.push('\n');
        s.push_str(&format!("entries={}", self.entries.len()));
        s.push('\n');
        for (_i, entry) in self.entries.iter().enumerate() {
            s.push_str(&format!(
                "  [{}] card={} ab#{} player={} completed={} cost_paid={} effect_started={} optional_cost_result={:?} pending_actions={}\n",
                entry.ability_index,
                entry.card_no,
                entry.ability_index,
                entry.player_id,
                entry.completed,
                entry.cost_paid,
                entry.effect_started,
                entry.optional_cost_result,
                entry.pending_actions.len(),
            ));
        }
        s
    }
}

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