seedkit 2.1.1

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

use anyhow::{bail, Context, Result};

use seedkit_core::check;
use seedkit_core::lock;

use crate::args::{CheckArgs, CheckFormat};

/// Run schema drift detection against seedkit.lock.
///
/// Exit codes:
///   0 — no drift detected
///   1 — drift detected (or error)
pub async fn run(args: &CheckArgs) -> Result<()> {
    let lock_path = Path::new(lock::LOCK_FILE_NAME);
    if !lock_path.exists() {
        bail!(
            "No {} found. Run `seedkit generate` first to create a lock file.",
            lock::LOCK_FILE_NAME,
        );
    }

    let lock_file = lock::read_lock_file(lock_path)?;

    // Resolve DB and introspect
    let schema = super::database::introspect_configured(
        args.db.as_deref(),
        args.schema.as_deref(),
        &args.schema_file,
    )
    .await?;

    let report = check::check_drift(&lock_file.schema_snapshot, &lock_file.schema_hash, &schema);

    match args.format {
        CheckFormat::Json => {
            let json = serde_json::to_string_pretty(&report)
                .context("Failed to serialize drift report")?;
            println!("{}", json);
        }
        CheckFormat::Text => {
            println!("{}", report.summary());
        }
    }

    if report.has_drift {
        process::exit(1);
    }

    Ok(())
}