use std::path::Path;

use anyhow::Result;

use astmap_core::diff::{since_against_ref, SinceResult};
use astmap_core::scanner::scan;

use super::VcsContext;

pub fn since(
    project_dir: &Path,
    reference: &str,
    depth: i64,
    ctx: &VcsContext<'_>,
) -> Result<SinceResult> {
    let ext = ctx.lang.supported_extensions();
    // Ensure index is fresh before comparing
    if let Err(e) = scan(project_dir, ctx.scan_store, false, ctx.lang, ctx.discovery) {
        tracing::warn!(
            "auto-scan failed, proceeding with potentially stale index: {}",
            e
        );
    }
    let result = since_against_ref(ctx.vcs, reference, ctx.diff_store, depth, ext, ctx.lang)?;
    Ok(result)
}
