#![allow(
    clippy::assertions_on_constants,
    clippy::assertions_on_result_states,
    clippy::clone_on_copy,
    clippy::decimal_literal_representation,
    clippy::doc_markdown,
    clippy::empty_line_after_doc_comments,
    clippy::field_reassign_with_default,
    clippy::get_unwrap,
    clippy::identity_op,
    clippy::inconsistent_digit_grouping,
    clippy::indexing_slicing,
    clippy::integer_division,
    clippy::len_zero,
    clippy::let_underscore_must_use,
    clippy::manual_div_ceil,
    clippy::manual_let_else,
    clippy::manual_range_contains,
    clippy::modulo_arithmetic,
    clippy::needless_range_loop,
    clippy::non_ascii_literal,
    clippy::redundant_clone,
    clippy::shadow_reuse,
    clippy::shadow_same,
    clippy::shadow_unrelated,
    clippy::single_match_else,
    clippy::str_to_string,
    clippy::string_slice,
    clippy::tests_outside_test_module,
    clippy::too_many_lines,
    clippy::unnecessary_wraps,
    clippy::unseparated_literal_suffix,
    clippy::use_debug,
    clippy::useless_vec,
    clippy::wildcard_enum_match_arm,
    clippy::else_if_without_else,
    clippy::expect_used,
    clippy::missing_const_for_fn,
    clippy::similar_names,
    clippy::type_complexity,
    clippy::collapsible_else_if,
    clippy::doc_lazy_continuation,
    clippy::items_after_test_module,
    clippy::map_clone,
    clippy::multiple_unsafe_ops_per_block,
    clippy::unwrap_or_default,
    clippy::assign_op_pattern,
    clippy::needless_borrow,
    clippy::println_empty_string,
    clippy::unnecessary_cast,
    clippy::used_underscore_binding,
    clippy::create_dir,
    clippy::implicit_saturating_sub,
    clippy::exit,
    clippy::expect_fun_call,
    clippy::too_many_arguments,
    clippy::unnecessary_map_or,
    clippy::unwrap_used,
    dead_code,
    unused_imports,
    unused_variables,
    clippy::cloned_ref_to_slice_refs,
    clippy::neg_multiply,
    clippy::while_let_loop,
    clippy::bool_assert_comparison,
    clippy::excessive_precision,
    clippy::trivially_copy_pass_by_ref,
    clippy::op_ref,
    clippy::redundant_closure,
    clippy::unnecessary_lazy_evaluations,
    clippy::if_then_some_else_none,
    clippy::unnecessary_to_owned,
    clippy::single_component_path_imports,
)]
//! Performance Benchmarks for Microstructure Features
//!
//! Agent A13 - Microstructure feature performance validation:
//! - Amihud Illiquidity Ratio (Agent A8)
//! - Roll Measure (Agent A9)
//! - Corwin-Schultz Spread (Agent A10)
//!
//! ## Targets
//! - Amihud: <8μs per update
//! - Roll: <5μs per update
//! - Corwin-Schultz: <15μs per update
//! - Memory: <72 bytes per feature state
//!
//! ## Run Benchmarks
//! ```bash
//! cargo bench -p ml --bench microstructure_bench
//! ```

use criterion::{black_box, criterion_group, criterion_main, BenchmarkId, Criterion};
use ml::features::microstructure::{
    AmihudIlliquidity, CorwinSchultzSpread, MicrostructureFeatures, RollMeasure,
};
use std::time::Duration;

// ============================================================================
// Test Data Generator
// ============================================================================

/// Generate realistic OHLCV market data for benchmarking
fn generate_ohlcv_data(num_bars: usize, seed: u64) -> Vec<(f64, f64, f64, f64)> {
    use std::f64::consts::PI;

    let mut rng = fastrand::Rng::with_seed(seed);
    let mut data = Vec::with_capacity(num_bars);
    let mut close = 100.0;

    for i in 0..num_bars {
        // Combine trend, cycle, and noise
        let trend = (i as f64 * 0.01) % 10.0 - 5.0;
        let cycle = (i as f64 * 0.1 * PI).sin() * 2.0;
        let noise = (rng.f64() - 0.5) * 0.5;

        close += trend * 0.01 + cycle * 0.05 + noise;
        close = close.max(50.0).min(150.0);

        // Generate realistic OHLC with typical 0.1-0.5% intrabar range
        let range = close * 0.003 * (1.0 + rng.f64());
        let high = close + range * rng.f64();
        let low = close - range * rng.f64();

        let volume = 10000.0 + (i as f64 * 0.5 * PI).sin().abs() * 5000.0 + rng.f64() * 2000.0;

        data.push((high, low, close, volume));
    }

    data
}

// ============================================================================
// Amihud Illiquidity Benchmarks (Agent A8)
// ============================================================================

/// Benchmark Amihud Illiquidity single update (cold start)
fn bench_amihud_cold(c: &mut Criterion) {
    let mut group = c.benchmark_group("amihud_illiquidity");
    group.measurement_time(Duration::from_secs(5));

    let data = generate_ohlcv_data(1000, 42);

    group.bench_function("single_update_cold", |b| {
        b.iter(|| {
            let mut amihud = AmihudIlliquidity::new(0.05);
            let (_, _, close, volume) = data[0];
            let result = amihud.update(black_box(close), black_box(volume));
            black_box(result);
        });
    });

    group.finish();
}

/// Benchmark Amihud Illiquidity incremental update (warm state)
fn bench_amihud_warm(c: &mut Criterion) {
    let mut group = c.benchmark_group("amihud_illiquidity_warm");
    group.measurement_time(Duration::from_secs(5));

    let data = generate_ohlcv_data(1000, 43);

    // Warm up with 20 bars
    let mut amihud = AmihudIlliquidity::new(0.05);
    for (_, _, close, volume) in data.iter().take(20) {
        amihud.update(*close, *volume);
    }

    group.bench_function("single_update_warm", |b| {
        let mut ami = amihud.clone();
        let mut idx = 20;

        b.iter(|| {
            let (_, _, close, volume) = data[idx % data.len()];
            let result = ami.update(black_box(close), black_box(volume));
            idx += 1;
            black_box(result);
        });
    });

    group.finish();
}

/// Benchmark Amihud throughput (bars/second)
fn bench_amihud_throughput(c: &mut Criterion) {
    let mut group = c.benchmark_group("amihud_throughput");
    group.measurement_time(Duration::from_secs(10));

    for batch_size in [10, 100, 1000] {
        let data = generate_ohlcv_data(batch_size, 44);

        group.bench_with_input(
            BenchmarkId::from_parameter(batch_size),
            &batch_size,
            |b, _| {
                b.iter(|| {
                    let mut amihud = AmihudIlliquidity::new(0.05);

                    for (_, _, close, volume) in &data {
                        let result = amihud.update(black_box(*close), black_box(*volume));
                        black_box(result);
                    }
                });
            },
        );
    }

    group.finish();
}

/// Benchmark Amihud memory footprint
fn bench_amihud_memory(c: &mut Criterion) {
    let mut group = c.benchmark_group("amihud_memory");
    group.measurement_time(Duration::from_secs(3));

    group.bench_function("struct_size", |b| {
        b.iter(|| {
            let amihud = AmihudIlliquidity::new(black_box(0.05));
            black_box(std::mem::size_of_val(&amihud));
        });
    });

    group.finish();
}

/// Benchmark Amihud normalization for ML features
fn bench_amihud_normalization(c: &mut Criterion) {
    let mut group = c.benchmark_group("amihud_normalization");
    group.measurement_time(Duration::from_secs(3));

    let data = generate_ohlcv_data(100, 45);

    // Warm up
    let mut amihud = AmihudIlliquidity::new(0.05);
    for (_, _, close, volume) in data.iter().take(20) {
        amihud.update(*close, *volume);
    }

    group.bench_function("get_normalized", |b| {
        let ami = amihud.clone();

        b.iter(|| {
            let normalized = ami.get_normalized();
            black_box(normalized);
        });
    });

    group.finish();
}

// ============================================================================
// Roll Measure Benchmarks (Agent A9)
// ============================================================================

/// Benchmark Roll Measure single update (cold start)
fn bench_roll_cold(c: &mut Criterion) {
    let mut group = c.benchmark_group("roll_measure");
    group.measurement_time(Duration::from_secs(5));

    let data = generate_ohlcv_data(1000, 46);

    group.bench_function("single_update_cold", |b| {
        b.iter(|| {
            let mut roll = RollMeasure::new();
            let (_, _, close, _) = data[0];
            roll.update(black_box(close));
            let result = roll.compute();
            black_box(result);
        });
    });

    group.finish();
}

/// Benchmark Roll Measure incremental update (warm state)
fn bench_roll_warm(c: &mut Criterion) {
    let mut group = c.benchmark_group("roll_measure_warm");
    group.measurement_time(Duration::from_secs(5));

    let data = generate_ohlcv_data(1000, 47);

    // Warm up with 21 prices (for 20 price changes)
    let mut roll = RollMeasure::new();
    for (_, _, close, _) in data.iter().take(21) {
        roll.update(*close);
    }

    group.bench_function("update_and_compute_warm", |b| {
        let mut r = roll.clone();
        let mut idx = 21;

        b.iter(|| {
            let (_, _, close, _) = data[idx % data.len()];
            r.update(black_box(close));
            let result = r.compute();
            idx += 1;
            black_box(result);
        });
    });

    group.finish();
}

/// Benchmark Roll Measure throughput
fn bench_roll_throughput(c: &mut Criterion) {
    let mut group = c.benchmark_group("roll_throughput");
    group.measurement_time(Duration::from_secs(10));

    for batch_size in [10, 100, 1000] {
        let data = generate_ohlcv_data(batch_size, 48);

        group.bench_with_input(
            BenchmarkId::from_parameter(batch_size),
            &batch_size,
            |b, _| {
                b.iter(|| {
                    let mut roll = RollMeasure::new();

                    for (_, _, close, _) in &data {
                        roll.update(black_box(*close));
                        let result = roll.compute();
                        black_box(result);
                    }
                });
            },
        );
    }

    group.finish();
}

/// Benchmark Roll Measure memory footprint
fn bench_roll_memory(c: &mut Criterion) {
    let mut group = c.benchmark_group("roll_memory");
    group.measurement_time(Duration::from_secs(3));

    group.bench_function("struct_size", |b| {
        b.iter(|| {
            let roll = RollMeasure::new();
            black_box(std::mem::size_of_val(&roll));
        });
    });

    group.finish();
}

/// Benchmark Roll spread computation only (no update)
fn bench_roll_compute_only(c: &mut Criterion) {
    let mut group = c.benchmark_group("roll_compute_only");
    group.measurement_time(Duration::from_secs(3));

    let data = generate_ohlcv_data(100, 49);

    // Pre-populate Roll with 21 prices
    let mut roll = RollMeasure::new();
    for (_, _, close, _) in data.iter().take(21) {
        roll.update(*close);
    }

    group.bench_function("compute_spread", |b| {
        let r = roll.clone();

        b.iter(|| {
            let result = r.compute();
            black_box(result);
        });
    });

    group.finish();
}

// ============================================================================
// Corwin-Schultz Benchmarks (Agent A10)
// ============================================================================

/// Benchmark Corwin-Schultz single update (cold start)
fn bench_corwin_schultz_cold(c: &mut Criterion) {
    let mut group = c.benchmark_group("corwin_schultz");
    group.measurement_time(Duration::from_secs(5));

    let data = generate_ohlcv_data(1000, 50);

    group.bench_function("single_update_cold", |b| {
        b.iter(|| {
            let mut cs = CorwinSchultzSpread::new();
            let (high, low, close, _) = data[0];
            cs.update(black_box(high), black_box(low), black_box(close));
            let result = cs.compute();
            black_box(result);
        });
    });

    group.finish();
}

/// Benchmark Corwin-Schultz incremental update (warm state)
fn bench_corwin_schultz_warm(c: &mut Criterion) {
    let mut group = c.benchmark_group("corwin_schultz_warm");
    group.measurement_time(Duration::from_secs(5));

    let data = generate_ohlcv_data(1000, 51);

    // Warm up with 21 bars (20-period window + 1)
    let mut cs = CorwinSchultzSpread::new();
    for (high, low, close, _) in data.iter().take(21) {
        cs.update(*high, *low, *close);
    }

    group.bench_function("update_and_compute_warm", |b| {
        let mut c = cs.clone();
        let mut idx = 21;

        b.iter(|| {
            let (high, low, close, _) = data[idx % data.len()];
            c.update(black_box(high), black_box(low), black_box(close));
            let result = c.compute();
            idx += 1;
            black_box(result);
        });
    });

    group.finish();
}

/// Benchmark Corwin-Schultz throughput
fn bench_corwin_schultz_throughput(c: &mut Criterion) {
    let mut group = c.benchmark_group("corwin_schultz_throughput");
    group.measurement_time(Duration::from_secs(10));

    for batch_size in [10, 100, 1000] {
        let data = generate_ohlcv_data(batch_size, 52);

        group.bench_with_input(
            BenchmarkId::from_parameter(batch_size),
            &batch_size,
            |b, _| {
                b.iter(|| {
                    let mut cs = CorwinSchultzSpread::new();

                    for (high, low, close, _) in &data {
                        cs.update(black_box(*high), black_box(*low), black_box(*close));
                        let result = cs.compute();
                        black_box(result);
                    }
                });
            },
        );
    }

    group.finish();
}

/// Benchmark Corwin-Schultz memory footprint
fn bench_corwin_schultz_memory(c: &mut Criterion) {
    let mut group = c.benchmark_group("corwin_schultz_memory");
    group.measurement_time(Duration::from_secs(3));

    group.bench_function("struct_size", |b| {
        b.iter(|| {
            let cs = CorwinSchultzSpread::new();
            black_box(std::mem::size_of_val(&cs));
        });
    });

    group.finish();
}

/// Benchmark Corwin-Schultz computation only (no update)
fn bench_corwin_schultz_compute_only(c: &mut Criterion) {
    let mut group = c.benchmark_group("corwin_schultz_compute_only");
    group.measurement_time(Duration::from_secs(3));

    let data = generate_ohlcv_data(100, 53);

    // Pre-populate with 21 bars
    let mut cs = CorwinSchultzSpread::new();
    for (high, low, close, _) in data.iter().take(21) {
        cs.update(*high, *low, *close);
    }

    group.bench_function("compute_spread", |b| {
        let c = cs.clone();

        b.iter(|| {
            let result = c.compute();
            black_box(result);
        });
    });

    group.finish();
}

// ============================================================================
// Comparative Benchmarks
// ============================================================================

/// Compare all three microstructure features side-by-side
fn bench_all_features_comparison(c: &mut Criterion) {
    let mut group = c.benchmark_group("microstructure_comparison");
    group.measurement_time(Duration::from_secs(10));

    let data = generate_ohlcv_data(1000, 54);

    // Warm up all features
    let mut amihud = AmihudIlliquidity::new(0.05);
    let mut roll = RollMeasure::new();
    let mut cs = CorwinSchultzSpread::new();

    for (high, low, close, volume) in data.iter().take(21) {
        amihud.update(*close, *volume);
        roll.update(*close);
        cs.update(*high, *low, *close);
    }

    // Benchmark Amihud
    group.bench_function("amihud_update", |b| {
        let mut ami = amihud.clone();
        let mut idx = 21;

        b.iter(|| {
            let (_, _, close, volume) = data[idx % data.len()];
            let result = ami.update(black_box(close), black_box(volume));
            idx += 1;
            black_box(result);
        });
    });

    // Benchmark Roll
    group.bench_function("roll_update_compute", |b| {
        let mut r = roll.clone();
        let mut idx = 21;

        b.iter(|| {
            let (_, _, close, _) = data[idx % data.len()];
            r.update(black_box(close));
            let result = r.compute();
            idx += 1;
            black_box(result);
        });
    });

    // Benchmark Corwin-Schultz
    group.bench_function("corwin_schultz_update_compute", |b| {
        let mut c = cs.clone();
        let mut idx = 21;

        b.iter(|| {
            let (high, low, close, _) = data[idx % data.len()];
            c.update(black_box(high), black_box(low), black_box(close));
            let result = c.compute();
            idx += 1;
            black_box(result);
        });
    });

    group.finish();
}

/// Benchmark all three features together (realistic pipeline)
fn bench_combined_pipeline(c: &mut Criterion) {
    let mut group = c.benchmark_group("microstructure_pipeline");
    group.measurement_time(Duration::from_secs(10));

    let data = generate_ohlcv_data(1000, 55);

    // Warm up
    let mut amihud = AmihudIlliquidity::new(0.05);
    let mut roll = RollMeasure::new();
    let mut cs = CorwinSchultzSpread::new();

    for (high, low, close, volume) in data.iter().take(21) {
        amihud.update(*close, *volume);
        roll.update(*close);
        cs.update(*high, *low, *close);
    }

    group.bench_function("all_three_features", |b| {
        let mut ami = amihud.clone();
        let mut r = roll.clone();
        let mut c = cs.clone();
        let mut idx = 21;

        b.iter(|| {
            let (high, low, close, volume) = data[idx % data.len()];

            // Update all features (realistic HFT pipeline)
            let amihud_val = ami.update(black_box(close), black_box(volume));
            r.update(black_box(close));
            let roll_val = r.compute();
            c.update(black_box(high), black_box(low), black_box(close));
            let cs_val = c.compute();

            idx += 1;

            black_box((amihud_val, roll_val, cs_val));
        });
    });

    group.finish();
}

// ============================================================================
// Latency Distribution Analysis
// ============================================================================

/// Measure P50/P95/P99 latencies for each microstructure feature
fn bench_latency_distribution(c: &mut Criterion) {
    let mut group = c.benchmark_group("microstructure_latency_distribution");
    group.measurement_time(Duration::from_secs(10));
    group.sample_size(1000); // Increase for better percentile accuracy

    let data = generate_ohlcv_data(1000, 56);

    // Warm up
    let mut amihud = AmihudIlliquidity::new(0.05);
    let mut roll = RollMeasure::new();
    let mut cs = CorwinSchultzSpread::new();

    for (high, low, close, volume) in data.iter().take(21) {
        amihud.update(*close, *volume);
        roll.update(*close);
        cs.update(*high, *low, *close);
    }

    // Amihud P50/P95/P99
    group.bench_function("amihud_p50_p95_p99", |b| {
        let mut ami = amihud.clone();
        let mut idx = 21;

        b.iter(|| {
            let (_, _, close, volume) = data[idx % data.len()];
            let result = ami.update(black_box(close), black_box(volume));
            idx += 1;
            black_box(result);
        });
    });

    // Roll P50/P95/P99
    group.bench_function("roll_p50_p95_p99", |b| {
        let mut r = roll.clone();
        let mut idx = 21;

        b.iter(|| {
            let (_, _, close, _) = data[idx % data.len()];
            r.update(black_box(close));
            let result = r.compute();
            idx += 1;
            black_box(result);
        });
    });

    // Corwin-Schultz P50/P95/P99
    group.bench_function("corwin_schultz_p50_p95_p99", |b| {
        let mut c = cs.clone();
        let mut idx = 21;

        b.iter(|| {
            let (high, low, close, _) = data[idx % data.len()];
            c.update(black_box(high), black_box(low), black_box(close));
            let result = c.compute();
            idx += 1;
            black_box(result);
        });
    });

    group.finish();
}

// ============================================================================
// Criterion Configuration
// ============================================================================

criterion_group!(
    benches,
    // Amihud Illiquidity (Agent A8)
    bench_amihud_cold,
    bench_amihud_warm,
    bench_amihud_throughput,
    bench_amihud_memory,
    bench_amihud_normalization,
    // Roll Measure (Agent A9)
    bench_roll_cold,
    bench_roll_warm,
    bench_roll_throughput,
    bench_roll_memory,
    bench_roll_compute_only,
    // Corwin-Schultz (Agent A10)
    bench_corwin_schultz_cold,
    bench_corwin_schultz_warm,
    bench_corwin_schultz_throughput,
    bench_corwin_schultz_memory,
    bench_corwin_schultz_compute_only,
    // Comparative benchmarks
    bench_all_features_comparison,
    bench_combined_pipeline,
    bench_latency_distribution,
);

criterion_main!(benches);
