use crate::config::GlobalConfig;
use crate::db;
use serde_json;

pub fn run(json: bool) {
    let global = GlobalConfig::load();
    let Some(path) = global.notebook_path() else {
        eprintln!("Notebook not initialized");
        std::process::exit(1);
    };

    match db::list_notes(&path) {
        Ok(notes) => {
            if json {
                match serde_json::to_string_pretty(&notes) {
                    Ok(json_str) => println!("{}", json_str),
                    Err(e) => {
                        eprintln!("Failed to serialize JSON: {}", e);
                        std::process::exit(1);
                    }
                }
            } else {
                for note in notes {
                    println!("{} {}", note.filename, note.title);
                }
            }
        }
        Err(e) => {
            eprintln!("Failed to list notes: {}", e);
            std::process::exit(1);
        }
    }
}
