use crate::sizing::RunnerSize;

/// Hard bound on OOM auto-retries per build — ADR-039 constraint oom-retry-bounded.
/// Repeated OOM after one retry indicates misconfiguration; surface to developer.
pub const MAX_OOM_RETRIES: u8 = 1;

/// Returns whether an OOM retry is permitted given the count of retries already consumed.
pub fn oom_retry_allowed(retries_used: u8) -> bool {
    retries_used < MAX_OOM_RETRIES
}

const SIZE_TIERS: &[(&str, u32, u32)] = &[
    ("cx22", 2, 4),
    ("cx32", 4, 8),
    ("cx42", 8, 16),
    ("cx52", 16, 32),
];

pub fn next_size_tier(current: &RunnerSize) -> Option<RunnerSize> {
    // Find current tier by matching cpu/memory_gb
    let current_idx = SIZE_TIERS.iter().position(|(_, cpu, mem)| {
        *cpu >= current.cpu && *mem >= current.memory_gb
    })?;

    SIZE_TIERS.get(current_idx + 1).map(|(_, cpu, mem)| RunnerSize {
        cpu: *cpu,
        memory_gb: *mem,
        disk_gb: current.disk_gb,
        time_budget_min: current.time_budget_min,
    })
}
