use epub::doc::EpubDoc;
use std::env;
use std::fs::File;
use std::io::BufReader;

fn main() {
    let args: Vec<String> = env::args().collect();
    if args.len() < 2 {
        eprintln!("Usage: {} <path-to-epub>", args[0]);
        std::process::exit(1);
    }

    let path = &args[1];
    let file = File::open(path).expect("Failed to open file");
    let reader = BufReader::new(file);
    let doc = EpubDoc::from_reader(reader).expect("Failed to open EPUB");

    println!("=== SPINE RESOURCES ===");
    for (idx, spine_item) in doc.spine.iter().enumerate() {
        if let Some(resource) = doc.resources.get(&spine_item.idref) {
            println!(
                "Spine[{}]: idref='{}', path='{}'",
                idx,
                spine_item.idref,
                resource.path.display()
            );
        } else {
            println!(
                "Spine[{}]: idref='{}' (resource not found)",
                idx, spine_item.idref
            );
        }
    }

    println!("\n=== TOC NAVPOINTS ===");
    for navpoint in &doc.toc {
        println!(
            "NavPoint: label='{}', content='{}'",
            navpoint.label,
            navpoint.content.display()
        );
        for child in &navpoint.children {
            println!(
                "  Child: label='{}', content='{}'",
                child.label,
                child.content.display()
            );
        }
    }
}
