use forge::budget::estimator::TokenEstimator;

#[derive(Debug, Clone)]
pub struct SavesReport {
    pub command: String,
    pub tokens_before: usize,
    pub tokens_after: usize,
}

impl SavesReport {
    pub fn new(command: impl Into<String>, tokens_before: usize, tokens_after: usize) -> Self {
        Self {
            command: command.into(),
            tokens_before,
            tokens_after,
        }
    }

    pub fn compute(command: impl Into<String>, before: &str, after: &str) -> Self {
        Self {
            command: command.into(),
            tokens_before: TokenEstimator::count_nonblocking(before),
            tokens_after: TokenEstimator::count_nonblocking(after),
        }
    }

    pub fn savings_pct(&self) -> f64 {
        TokenEstimator::savings_pct(self.tokens_before, self.tokens_after)
    }

    pub fn tokens_saved(&self) -> usize {
        self.tokens_before.saturating_sub(self.tokens_after)
    }

    pub fn render_inline(&self) -> String {
        if self.tokens_before == self.tokens_after || self.tokens_before == 0 {
            return String::new();
        }
        format!(
            "\n\x1b[2m[bctx: {} → {} tokens, {:.0}% saved]\x1b[0m",
            self.tokens_before,
            self.tokens_after,
            self.savings_pct()
        )
    }
}
