use std::error::Error;
use std::fs::create_dir_all;

use radial_sugiyama::{
    filter_graph_to_descendants, graph_from_ttl_path, layout_radial_hierarchy_with_artifacts,
    write_svg_path_with_options, EnvConfig,
};

fn main() -> Result<(), Box<dyn Error>> {
    let config = EnvConfig::from_env()?;
    let input_path = config.input_path();
    let output_path = config.output_path();
    let imported_graph = graph_from_ttl_path(&input_path)?;
    let mut graph = filter_graph_to_descendants(&imported_graph, &config.root_class_iri)?;
    let artifacts = layout_radial_hierarchy_with_artifacts(&mut graph, config.layout)?;
    if let Some(parent) = output_path.parent() {
        create_dir_all(parent)?;
    }
    write_svg_path_with_options(&output_path, &graph, &artifacts, config.layout, config.svg)?;

    println!("input={}", input_path.display());
    println!("root={}", config.root_class_iri);
    println!("output={}", output_path.display());
    println!("nodes={}", graph.nodes.len());
    println!("edges={}", graph.edges.len());
    println!("routes={}", artifacts.edge_routes.len());

    Ok(())
}
