use clap::{Arg, ArgMatches, Command};
use std::path::Path;
use std::time::Instant;

use crate::cmd::resolve_db_path;

pub fn command() -> Command {
    Command::new("migrate")
        .about("Apply any pending database schema migrations")
        .long_about(
            "Open the local commonmeta database and apply any pending schema \
             migrations, then exit. Safe to run on databases built with older \
             releases: migrations are idempotent and only alter the schema \
             (no existing records are modified or removed).\n\n\
             Each step is printed to stderr with elapsed time. On large databases \
             (hundreds of millions of records) index creation can take 30–90 minutes; \
             the command will not hang silently.\n\n\
             Examples:\n\n\
             commonmeta migrate\n\
             commonmeta migrate --file /var/lib/commonmeta/commonmeta.sqlite3",
        )
        .arg(
            Arg::new("file")
                .long("file")
                .help("Path to the SQLite database (overrides COMMONMETA_DB and platform default)"),
        )
}

pub fn execute(matches: &ArgMatches) -> Result<(), String> {
    let db_path_str = resolve_db_path(matches.get_one::<String>("file"));
    let db_path = Path::new(&db_path_str);

    if !db_path.exists() {
        return Err(format!(
            "database not found at '{}'; run 'commonmeta import' first",
            db_path_str
        ));
    }

    eprintln!("migrate: {}", db_path_str);
    let total_start = Instant::now();

    let (applied, version) =
        commonmeta::run_migrations(db_path).map_err(|e| e.to_string())?;

    if applied == 0 {
        eprintln!("migrate: already at schema version {version}, nothing to do");
    } else {
        eprintln!(
            "migrate: applied {} step(s), schema version {} ({:.1?})",
            applied,
            version,
            total_start.elapsed()
        );
    }

    println!("migrated {} (schema version {})", db_path_str, version);
    Ok(())
}
