//! Narrative system for AI-generated storytelling.
//!
//! This module provides the foundation for the AI Story Director,
//! which tracks player actions and weaves them into an ongoing narrative.
//!
//! ## Components
//!
//! - **Event Logging**: Capture all player actions as structured events
//! - **Campaign History**: Maintain timeline of events with chapter divisions
//! - **Narrative Synthesis**: Generate story text from events (LLM-powered)
//! - **Story UI**: Display campaign narrative to players

mod events;
mod history;
mod synthesis;
mod ui;

pub use events::*;
pub use history::*;
pub use synthesis::*;
pub use ui::*;

use bevy::prelude::*;

use crate::state::AppState;

/// Main narrative plugin.
pub struct NarrativePlugin;

impl Plugin for NarrativePlugin {
    fn build(&self, app: &mut App) {
        app.init_resource::<NarrativeConfig>()
            .init_resource::<CampaignHistory>()
            .add_event::<GameEvent>()
            // Narrative systems run during gameplay
            .add_systems(
                Update,
                events::log_game_events.run_if(in_state(AppState::InGame)),
            )
            // Chapter detection runs periodically
            .add_systems(
                Update,
                history::detect_chapters.run_if(in_state(AppState::InGame)),
            );
    }
}

/// Configuration for narrative generation.
#[derive(Resource, Debug, Clone)]
pub struct NarrativeConfig {
    /// Whether narrative generation is enabled
    pub enabled: bool,
    /// Events per chapter (approximate)
    pub events_per_chapter: usize,
    /// Whether to use LLM for synthesis (when available)
    pub use_llm_synthesis: bool,
    /// Update interval for story generation (seconds)
    pub synthesis_interval: f32,
}

impl Default for NarrativeConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            events_per_chapter: 25,
            use_llm_synthesis: false, // Disabled until LLM integration
            synthesis_interval: 60.0,
        }
    }
}
