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

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

pub fn run(root: &Path, id: &str, message: &str, json: bool) -> Result<()> {
    let store = db::open(root)?;
    let task_id = db::resolve_task_id(&store, id, false)?;
    let entry = ops::add_log(&store, &task_id, message)?;

    if json {
        println!("{}", serde_json::to_string(&entry)?);
    } else {
        println!("logged to {}", task_id);
    }

    Ok(())
}
