use audio_samples::{AudioStatistics, sine_wave};
use criterion::{Criterion, black_box, criterion_group, criterion_main};
use std::time::Duration;

fn bench_transforms_peak(c: &mut Criterion) {
    let audio = sine_wave::<f32, f32>(100.0, Duration::from_secs(5), 44100, 0.6);
    c.bench_function("transforms::peak", |b| b.iter(|| black_box(audio.peak())));
}

// FFT / spectral benches are feature-gated because they require the
// `spectral-analysis` feature (and its FFT dependencies). Add these
// benches when the feature is enabled.
#[cfg(feature = "spectral-analysis")]
mod spectral_benches {
    use super::*;
    use audio_samples::operations::AudioTransforms;
    use audio_samples::operations::types::WindowType;
    use ndarray::Array2;

    pub fn bench_fft_info(c: &mut Criterion) {
        let audio = sine_wave::<f32, f32>(440.0, Duration::from_secs(5), 44100, 0.6);
        c.bench_function("transforms::fft_info", |b| {
            b.iter(|| {
                // Call fft_info with small n_fft and a window to keep runtime short
                let _ = audio
                    .fft_info::<f32>(Some(1024), Some(WindowType::Hanning), true)
                    .unwrap();
            })
        });
    }

    pub fn bench_stft_and_spectrogram(c: &mut Criterion) {
        let audio = sine_wave::<f32, f32>(440.0, Duration::from_secs(5), 44100, 0.6);
        c.bench_function("transforms::stft", |b| {
            b.iter(|| {
                let _ = audio.stft::<f32>(1024, 512, WindowType::Hanning).unwrap();
            })
        });

        c.bench_function("transforms::spectrogram", |b| {
            b.iter(|| {
                let _ = audio
                    .spectrogram::<f32>(
                        1024,
                        512,
                        WindowType::Hanning,
                        audio_samples::operations::types::SpectrogramScale::Linear,
                        false,
                    )
                    .unwrap();
            })
        });
    }

    criterion_group!(spectral, bench_fft_info, bench_stft_and_spectrogram);
}

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