#![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,
)]
//! DQN Hyperopt End-to-End Test
//!
//! Proves that the hyperparameter optimizer works end-to-end with real DQN training.
//! This test runs 10 trials of Bayesian optimization (Argmin PSO) and verifies
//! convergence, result structure, and checkpoint artifacts.
//!
//! Run manually:
//!   SQLX_OFFLINE=true cargo test -p ml --test dqn_hyperopt_test -- --ignored --nocapture
//!
//! Expected runtime: 10-20 minutes (GPU), 30-60 minutes (CPU)

#![allow(unused_crate_dependencies)]

use anyhow::{Context, Result};
use std::path::PathBuf;
use tracing::info;
use tracing::warn;

use ml::hyperopt::adapters::dqn::DQNTrainer;
use ml::hyperopt::ArgminOptimizer;

/// Locate the real training data directory.
///
/// Returns `Ok(path)` if the directory exists, `Err` otherwise so the test
/// can skip gracefully without marking as failed.
fn get_data_dir() -> Result<PathBuf> {
    let workspace_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .parent()
        .context("Failed to resolve workspace root")?
        .to_path_buf();

    let data_dir = workspace_root.join("test_data/real/databento/ml_training_small");
    if !data_dir.exists() {
        anyhow::bail!(
            "Training data not found at {}. \
             This test requires real Databento data to run.",
            data_dir.display()
        );
    }
    Ok(data_dir)
}

#[test]
#[ignore]
fn test_dqn_hyperopt_10_trials() -> Result<()> {
    // --- Setup ---
    let data_dir = match get_data_dir() {
        Ok(dir) => dir,
        Err(e) => {
            warn!(reason = %e, "Skipping test: data not available");
            return Ok(());
        }
    };

    let start = std::time::Instant::now();
    let num_trials: usize = 10;
    let n_initial: usize = 3;
    let epochs_per_trial: usize = 5;

    // --- Create trainer and optimizer ---
    let trainer = DQNTrainer::new(&data_dir, epochs_per_trial)
        .context("Failed to create DQNTrainer")?;

    let optimizer = ArgminOptimizer::builder()
        .max_trials(num_trials)
        .n_initial(n_initial)
        .n_particles(5) // small swarm for test speed
        .seed(42)
        .build();

    // --- Run optimization ---
    let result = optimizer
        .optimize(trainer)
        .context("Hyperopt optimization failed")?;

    let elapsed = start.elapsed();

    // --- Assertions ---

    // 1. All trials should have completed (at least n_initial; PSO may add more)
    let trials_completed = result.all_trials.len();
    assert!(
        trials_completed >= n_initial,
        "Expected at least {n_initial} trials, got {trials_completed}"
    );

    // 2. Best objective should be finite (not NaN or Inf)
    assert!(
        result.best_objective.is_finite(),
        "Best objective is not finite: {}",
        result.best_objective
    );

    // 3. Every trial objective should be finite
    for trial in &result.all_trials {
        assert!(
            trial.objective.is_finite(),
            "Trial {} has non-finite objective: {}",
            trial.trial_num,
            trial.objective
        );
        assert!(
            trial.duration_secs > 0.0,
            "Trial {} has non-positive duration: {}",
            trial.trial_num,
            trial.duration_secs
        );
    }

    // 4. Best objective should be <= first trial (optimizer should not regress)
    if let Some(first_trial) = result.all_trials.first() {
        assert!(
            result.best_objective <= first_trial.objective,
            "Optimizer regressed: best={} > first={}",
            result.best_objective,
            first_trial.objective
        );
    }

    // 5. Convergence plot should have entries matching trial count
    assert_eq!(
        result.convergence_plot_data.len(),
        trials_completed,
        "Convergence plot entries ({}) should match trial count ({trials_completed})",
        result.convergence_plot_data.len()
    );

    // 6. Convergence plot should be monotonically non-increasing
    for window in result.convergence_plot_data.windows(2) {
        let (_, prev_best) = window[0];
        let (_, curr_best) = window[1];
        assert!(
            curr_best <= prev_best + f64::EPSILON,
            "Convergence plot is not monotonically non-increasing: {prev_best} -> {curr_best}"
        );
    }

    // 7. Best params should have sane learning rate (log-scale, typically 1e-5 to 1e-2)
    let best_lr = result.best_params.learning_rate;
    assert!(
        best_lr > 1e-7 && best_lr < 1.0,
        "Best learning rate out of sane range: {best_lr}"
    );

    // 8. Best params batch size should be positive and bounded
    let best_bs = result.best_params.batch_size;
    assert!(
        best_bs > 0 && best_bs <= 512,
        "Best batch size out of range: {best_bs}"
    );

    // --- Report ---
    info!(
        trials_completed,
        best_objective = result.best_objective,
        best_lr = result.best_params.learning_rate,
        best_batch_size = result.best_params.batch_size,
        best_gamma = result.best_params.gamma,
        best_buffer_size = result.best_params.buffer_size,
        total_secs = elapsed.as_secs_f64(),
        avg_secs_per_trial = elapsed.as_secs_f64() / trials_completed as f64,
        "DQN HYPEROPT REPORT"
    );
    for trial in &result.all_trials {
        info!(
            trial_num = trial.trial_num,
            objective = trial.objective,
            duration_secs = trial.duration_secs,
            lr = trial.params.learning_rate,
            batch_size = trial.params.batch_size,
            "Trial history"
        );
    }
    for (trial_num, best_so_far) in &result.convergence_plot_data {
        info!(trial_num, best_so_far, "Convergence");
    }

    Ok(())
}
