#![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 Factored Action Tests - TDD Red Phase
///
/// These tests verify the porting of DQN's 45-action factored space to PPO.
/// Tests written BEFORE implementation to ensure correct behavior.

use ml::ppo::{ExposureLevel, FactoredAction, OrderType, Urgency};

#[test]
fn test_factored_action_index_mapping() {
    // Test index <-> (exposure, order, urgency) conversion
    // Formula: index = exposure*9 + order*3 + urgency

    // Test boundary cases
    let action_0 = FactoredAction::from_index(0).unwrap();
    assert_eq!(action_0.exposure, ExposureLevel::Short100);
    assert_eq!(action_0.order, OrderType::Market);
    assert_eq!(action_0.urgency, Urgency::Patient);
    assert_eq!(action_0.to_index(), 0);

    let action_44 = FactoredAction::from_index(44).unwrap();
    assert_eq!(action_44.exposure, ExposureLevel::Long100);
    assert_eq!(action_44.order, OrderType::IoC);
    assert_eq!(action_44.urgency, Urgency::Aggressive);
    assert_eq!(action_44.to_index(), 44);

    // Test middle case (Flat + Market + Normal)
    // index = 2*9 + 0*3 + 1 = 19
    let action_19 = FactoredAction::from_index(19).unwrap();
    assert_eq!(action_19.exposure, ExposureLevel::Flat);
    assert_eq!(action_19.order, OrderType::Market);
    assert_eq!(action_19.urgency, Urgency::Normal);
    assert_eq!(action_19.to_index(), 19);

    // Test round-trip for all 45 actions
    for idx in 0..45 {
        let action = FactoredAction::from_index(idx).unwrap();
        assert_eq!(action.to_index(), idx, "Round-trip failed for index {}", idx);
    }

    // Test out of bounds
    assert!(FactoredAction::from_index(45).is_err());
    assert!(FactoredAction::from_index(100).is_err());
}

#[test]
fn test_factored_action_exposure_levels() {
    // Test all 5 exposure levels: -100%, -50%, 0%, +50%, +100%

    let short100 = FactoredAction::new(
        ExposureLevel::Short100,
        OrderType::Market,
        Urgency::Normal,
    );
    assert_eq!(short100.target_exposure(), -1.0);

    let short50 = FactoredAction::new(
        ExposureLevel::Short50,
        OrderType::Market,
        Urgency::Normal,
    );
    assert_eq!(short50.target_exposure(), -0.5);

    let flat = FactoredAction::new(
        ExposureLevel::Flat,
        OrderType::Market,
        Urgency::Normal,
    );
    assert_eq!(flat.target_exposure(), 0.0);

    let long50 = FactoredAction::new(
        ExposureLevel::Long50,
        OrderType::Market,
        Urgency::Normal,
    );
    assert_eq!(long50.target_exposure(), 0.5);

    let long100 = FactoredAction::new(
        ExposureLevel::Long100,
        OrderType::Market,
        Urgency::Normal,
    );
    assert_eq!(long100.target_exposure(), 1.0);
}

#[test]
fn test_factored_action_order_types() {
    // Test 3 order types: Market (0.15%), LimitMaker (0.05%), IoC (0.10%)

    let market = FactoredAction::new(
        ExposureLevel::Flat,
        OrderType::Market,
        Urgency::Normal,
    );
    assert_eq!(market.transaction_cost(), 0.0015); // 0.15%

    let limit_maker = FactoredAction::new(
        ExposureLevel::Flat,
        OrderType::LimitMaker,
        Urgency::Normal,
    );
    assert_eq!(limit_maker.transaction_cost(), 0.0005); // 0.05%

    let ioc = FactoredAction::new(
        ExposureLevel::Flat,
        OrderType::IoC,
        Urgency::Normal,
    );
    assert_eq!(ioc.transaction_cost(), 0.0010); // 0.10%
}

#[test]
fn test_factored_action_urgency_levels() {
    // Test 3 urgency levels: Patient (0.5x), Normal (1.0x), Aggressive (1.5x)

    let patient = FactoredAction::new(
        ExposureLevel::Flat,
        OrderType::Market,
        Urgency::Patient,
    );
    assert_eq!(patient.urgency_weight(), 0.5);

    let normal = FactoredAction::new(
        ExposureLevel::Flat,
        OrderType::Market,
        Urgency::Normal,
    );
    assert_eq!(normal.urgency_weight(), 1.0);

    let aggressive = FactoredAction::new(
        ExposureLevel::Flat,
        OrderType::Market,
        Urgency::Aggressive,
    );
    assert_eq!(aggressive.urgency_weight(), 1.5);
}

#[test]
fn test_factored_action_to_position_delta() {
    // Test conversion to position changes
    // Formula: delta = (target_exposure - current_position) * max_position

    // Test Long100 from Flat position
    let long100 = FactoredAction::new(
        ExposureLevel::Long100,
        OrderType::Market,
        Urgency::Normal,
    );
    let delta = long100.to_position_delta(0.0, 2.0);
    // target_exposure = 1.0, current = 0.0
    // delta = (1.0 - 0.0) * 2.0 = 2.0
    assert_eq!(delta, 2.0);

    // Test Flat from Long50 position
    let flat = FactoredAction::new(
        ExposureLevel::Flat,
        OrderType::Market,
        Urgency::Normal,
    );
    let delta = flat.to_position_delta(1.0, 2.0);
    // target_exposure = 0.0, current_position = 1.0
    // target_position = 0.0 * 2.0 = 0.0
    // delta = 0.0 - 1.0 = -1.0
    assert_eq!(delta, -1.0);

    // Test Short100 from Flat position
    let short100 = FactoredAction::new(
        ExposureLevel::Short100,
        OrderType::Market,
        Urgency::Normal,
    );
    let delta = short100.to_position_delta(0.0, 2.0);
    // target_exposure = -1.0, current = 0.0
    // delta = (-1.0 - 0.0) * 2.0 = -2.0
    assert_eq!(delta, -2.0);

    // Test no change (already at target)
    let long50 = FactoredAction::new(
        ExposureLevel::Long50,
        OrderType::Market,
        Urgency::Normal,
    );
    let delta = long50.to_position_delta(1.0, 2.0);
    // target_exposure = 0.5, current = 1.0
    // current_exposure = 1.0 / 2.0 = 0.5
    // delta = (0.5 - 0.5) * 2.0 = 0.0
    assert_eq!(delta, 0.0);
}

#[test]
fn test_factored_action_boundary_cases() {
    // Test index 0: Short100 + Market + Patient
    let action_0 = FactoredAction::from_index(0).unwrap();
    assert_eq!(action_0.exposure, ExposureLevel::Short100);
    assert_eq!(action_0.order, OrderType::Market);
    assert_eq!(action_0.urgency, Urgency::Patient);
    assert_eq!(action_0.target_exposure(), -1.0);
    assert_eq!(action_0.transaction_cost(), 0.0015);
    assert_eq!(action_0.urgency_weight(), 0.5);

    // Test index 44: Long100 + IoC + Aggressive
    let action_44 = FactoredAction::from_index(44).unwrap();
    assert_eq!(action_44.exposure, ExposureLevel::Long100);
    assert_eq!(action_44.order, OrderType::IoC);
    assert_eq!(action_44.urgency, Urgency::Aggressive);
    assert_eq!(action_44.target_exposure(), 1.0);
    assert_eq!(action_44.transaction_cost(), 0.0010);
    assert_eq!(action_44.urgency_weight(), 1.5);

    // Test specific known index: 22 = Flat + LimitMaker + Patient
    // index = 2*9 + 1*3 + 0 = 18 + 3 + 0 = 21 (wait, let me recalculate)
    // index = 2*9 + 1*3 + 0 = 18 + 3 = 21
    let action_21 = FactoredAction::from_index(21).unwrap();
    assert_eq!(action_21.exposure, ExposureLevel::Flat);
    assert_eq!(action_21.order, OrderType::LimitMaker);
    assert_eq!(action_21.urgency, Urgency::Patient);
    assert_eq!(action_21.to_index(), 21);
}
