use crate::file::{resolve_hash, CitrineFile};
use anyhow::Result;
use std::path::Path;

pub fn run(dir: &Path, partial_hash: &str) -> Result<()> {
    let citrine_path = dir.join(".citrine");
    if !citrine_path.exists() {
        anyhow::bail!("Not initialized. Run 'citrine init' first");
    }

    let mut main_file = CitrineFile::read_or_empty(&citrine_path);
    let mut local_file = CitrineFile::read_or_empty(&dir.join(".citrine.local"));

    let (hash, is_local) = resolve_hash(&main_file, &local_file, partial_hash)?;

    let file = if is_local { &mut local_file } else { &mut main_file };
    let task = file.remove_task(&hash).unwrap();

    println!("Removed [{}]: {}", hash, task.description);

    let path = if is_local {
        dir.join(".citrine.local")
    } else {
        citrine_path
    };
    file.write(&path)?;

    Ok(())
}
