use std::collections::BTreeMap;
use aurora_core::{AuroraResult, Pipeline, Value};

pub fn suggest_fix(errors: &[String]) -> AuroraResult<Pipeline> {
    let mut results = Vec::new();

    for error in errors {
        let suggestion = suggest_one(error);
        let mut record = BTreeMap::new();
        record.insert("error".to_string(), Value::String(error.clone()));
        record.insert("suggestion".to_string(), Value::String(suggestion));
        results.push(Value::Record(record));
    }

    Ok(Pipeline::table(
        vec!["error".to_string(), "suggestion".to_string()],
        results.into_iter().map(|r| {
            match r {
                Value::Record(map) => {
                    vec![
                        map.get("error").cloned().unwrap_or(Value::Null),
                        map.get("suggestion").cloned().unwrap_or(Value::Null),
                    ]
                }
                _ => vec![],
            }
        }).collect(),
    ))
}

fn suggest_one(error: &str) -> String {
    let lower = error.to_lowercase();
    if lower.contains("permission denied") || lower.contains("eacces") {
        "Try: chmod +x <file> or run with sudo (if appropriate)".to_string()
    } else if lower.contains("not found") || lower.contains("enoent") || lower.contains("no such") {
        "Check the path exists. Try: which <command> or ls <path>".to_string()
    } else if lower.contains("segfault") || lower.contains("segv") || lower.contains("core dump") {
        "Run with valgrind to detect memory issues: valgrind --tool=memcheck <program>".to_string()
    } else if lower.contains("out of memory") || lower.contains("oom") || lower.contains("cannot allocate") {
        "Check for memory leaks with valgrind. Try: free -h to check available memory. Consider increasing swap.".to_string()
    } else if lower.contains("connection refused") || lower.contains("econnrefused") {
        "Check if the service is running: systemctl status <service> or netstat -tlnp".to_string()
    } else if lower.contains("file exists") || lower.contains("eexist") {
        "Use --force or -f flag to overwrite, or choose a different output path.".to_string()
    } else if lower.contains("timeout") || lower.contains("timed out") || lower.contains("etimedout") {
        "Increase timeout value or check network connectivity. Try: ping <host>".to_string()
    } else if lower.contains("syntax error") || lower.contains("parse error") {
        "Check your syntax near the reported line. Use a linter: cargo check, python -m py_compile, etc.".to_string()
    } else if lower.contains("rustc") || lower.contains("compile") || lower.contains("error[") {
        "Check the compiler error details. Try: cargo check for a faster feedback loop.".to_string()
    } else if lower.contains("git") {
        "Try: git status to see current state, then git add/commit/stash as needed.".to_string()
    } else {
        "No specific suggestion available. Try searching the error message online.".to_string()
    }
}
