use std::path::Path;

use rrt_runtime::inspect::smp::{
    regions::{
        SmpSaveRegionFixedRowRunComparisonReport, compare_save_region_fixed_row_run_candidates,
    },
    world::inspect_save_company_and_chairman_analysis_file,
};
use serde::Serialize;

#[derive(Debug, Serialize)]
pub(crate) struct RuntimeRegionFixedRowRunComparisonOutput {
    pub(crate) left_path: String,
    pub(crate) right_path: String,
    pub(crate) comparison: SmpSaveRegionFixedRowRunComparisonReport,
}

pub(crate) fn compare_region_fixed_row_runs(
    left_path: &Path,
    right_path: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    let left = inspect_save_company_and_chairman_analysis_file(left_path)?;
    let right = inspect_save_company_and_chairman_analysis_file(right_path)?;
    let comparison = compare_save_region_fixed_row_run_candidates(&left, &right)
        .ok_or("save inspection did not expose grounded region fixed-row candidate probes")?;
    let report = RuntimeRegionFixedRowRunComparisonOutput {
        left_path: left_path.display().to_string(),
        right_path: right_path.display().to_string(),
        comparison,
    };
    println!("{}", serde_json::to_string_pretty(&report)?);
    Ok(())
}
