use amaru_uplc::{arena::Arena, binder::DeBruijn, flat, machine::PlutusVersion};
use bumpalo::Bump;
use criterion::{criterion_group, criterion_main, Criterion};
use itertools::Itertools;
use std::{fs, time::Duration};

pub fn bench_plutus_use_cases(c: &mut Criterion) {
    let data_dir = std::path::Path::new("benches/use_cases/plutus_use_cases");

    for path in fs::read_dir(data_dir)
        .unwrap()
        .map(|entry| entry.unwrap())
        .map(|entry| entry.path())
        .sorted()
    {
        if path.is_file() {
            let file_name = path
                .file_name()
                .unwrap()
                .to_str()
                .unwrap()
                .replace(".flat", "");

            let script = std::fs::read(&path).unwrap();

            let mut arena = Arena::from_bump(Bump::with_capacity(1_048_576));

            c.bench_function(&file_name, |b| {
                b.iter(|| {
                    let program = flat::decode::<DeBruijn>(&arena, &script, PlutusVersion::V3, 10)
                        .expect("Failed to decode");

                    let result = program.eval(&arena);

                    let _term = result.term.expect("Failed to evaluate");

                    arena.reset();
                })
            });
        }
    }
}

criterion_group! {
    name = plutus_use_cases;
    config = Criterion::default()
        .measurement_time(Duration::from_secs(10));
    targets = bench_plutus_use_cases
}

criterion_main! {
    plutus_use_cases,
}
