#![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,
)]
// Bug #20: Portfolio value normalization test
// Tests that portfolio value is normalized by initial_capital
// to prevent 100,000x feature imbalance

use anyhow::Result;
use ml::dqn::portfolio_tracker::PortfolioTracker;
use tracing::info;

#[test]
fn test_portfolio_value_normalized_to_baseline() -> Result<()> {
    // Bug #20: Portfolio value should be normalized to ~1.0 baseline
    let initial_capital = 100_000.0;
    let avg_spread = 0.0001;
    let cash_reserve_percent = 0.0;

    let tracker = PortfolioTracker::new(initial_capital, avg_spread, cash_reserve_percent);

    // At start, portfolio value = initial_capital
    let features = tracker.get_portfolio_features(4000.0);

    // Bug #20: Feature should be 1.0 (normalized), NOT 100,000
    assert_eq!(features.len(), 3, "Should have 3 portfolio features");

    let portfolio_feature = features[0];

    // After fix, this should be ~1.0 (normalized by initial_capital)
    // Before fix, this would be 100,000.0 (raw value)
    assert!(
        (portfolio_feature - 1.0).abs() < 0.01,
        "Portfolio value should be normalized to 1.0, got: {}",
        portfolio_feature
    );

    info!(portfolio_feature, "Portfolio value normalized correctly");

    Ok(())
}

#[test]
fn test_all_portfolio_features_similar_scale() -> Result<()> {
    // Bug #20: All portfolio features should be in similar scale
    // No 100,000x imbalance
    let initial_capital = 100_000.0;
    let avg_spread = 0.0001;
    let cash_reserve_percent = 0.0;

    let tracker = PortfolioTracker::new(initial_capital, avg_spread, cash_reserve_percent);

    let features = tracker.get_portfolio_features(4000.0);

    // Check all features are in reasonable scale (NOT 100,000x difference)
    for (i, &feature) in features.iter().enumerate() {
        assert!(
            feature.abs() < 10.0,
            "Feature {} should be in reasonable scale, got: {}",
            i,
            feature
        );
    }

    info!(?features, "All portfolio features in similar scale");

    Ok(())
}

#[test]
fn test_feature_scale_consistency() -> Result<()> {
    // Bug #20: Verify portfolio features don't dominate other features
    // The key test is that portfolio value is NOT 100,000 (raw value)
    let initial_capital = 100_000.0;
    let avg_spread = 0.0001;
    let cash_reserve_percent = 0.0;

    let tracker = PortfolioTracker::new(initial_capital, avg_spread, cash_reserve_percent);

    let features = tracker.get_portfolio_features(4000.0);

    // The key fix: Portfolio value should be ~1.0 (normalized), NOT 100,000
    let portfolio_value = features[0];

    // Before Bug #20 fix: Would be 100,000.0 (raw value)
    // After Bug #20 fix: Should be ~1.0 (normalized)
    assert!(
        portfolio_value.abs() < 10.0,
        "Portfolio value should be normalized, got: {}",
        portfolio_value
    );

    // Check that portfolio value doesn't dwarf other features by 100,000x
    // (spread is intentionally small, but that's OK - key is portfolio isn't massive)
    let max_feature = features.iter().copied().fold(f32::NEG_INFINITY, f32::max);

    // Before fix: max_feature would be 100,000 (raw portfolio value)
    // After fix: max_feature should be ~1.0 (normalized portfolio value)
    assert!(
        max_feature < 10.0,
        "Max feature should be in normalized scale, got: {}",
        max_feature
    );

    info!(max_feature, portfolio_value, "Feature scale check passed");

    Ok(())
}

#[test]
fn test_portfolio_feature_format() -> Result<()> {
    // Test that get_portfolio_features returns 3 features:
    // [portfolio_value, position_normalized, spread]
    let tracker = PortfolioTracker::new(100_000.0, 0.0001, 0.0);
    let features = tracker.get_portfolio_features(4000.0);

    assert_eq!(features.len(), 3, "Should return exactly 3 features");

    // Feature 0: Portfolio value (should be normalized to ~1.0)
    // Feature 1: Position (normalized by max_position)
    // Feature 2: Spread

    info!(?features, "Portfolio features");
    info!(value = features[0], "Feature 0 (portfolio value)");
    info!(position = features[1], "Feature 1 (position)");
    info!(spread = features[2], "Feature 2 (spread)");

    Ok(())
}
