//! Combat trigger handling.
//!
//! Provides a unified function for triggering creature abilities during combat.

use crate::core::cards::CardDatabase;
use crate::core::effects::{EffectSource, Trigger};
use crate::core::engine::EffectQueue;
use crate::core::state::GameState;
use crate::core::types::{PlayerId, Slot};

/// Trigger a creature's ability of a specific type.
///
/// This is the unified handler for all creature-triggered abilities during combat:
/// - OnAttack
/// - OnDealDamage
/// - OnTakeDamage
/// - OnKill
/// - OnDeath
/// - OnAllyDeath
///
/// # Arguments
/// * `state` - The current game state
/// * `card_db` - Card database for looking up card definitions
/// * `effect_queue` - Effect queue for queuing triggered effects
/// * `player` - The player who owns the creature
/// * `slot` - The slot of the creature
/// * `trigger` - The trigger type to check for
pub fn trigger_creature_ability(
    state: &GameState,
    card_db: &CardDatabase,
    effect_queue: &mut EffectQueue,
    player: PlayerId,
    slot: Slot,
    trigger: Trigger,
) {
    let creature = match state.players[player.index()].get_creature(slot) {
        Some(c) => c,
        None => return,
    };

    // Don't trigger if silenced
    if creature.status.is_silenced() {
        return;
    }

    let card_id = creature.card_id;
    let card_def = match card_db.get(card_id) {
        Some(c) => c,
        None => return,
    };

    let abilities = match card_def.creature_abilities() {
        Some(a) => a,
        None => return,
    };

    for ability in abilities {
        if ability.trigger == trigger {
            let source = EffectSource::Creature { owner: player, slot };
            for effect_def in &ability.effects {
                if let Some(effect) = crate::engine::convert_effect_def(
                    effect_def,
                    crate::engine::ConversionContext::CombatTrigger {
                        source_owner: player,
                        source_slot: slot,
                        ability,
                    },
                ) {
                    effect_queue.push(effect, source);
                }
            }
        }
    }
}

