seedkit 2.1.1

Generate realistic, constraint-safe seed data for PostgreSQL, MySQL, and SQLite
use std::path::Path;

use anyhow::Result;

use seedkit_core::config::read_config;
use seedkit_core::graph::cycle::break_cycles;
use seedkit_core::graph::dag::DependencyGraph;
use seedkit_core::graph::visualize::{self, GraphFormat as VizFormat};

use crate::args::GraphArgs;

pub async fn run(args: &GraphArgs) -> Result<()> {
    let config = read_config(Path::new("."))?;
    let (_, schema) = super::database::introspect(
        args.db.as_deref(),
        args.schema.as_deref(),
        config.as_ref(),
        &args.schema_file,
    )
    .await?;

    // Break cycles where `generate` does, so the diagram matches the output.
    let hints = config.map(|c| c.graph.break_cycle_at).unwrap_or_default();
    let mut dep_graph = DependencyGraph::from_schema(&schema);
    let deferred = break_cycles(&mut dep_graph, &hints)?;

    let format = match args.format {
        crate::args::GraphFormat::Mermaid => VizFormat::Mermaid,
        crate::args::GraphFormat::Dot => VizFormat::Dot,
    };

    let output = visualize::visualize(&dep_graph, &deferred, format);
    println!("{}", output);

    Ok(())
}