use anyhow::Result;
use mdbook_preprocessor::PreprocessorContext;
use mdbook_preprocessor::book::Book;
use mdbook_preprocessor::book::BookItem;
use mdbook_preprocessor::{Preprocessor, parse_input};
use std::env;
use std::io;

use mdbook_nix_repl::NixRepl;

fn main() -> Result<()> {
    env_logger::init();

    let mut args = env::args();
    let _bin = args.next();

    if let Some(sub) = args.next()
        && sub == "supports"
    {
        if let Some(renderer) = args.next() {
            let pre = NixRepl;
            let supported = pre.supports_renderer(&renderer).unwrap_or(false);
            if supported {
                println!("true");
                std::process::exit(0);
            } else {
                println!("false");
                std::process::exit(1);
            }
        } else {
            println!("false");
            std::process::exit(1);
        }
    }
    eprintln!("nix-repl main: running on full book");
    let (ctx, book): (PreprocessorContext, Book) = parse_input(io::stdin())?;

    let pre = NixRepl;
    let processed = pre.run(&ctx, book)?;
    // Debug: dump all chapter contents
    for (idx, item) in processed.items.iter().enumerate() {
        if let BookItem::Chapter(ch) = item {
            eprintln!("nix-repl processed chapter {idx}:\n{}", ch.content);
        }
    }
    serde_json::to_writer(io::stdout(), &processed)?;

    Ok(())
}
