//! `ckg stats` — symbol/edge counts for the current repo.
use std::path::PathBuf;

use anyhow::Result;
use clap::Args as ClapArgs;

#[derive(ClapArgs, Debug)]
pub struct Args {
    #[arg(long)]
    pub path: Option<PathBuf>,
}

pub fn run(home: Option<PathBuf>, args: Args) -> Result<()> {
    let (_base, storage) = super::repo_lookup::open_for(home, args.path.as_deref())?;
    let count = |script: &str| -> Result<i64> {
        let rows = storage.run_immutable(script)?;
        Ok(rows.rows.len() as i64)
    };
    let n_sym = count("?[id] := *Symbol{id}")?;
    let n_calls = count("?[s] := *Calls{src: s}")?;
    let n_imports = count("?[s] := *Imports{src: s}")?;
    let n_extends = count("?[s] := *Extends{src: s}")?;
    let n_implements = count("?[s] := *Implements{src: s}")?;
    println!("repo_id:    {}", storage.repo_id());
    println!("db_path:    {}", storage.db_path().display());
    println!("symbols:    {n_sym}");
    println!("calls:      {n_calls}");
    println!("imports:    {n_imports}");
    println!("extends:    {n_extends}");
    println!("implements: {n_implements}");
    Ok(())
}
