use std::path::Path;

use crate::app::reports::state::{RuntimeLoadedSaveSliceOutput, RuntimeSaveLoadSummaryOutput};
use rrt_runtime::inspect::smp::{bundle::inspect_smp_file, save_load::load_save_slice_file};

pub(crate) fn summarize_save_load(smp_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
    let inspection = inspect_smp_file(smp_path)?;
    let summary = inspection.save_load_summary.ok_or_else(|| {
        format!(
            "{} did not expose a recognizable save-load summary",
            smp_path.display()
        )
    })?;
    let report = RuntimeSaveLoadSummaryOutput {
        path: smp_path.display().to_string(),
        summary,
    };
    println!("{}", serde_json::to_string_pretty(&report)?);
    Ok(())
}

pub(crate) fn load_save_slice(smp_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
    let report = RuntimeLoadedSaveSliceOutput {
        path: smp_path.display().to_string(),
        save_slice: load_save_slice_file(smp_path)?,
    };
    println!("{}", serde_json::to_string_pretty(&report)?);
    Ok(())
}
