//! Generates ONE dungeon for a fixed seed and prints its ASCII map plus a
//! per-region table (id, kind, theme, cell count, bounds) and the entity list.
//!
//! A throwaway viewer used to produce a concrete example board for tabletop
//! game design (DEEPFALL). Deterministic: same seed → same dungeon.
//!
//! Usage:
//! ```text
//! cargo run --example sample_dungeon -p reikhelm-core [seed] [width] [height]
//! ```

use std::env;

use reikhelm_core::recipes::dungeon::{dungeon, DungeonConfig};

fn main() {
    let mut args = env::args().skip(1);
    let seed: u64 = args.next().and_then(|s| s.parse().ok()).unwrap_or(42);
    let width: u32 = args.next().and_then(|s| s.parse().ok()).unwrap_or(48);
    let height: u32 = args.next().and_then(|s| s.parse().ok()).unwrap_or(32);

    let cfg = DungeonConfig {
        width,
        height,
        ..DungeonConfig::default()
    };

    let pipeline = match dungeon(cfg) {
        Ok(p) => p,
        Err(e) => {
            eprintln!("config rejected: {e}");
            std::process::exit(1);
        }
    };

    let map = pipeline.run(seed);

    println!("=== reikhelm dungeon — seed {seed}, {width}x{height} ===");
    println!();
    println!("Legend: # wall  . floor  + door  o pillar  ~ water  ! lava");
    println!();
    println!("{}", map.to_ascii());
    println!();

    // Per-region table. Only real rooms carry a theme; corridors stay None.
    println!("--- regions (id | kind | theme | cells | bounds) ---");
    let mut rooms = 0usize;
    let mut corridors = 0usize;
    for r in &map.regions {
        if r.cells.is_empty() {
            continue;
        }
        let kind = format!("{:?}", r.kind);
        let theme = match r.theme {
            Some(t) => format!("{t:?}"),
            None => "-".to_string(),
        };
        let b = r.bounds;
        println!(
            "{:>3} | {:<8} | {:<10} | {:>4} | ({},{}) {}x{}",
            r.id.0,
            kind,
            theme,
            r.cells.len(),
            b.x,
            b.y,
            b.w,
            b.h,
        );
        match r.kind {
            reikhelm_core::region::RegionKind::Room => rooms += 1,
            reikhelm_core::region::RegionKind::Corridor => corridors += 1,
        }
    }
    println!();
    println!("totals: {rooms} rooms, {corridors} corridors");
    println!();

    // Entities (entrance / exit / treasure / monsters) live as an overlay.
    println!("--- entities (kind @ x,y) ---");
    for e in &map.entities {
        println!("{:?} @ {},{}", e.kind, e.at.x, e.at.y);
    }
}
