use criterion::{black_box, criterion_group, criterion_main, Criterion};
use wasot_compiler::base36::{base36_to_u64, u64_to_base36};

fn benchmark_roundtrip(c: &mut Criterion) {
    c.bench_function("base36_roundtrip_small", |b| {
        b.iter(|| {
            // 0 to 1000 in steps of 1
            for i in (0..=1000).step_by(1) {
                let encoded = u64_to_base36(black_box(i));
                let decoded = base36_to_u64(&encoded).unwrap();
                black_box(decoded);
            }
        });
    });

    c.bench_function("base36_roundtrip_medium", |b| {
        b.iter(|| {
            // 1000 to 1_000_000 in steps of 100
            for i in (1000..=1_000_000).step_by(100) {
                let encoded = u64_to_base36(black_box(i));
                let decoded = base36_to_u64(&encoded).unwrap();
                black_box(decoded);
            }
        });
    });

    c.bench_function("base36_roundtrip_large", |b| {
        b.iter(|| {
            // 1_000_000 to 1_000_000_000 in steps of 10_000
            for i in (1_000_000..=1_000_000_000).step_by(10_000) {
                let encoded = u64_to_base36(black_box(i));
                let decoded = base36_to_u64(&encoded).unwrap();
                black_box(decoded);
            }
        });
    });

    c.bench_function("base36_roundtrip_combined", |b| {
        b.iter(|| {
            // Combined: all three ranges
            // 0 to 1000 in steps of 1
            for i in (0..=1000).step_by(1) {
                let encoded = u64_to_base36(black_box(i));
                let decoded = base36_to_u64(&encoded).unwrap();
                black_box(decoded);
            }

            // 1000 to 1_000_000 in steps of 100
            for i in (1000..=1_000_000).step_by(100) {
                let encoded = u64_to_base36(black_box(i));
                let decoded = base36_to_u64(&encoded).unwrap();
                black_box(decoded);
            }

            // 1_000_000 to 1_000_000_000 in steps of 10_000
            for i in (1_000_000..=1_000_000_000).step_by(10_000) {
                let encoded = u64_to_base36(black_box(i));
                let decoded = base36_to_u64(&encoded).unwrap();
                black_box(decoded);
            }
        });
    });
}

criterion_group!(benches, benchmark_roundtrip);
criterion_main!(benches);
