#![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,
)]
//! PPO Hyperopt Validation Test
//!
//! Validates PPO hyperparameter optimization pipeline end-to-end.
//! Runs 5 trials of PSO optimization and verifies convergence,
//! result structure, and parameter bounds.
//!
//! Run manually:
//!   SQLX_OFFLINE=true cargo test -p ml --test ppo_hyperopt_validation_test -- --ignored --nocapture
//!
//! Expected runtime: 15-30 minutes (GPU), 45-90 minutes (CPU)

#![allow(unused_crate_dependencies)]

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

use ml::hyperopt::adapters::ppo::PPOTrainer;
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_ppo_hyperopt_5_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 = 5;
    let n_initial: usize = 2;
    let episodes_per_trial: usize = 10;

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

    let optimizer = ArgminOptimizer::builder()
        .max_trials(num_trials)
        .n_initial(n_initial)
        .n_particles(3) // 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 with positive duration
    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 policy learning rate should be in sane range
    let best_policy_lr = result.best_params.policy_learning_rate;
    assert!(
        best_policy_lr > 1e-7 && best_policy_lr < 1.0,
        "Best policy learning rate out of sane range: {best_policy_lr}"
    );

    // 8. Best value learning rate should be in sane range
    let best_value_lr = result.best_params.value_learning_rate;
    assert!(
        best_value_lr > 1e-7 && best_value_lr < 1.0,
        "Best value learning rate out of sane range: {best_value_lr}"
    );

    // 9. Clip epsilon should be within configured bounds
    let best_clip = result.best_params.clip_epsilon;
    assert!(
        best_clip >= 0.1 && best_clip <= 0.3,
        "Best clip epsilon out of bounds [0.1, 0.3]: {best_clip}"
    );

    // 10. Value loss coefficient should be within configured bounds
    let best_vlc = result.best_params.value_loss_coeff;
    assert!(
        best_vlc >= 0.5 && best_vlc <= 2.0,
        "Best value loss coeff out of bounds [0.5, 2.0]: {best_vlc}"
    );

    // 11. Entropy coefficient should be in sane range
    let best_entropy = result.best_params.entropy_coeff;
    assert!(
        best_entropy > 1e-5 && best_entropy < 1.0,
        "Best entropy coeff out of sane range: {best_entropy}"
    );

    // --- Report ---
    info!(
        trials_completed,
        best_objective = result.best_objective,
        best_policy_lr = result.best_params.policy_learning_rate,
        best_value_lr = result.best_params.value_learning_rate,
        best_clip_eps = result.best_params.clip_epsilon,
        best_value_coeff = result.best_params.value_loss_coeff,
        best_entropy_coeff = result.best_params.entropy_coeff,
        total_time_secs = elapsed.as_secs_f64(),
        avg_time_per_trial = elapsed.as_secs_f64() / trials_completed as f64,
        "PPO Hyperopt Report"
    );
    for trial in &result.all_trials {
        info!(
            trial_num = trial.trial_num,
            objective = trial.objective,
            duration_secs = trial.duration_secs,
            policy_lr = trial.params.policy_learning_rate,
            value_lr = trial.params.value_learning_rate,
            clip_eps = trial.params.clip_epsilon,
            "Trial result"
        );
    }
    for (trial_num, best_so_far) in &result.convergence_plot_data {
        info!(trial_num, best_so_far, "Convergence");
    }

    Ok(())
}
