use std::io::{self, BufRead};
use crate::{
config::Config,
error::Result,
habitica::{HabiticaClient, StatsCache},
sync::ConflictResolver,
taskwarrior::{Task, TaskwarriorClient},
};
pub fn handle_add(config: &Config) -> Result<()> {
let stdin = io::stdin();
let mut lines = stdin.lock().lines();
let task_json = lines
.next()
.ok_or_else(|| crate::error::Error::custom("No input provided"))??;
if config.verbose {
eprintln!(
"DEBUG: Received JSON (length {}): {}",
task_json.len(),
task_json
);
}
let task: Task = serde_json::from_str(&task_json).map_err(|e| {
crate::error::Error::custom(format!(
"Failed to parse task JSON: {}. Input length: {}",
e,
task_json.len()
))
})?;
let tw_client = TaskwarriorClient::new();
let h_client = HabiticaClient::new(config)?;
let resolver = ConflictResolver::new(config, &tw_client, &h_client);
let mut stats_cache = if task.status.is_completed() {
let stats = h_client.get_user_stats()?;
Some(StatsCache::new(stats))
} else {
None
};
let updated_task = resolver.push_to_habitica(&task, &mut stats_cache)?;
if let Some(cache) = stats_cache {
cache.save(&config.stats_cache_path())?;
}
let output_json = serde_json::to_string(&updated_task)?;
println!("{output_json}");
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
fn test_add_command_exists() {
}
}