#![allow(clippy::unwrap_used)]

//! Criterion benchmarks for the diversity enforcement selector.
//!
//! Measures greedy selection across five scenarios:
//! - `max_per_creator` only (common case)
//! - `format_mix` only
//! - Both constraints combined
//! - Worst-case relaxation (all stages triggered)
//! - No constraints (fast path baseline)

use criterion::{Criterion, black_box, criterion_group, criterion_main};
use tidaldb::ranking::{DiversityConstraints, DiversitySelector, ScoredCandidate};
use tidaldb::schema::EntityId;

fn make_200_candidates(n_creators: usize) -> Vec<ScoredCandidate> {
    (0..200_usize)
        .map(|i| ScoredCandidate {
            entity_id: EntityId::new(i as u64 + 1),
            #[allow(clippy::cast_precision_loss)]
            score: (200 - i) as f64 / 200.0,
            signal_snapshot: vec![],
            creator_id: Some(EntityId::new((i % n_creators) as u64 + 1)),
            format: Some(if i % 3 == 0 {
                "video".into()
            } else {
                "audio".into()
            }),
        })
        .collect()
}

fn bench_diversity_200_max_per_creator_2(c: &mut Criterion) {
    let candidates = make_200_candidates(50);
    let constraints = DiversityConstraints::new().max_per_creator(2);
    c.bench_function("diversity_200_max_per_creator_2", |b| {
        b.iter(|| {
            DiversitySelector::select(
                black_box(&candidates),
                black_box(&constraints),
                black_box(100),
            )
        });
    });
}

fn bench_diversity_200_format_mix(c: &mut Criterion) {
    let candidates = make_200_candidates(200);
    let constraints = DiversityConstraints::new().format_mix(0.6);
    c.bench_function("diversity_200_format_mix", |b| {
        b.iter(|| {
            DiversitySelector::select(
                black_box(&candidates),
                black_box(&constraints),
                black_box(100),
            )
        });
    });
}

fn bench_diversity_200_combined(c: &mut Criterion) {
    let candidates = make_200_candidates(50);
    let constraints = DiversityConstraints::new()
        .max_per_creator(2)
        .format_mix(0.6);
    c.bench_function("diversity_200_combined", |b| {
        b.iter(|| {
            DiversitySelector::select(
                black_box(&candidates),
                black_box(&constraints),
                black_box(100),
            )
        });
    });
}

fn bench_diversity_200_worst_case_relaxation(c: &mut Criterion) {
    // All same creator -- unsatisfiable constraints trigger all relaxation stages.
    let candidates = make_200_candidates(1);
    let constraints = DiversityConstraints::new()
        .max_per_creator(1)
        .format_mix(0.4);
    c.bench_function("diversity_200_worst_case_relaxation", |b| {
        b.iter(|| {
            DiversitySelector::select(
                black_box(&candidates),
                black_box(&constraints),
                black_box(100),
            )
        });
    });
}

fn bench_diversity_200_no_constraints(c: &mut Criterion) {
    let candidates = make_200_candidates(200);
    let constraints = DiversityConstraints::new();
    c.bench_function("diversity_200_no_constraints", |b| {
        b.iter(|| {
            DiversitySelector::select(
                black_box(&candidates),
                black_box(&constraints),
                black_box(100),
            )
        });
    });
}

criterion_group!(
    benches,
    bench_diversity_200_max_per_creator_2,
    bench_diversity_200_format_mix,
    bench_diversity_200_combined,
    bench_diversity_200_worst_case_relaxation,
    bench_diversity_200_no_constraints,
);
criterion_main!(benches);
