use crate::config::Config;
use crate::store::Store;
use anyhow::{Context, Result};
use chrono::NaiveDate;

pub fn run(config: &Config, date: NaiveDate) -> Result<()> {
    let store = Store::new(&config.storage_dir);
    let path = store.find_or_create_path(date);

    let editor = std::env::var("EDITOR")
        .or_else(|_| std::env::var("VISUAL"))
        .unwrap_or_else(|_| "vim".to_string());

    std::process::Command::new(&editor)
        .arg(&path)
        .status()
        .with_context(|| format!("failed to launch editor '{}'", editor))?;

    Ok(())
}
