pub mod patterns;

use std::collections::HashMap;

/// Detects repeated CLI misfires and records correction heuristics.
#[derive(Default)]
pub struct CorrectionDetector {
    history: Vec<(String, i32)>, // (command, exit_code)
    pub corrections: HashMap<String, String>,
}

impl CorrectionDetector {
    pub fn record(&mut self, command: &str, exit_code: i32) {
        self.history.push((command.to_string(), exit_code));
        self.detect();
    }

    fn detect(&mut self) {
        // If the same command failed twice in a row → mark as needing correction
        let len = self.history.len();
        if len >= 2 {
            let prev = &self.history[len - 2];
            let curr = &self.history[len - 1];
            if prev.0 == curr.0 && prev.1 != 0 && curr.1 != 0 {
                self.corrections
                    .entry(curr.0.clone())
                    .or_insert_with(|| format!("check usage of `{}`", curr.0));
            }
        }
    }
}
