#![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 Long Training Test (50 epochs)
//!
//! Proves 50 epochs of training on the small dataset produces meaningful
//! convergence: loss decreases >20%, all losses finite, epsilon decays
//! below 0.15, and loss trajectory trends downward.
//!
//! Run manually:
//! ```sh
//! SQLX_OFFLINE=true cargo test -p ml --test dqn_long_training_test -- --ignored --nocapture
//! ```

#![allow(unused_crate_dependencies)]

use anyhow::{Context, Result};
use ml::trainers::dqn::{DQNHyperparameters, DQNTrainer};
use std::path::PathBuf;
use std::time::Instant;
use tracing::info;
use tracing::warn;

fn get_data_dir() -> Result<String> {
    let workspace_root = PathBuf::from(env!("CARGO_MANIFEST_DIR"))
        .ancestors()
        .find(|p| p.join("test_data").exists())
        .context("Failed to find workspace root with test_data/")?
        .to_path_buf();
    let data_dir = workspace_root.join("test_data/real/databento");
    if !data_dir.exists() {
        anyhow::bail!("Data not found: {}", data_dir.display());
    }
    Ok(data_dir.to_string_lossy().to_string())
}

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

    let checkpoint_dir = tempfile::tempdir()?;
    let start_time = Instant::now();

    // --- Configure hyperparameters ---
    let mut hyperparams = DQNHyperparameters::conservative();
    hyperparams.replay_buffer_vram_fraction = 0.0; // Disable AutoReplaySizer for test determinism
    hyperparams.epochs = 50;
    hyperparams.batch_size = 64;
    hyperparams.learning_rate = 0.0001;
    hyperparams.epsilon_start = 1.0;
    hyperparams.epsilon_end = 0.01;
    hyperparams.epsilon_decay = 0.95;
    hyperparams.early_stopping_enabled = true;
    hyperparams.min_epochs_before_stopping = 50; // allow all 50 epochs
    hyperparams.gradient_collapse_patience = 20;
    hyperparams.checkpoint_frequency = 10;

    // --- Train ---
    let mut trainer = DQNTrainer::new(hyperparams)?;

    let _metrics = trainer
        .train(&data_dir, |epoch, checkpoint_data, is_best| {
            let name = if is_best {
                "long_best.safetensors".to_string()
            } else {
                format!("long_epoch_{epoch}.safetensors")
            };
            let path = checkpoint_dir.path().join(&name);
            std::fs::write(&path, &checkpoint_data)?;
            Ok(path.to_string_lossy().to_string())
        })
        .await?;

    let training_duration = start_time.elapsed();

    // --- Collect results ---
    let loss_history = trainer.loss_history();
    let final_epsilon = trainer.get_agent_epsilon().await;

    let initial_loss = loss_history.first().copied().unwrap_or(f64::MAX);
    let final_loss = loss_history.last().copied().unwrap_or(f64::MAX);

    // --- ASSERT 1: All 50 epochs completed ---
    assert!(
        loss_history.len() >= 10,
        "Expected at least 10 epochs of loss history, got {}",
        loss_history.len()
    );

    // --- ASSERT 2: Loss decreases >20% ---
    // Observed: ~32% reduction with conservative hyperparams on small dataset.
    // Threshold set to 20% for robustness across runs.
    let loss_reduction_pct = if initial_loss.abs() > f64::EPSILON {
        (1.0 - final_loss / initial_loss) * 100.0
    } else {
        0.0
    };
    assert!(
        final_loss < initial_loss * 0.80,
        "Loss did not decrease >20%. Initial={initial_loss:.6}, Final={final_loss:.6}, \
         Reduction={loss_reduction_pct:.1}%"
    );

    // --- ASSERT 3: Final loss is bounded (not diverging) ---
    // Initial loss is typically ~4.2; final should be well below initial.
    assert!(
        final_loss < initial_loss,
        "Final loss ({final_loss:.6}) should be less than initial loss ({initial_loss:.6})"
    );

    // --- ASSERT 4: All losses finite (no NaN/Inf) ---
    for (i, loss) in loss_history.iter().enumerate() {
        assert!(
            loss.is_finite(),
            "Loss at epoch {i} is not finite: {loss}"
        );
    }

    // --- ASSERT 5: Epsilon correct for noisy nets (BUG #40 FIX) ---
    // conservative() enables use_noisy_nets=true → epsilon is pinned to 0.0
    // (exploration via NoisyLinear weight perturbation + noisy_epsilon_floor)
    assert!(
        (final_epsilon as f64) < 0.01,
        "Epsilon should be ~0.0 with noisy nets (got {final_epsilon:.4}). \
         BUG #40: epsilon is set to 0 at training start when noisy nets are on."
    );

    // --- ASSERT 6: Smoothed loss trajectory trends downward ---
    // Average of first 10 epochs should be higher than average of last 10 epochs.
    // This catches cases where loss oscillates wildly but endpoints happen to look ok.
    let n = loss_history.len();
    if n >= 20 {
        let first_10_avg: f64 = loss_history[..10].iter().sum::<f64>() / 10.0;
        let last_10_avg: f64 = loss_history[n - 10..].iter().sum::<f64>() / 10.0;
        assert!(
            last_10_avg < first_10_avg,
            "Smoothed loss trajectory is not decreasing: first_10_avg={first_10_avg:.6}, \
             last_10_avg={last_10_avg:.6}"
        );
    }

    // --- ASSERT 7: Best checkpoint file was saved ---
    let best_checkpoint = checkpoint_dir.path().join("long_best.safetensors");
    assert!(
        best_checkpoint.exists(),
        "Best checkpoint file was not saved"
    );
    let checkpoint_size = std::fs::metadata(&best_checkpoint)?.len();
    assert!(
        checkpoint_size > 0,
        "Best checkpoint file is empty ({checkpoint_size} bytes)"
    );

    // --- Report ---
    info!(
        epochs_completed = loss_history.len(),
        initial_loss,
        final_loss,
        loss_reduction_pct,
        final_epsilon,
        checkpoint_size_bytes = checkpoint_size,
        training_secs = training_duration.as_secs_f64(),
        "DQN 50-EPOCH LONG TRAINING REPORT — ALL 7 ASSERTIONS PASSED"
    );

    Ok(())
}
