use std::path::Path;

use crate::app::reports::inspect::{
    RuntimeInfrastructureAssetTraceOutput, RuntimePeriodicCompanyServiceTraceOutput,
    RuntimeRegionServiceTraceOutput, RuntimeSaveCompanyChairmanAnalysisOutput,
    RuntimeSmpInspectionOutput,
};
use rrt_runtime::inspect::smp::{
    bundle::{inspect_smp_file, inspect_unclassified_save_collection_headers_file},
    services::{
        inspect_save_infrastructure_asset_trace_file,
        inspect_save_periodic_company_service_trace_file, inspect_save_region_service_trace_file,
    },
    structures::{
        inspect_save_placed_structure_dynamic_side_buffer_file,
        inspect_save_region_queued_notice_records_file,
    },
    world::inspect_save_company_and_chairman_analysis_file,
};

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

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

pub(crate) fn inspect_save_placed_structure_triplets(
    smp_path: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    let analysis = inspect_save_company_and_chairman_analysis_file(smp_path)?;
    println!(
        "{}",
        serde_json::to_string_pretty(&analysis.placed_structure_record_triplets)?
    );
    Ok(())
}

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

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

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

pub(crate) fn inspect_save_region_queued_notice_records(
    smp_path: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    println!(
        "{}",
        serde_json::to_string_pretty(&inspect_save_region_queued_notice_records_file(smp_path)?)?
    );
    Ok(())
}

pub(crate) fn inspect_placed_structure_dynamic_side_buffer(
    smp_path: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    println!(
        "{}",
        serde_json::to_string_pretty(&inspect_save_placed_structure_dynamic_side_buffer_file(
            smp_path
        )?)?
    );
    Ok(())
}

pub(crate) fn inspect_unclassified_save_collections(
    smp_path: &Path,
) -> Result<(), Box<dyn std::error::Error>> {
    println!(
        "{}",
        serde_json::to_string_pretty(&inspect_unclassified_save_collection_headers_file(
            smp_path
        )?)?
    );
    Ok(())
}
