//! `ckg list` — print indexed repos from the manifest.
use std::path::PathBuf;

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

#[derive(ClapArgs, Debug)]
pub struct Args {}

pub fn run(home: Option<PathBuf>, _args: Args) -> Result<()> {
    let base = crate::ckg_home(home)?;
    let manifest = Manifest::read(&base.join("manifest.json"))?;
    if manifest.repos.is_empty() {
        println!("(no repositories indexed)");
        return Ok(());
    }
    println!("{:<18}  {:<10}  PATH", "REPO_ID", "HEAD");
    for (path, e) in &manifest.repos {
        let head_short = if e.head_sha.len() >= 8 {
            &e.head_sha[..8]
        } else {
            &e.head_sha
        };
        println!("{:<18}  {:<10}  {}", e.repo_id, head_short, path);
    }
    Ok(())
}
