//! Faction-based AI behavior profiles.
//!
//! Different factions have different AI behaviors and response patterns.
//! This module defines faction-specific AI characteristics.

use bevy::prelude::*;

/// AI behavior profile for a faction.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FactionAiProfile {
    /// Concord: Strict law enforcement, immediate response to hostiles
    Concord,
    /// Amarr: Hierarchical, organized patrols
    Amarr,
    /// Minmatar: Aggressive, fast response
    Minmatar,
    /// Gallente: Balanced, diplomatic preference
    Gallente,
    /// Caldari: Calculated, strategic positioning
    Caldari,
    /// Pirates: Opportunistic, ambush tactics
    Pirates,
    /// Independent: Variable behavior
    Independent,
}

/// Faction-specific AI behavior settings.
#[derive(Component, Debug, Clone)]
pub struct FactionAiBehavior {
    /// Faction this behavior belongs to
    pub faction: String,
    /// Behavior profile for this faction
    pub profile: FactionAiProfile,
    /// Combat aggressiveness (0.0 to 1.0)
    pub aggressiveness: f32,
    /// Response time to threats (seconds)
    pub response_time: f32,
    /// Preference for diplomatic solutions (0.0 to 1.0)
    pub diplomatic_preference: f32,
    /// Trade openness (0.0 to 1.0)
    pub trade_openness: f32,
}

impl Default for FactionAiBehavior {
    fn default() -> Self {
        Self {
            faction: "Independent".to_string(),
            profile: FactionAiProfile::Independent,
            aggressiveness: 0.5,
            response_time: 5.0,
            diplomatic_preference: 0.5,
            trade_openness: 0.5,
        }
    }
}

impl FactionAiBehavior {
    /// Create faction-specific AI behavior from faction name.
    pub fn from_faction(faction: &str) -> Self {
        let profile = match faction {
            "Concord" => FactionAiProfile::Concord,
            "Amarr" => FactionAiProfile::Amarr,
            "Minmatar" => FactionAiProfile::Minmatar,
            "Gallente" => FactionAiProfile::Gallente,
            "Caldari" => FactionAiProfile::Caldari,
            "Pirates" => FactionAiProfile::Pirates,
            _ => FactionAiProfile::Independent,
        };

        let (aggressiveness, response_time, diplomatic_preference, trade_openness) = match profile {
            FactionAiProfile::Concord => (0.9, 2.0, 0.3, 0.5), // Quick to respond, lawful
            FactionAiProfile::Amarr => (0.7, 4.0, 0.2, 0.6),    // Organized but slower
            FactionAiProfile::Minmatar => (0.8, 3.0, 0.4, 0.7), // Aggressive but reasonable
            FactionAiProfile::Gallente => (0.5, 5.0, 0.8, 0.9), // Diplomatic, trade-friendly
            FactionAiProfile::Caldari => (0.6, 4.0, 0.5, 0.7), // Calculated, balanced
            FactionAiProfile::Pirates => (0.9, 1.0, 0.0, 0.1), // Very aggressive, hostile
            FactionAiProfile::Independent => (0.5, 5.0, 0.6, 0.8), // Variable
        };

        Self {
            faction: faction.to_string(),
            profile,
            aggressiveness,
            response_time,
            diplomatic_preference,
            trade_openness,
        }
    }

    /// Get the preferred engagement distance for this faction.
    pub fn preferred_engagement_distance(&self) -> f32 {
        match self.profile {
            FactionAiProfile::Concord => 80.0,  // Close range for law enforcement
            FactionAiProfile::Amarr => 120.0,    // Mid-range, organized
            FactionAiProfile::Minmatar => 60.0,  // Close range, aggressive
            FactionAiProfile::Gallente => 100.0, // Mid-range, balanced
            FactionAiProfile::Caldari => 150.0,   // Long range, strategic
            FactionAiProfile::Pirates => 50.0,  // Very close, ambush
            FactionAiProfile::Independent => 100.0,
        }
    }

    /// Get the flee threshold for this faction.
    pub fn flee_threshold(&self) -> f32 {
        match self.profile {
            FactionAiProfile::Concord => 0.1, // Rarely flee (law enforcement)
            FactionAiProfile::Amarr => 0.25,   // Flee at 25% hull
            FactionAiProfile::Minmatar => 0.3, // Flee at 30% hull
            FactionAiProfile::Gallente => 0.35,
            FactionAiProfile::Caldari => 0.2,   // Calculated retreat
            FactionAiProfile::Pirates => 0.5,  // Flee easily (opportunistic)
            FactionAiProfile::Independent => 0.35,
        }
    }
}
