use anyhow::Result;
use std::path::Path;

use crate::db;
use crate::ops;

pub fn run(root: &Path, ids: &[String], json: bool) -> Result<()> {
    let store = db::open(root)?;

    let mut closed = Vec::new();
    for raw in ids {
        let id = db::resolve_task_id(&store, raw, false)?;
        ops::mark_done(&store, &id)?;
        closed.push(id);
    }

    if json {
        let out: Vec<_> = closed
            .iter()
            .map(|id| serde_json::json!({"id": id, "status": "closed"}))
            .collect();
        println!("{}", serde_json::to_string(&out)?);
    } else {
        let c = crate::color::stdout_theme();
        for id in &closed {
            println!("{}closed{} {id}", c.green, c.reset);
        }
    }

    Ok(())
}
