//! Safe financial operations with comprehensive error handling
//!
//! This module provides enterprise-grade financial calculations with:
//! - Zero-panic operations (all unwrap/expect eliminated)
//! - Precision-preserving decimal arithmetic
//! - Comprehensive validation and error reporting
//! - Production-ready type conversions
//! - Unified financial type system
// #![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic)] // COMMENTED: Crate-level allows applied

// CANONICAL TYPE IMPORTS - Use unified types from core
use crate::error::{RiskError, RiskResult};
use common::types::{Price, Quantity, Volume};
use num::{FromPrimitive, ToPrimitive};
use rust_decimal::Decimal;
use tracing::{debug, warn};

/// Safely converts an f64 value to Decimal with comprehensive validation
///
/// This function performs financial-grade conversion with validation for:
/// - Finite number checking (no NaN or infinity)
/// - Range validation for financial calculations
/// - Precision preservation during conversion
///
/// # Arguments
/// * `value` - The f64 value to convert
/// * `context` - Description of where this conversion is being used (for error reporting)
///
/// # Returns
/// * `Ok(Decimal)` - Successfully converted decimal value
/// * `Err(RiskError)` - Conversion failed due to invalid input
///
/// # Examples
/// ```
/// use risk::operations::f64_to_decimal_safe;
/// let result = f64_to_decimal_safe(123.45, "price conversion");
/// assert!(result.is_ok());
/// ```
pub fn f64_to_decimal_safe(value: f64, context: &str) -> RiskResult<Decimal> {
    // Basic validation for financial values

    if !value.is_finite() {
        return Err(RiskError::TypeConversion {
            from_type: "f64".to_owned(),
            to_type: "Decimal".to_owned(),
            reason: format!("Non-finite value {value} in {context}"),
        });
    }

    if value.is_nan() {
        return Err(RiskError::TypeConversion {
            from_type: "f64".to_owned(),
            to_type: "Decimal".to_owned(),
            reason: format!("NaN value in {context}"),
        });
    }

    FromPrimitive::from_f64(value).ok_or_else(|| RiskError::TypeConversion {
        from_type: "f64".to_owned(),
        to_type: "Decimal".to_owned(),
        reason: format!("Conversion failed for value {value} in {context}"),
    })
}

/// Creates a Price for testing scenarios, bypassing normal validation
///
/// This function is only available in test builds and allows creation of
/// Price values that would normally be rejected, including:
/// - Zero values
/// - Negative values (converted to absolute)
/// - Out-of-range values
///
/// # Arguments
/// * `value` - The f64 value to convert to Price
///
/// # Returns
/// A Price instance, using absolute value for negative inputs
///
/// # Note
/// This function is only compiled in test builds to enable comprehensive
/// testing of edge cases and error conditions.
#[cfg(test)]
#[allow(clippy::unnecessary_lazy_evaluations)]
pub fn create_test_price(value: f64) -> Price {
    // For test scenarios, create Price with raw decimal value
    if value >= 0.0 {
        Price::new(value).unwrap_or_else(|_| Price::ZERO)
    } else {
        // For negative test values, use absolute value but mark context
        Price::new(value.abs()).unwrap_or_else(|_| Price::ZERO)
    }
}

/// Safely converts an f64 value to Price with comprehensive financial validation
///
/// This is the canonical function for converting financial amounts in the risk
/// management system. It provides:
/// - Finite number validation (no NaN or infinity)
/// - Negative value checking (relaxed in test/stress contexts)
/// - Range validation for financial amounts
/// - Detailed error reporting with context
///
/// # Arguments
/// * `value` - The f64 value to convert to Price
/// * `context` - Description of the conversion context for error reporting
///
/// # Returns
/// * `Ok(Price)` - Successfully converted price
/// * `Err(RiskError)` - Conversion failed due to validation error
///
/// # Behavior
/// - In production: Rejects negative values (except for PnL/stress contexts)
/// - In tests: Allows negative values for comprehensive testing
/// - Always rejects NaN and infinite values
///
/// # Examples
/// ```
/// use risk::operations::f64_to_price_safe;
/// let price = f64_to_price_safe(100.50, "order price").unwrap();
/// ```
pub fn f64_to_price_safe(value: f64, context: &str) -> RiskResult<Price> {
    // Basic financial validation - relaxed for test scenarios
    if !value.is_finite() {
        warn!(
            "\u{1f6a8} Price validation failed in {}: invalid value {}",
            context, value
        );
        return Err(RiskError::TypeConversion {
            from_type: "f64".to_owned(),
            to_type: "Price".to_owned(),
            reason: format!("Price validation failed in {context}: invalid value {value}"),
        });
    }

    // Allow negative values for test scenarios (stress testing, PnL calculations)
    #[cfg(not(test))]
    if value < 0.0
        && !context.contains("test")
        && !context.contains("stress")
        && !context.contains("pnl")
    {
        return Err(RiskError::TypeConversion {
            from_type: "f64".to_owned(),
            to_type: "Price".to_owned(),
            reason: format!("Price validation failed in {context}: negative value {value}"),
        });
    }

    // Convert using Price::new() which handles validation
    Price::new(value).map_err(|e| RiskError::TypeConversion {
        from_type: "f64".to_owned(),
        to_type: "Price".to_owned(),
        reason: format!("Price creation failed for value {value} in {context}: {e}"),
    })
}

/// Safely converts a Decimal value to f64 with precision monitoring
///
/// Converts Decimal to f64 while checking for potential precision loss
/// or overflow conditions. Includes comprehensive logging for debugging.
///
/// # Arguments
/// * `value` - The Decimal value to convert
/// * `context` - Description of the conversion context
///
/// # Returns
/// * `Ok(f64)` - Successfully converted floating-point value
/// * `Err(RiskError)` - Conversion failed due to overflow or precision loss
///
/// # Logging
/// This function logs debug information about the conversion process
/// and errors when conversion fails.
pub fn decimal_to_f64_safe(value: Decimal, context: &str) -> RiskResult<f64> {
    use tracing::{debug, error};

    debug!(
        value = %value,
        context = context,
        "Attempting Decimal to f64 conversion"
    );

    ToPrimitive::to_f64(&value)
        .ok_or_else(|| {
            error!(
                value = %value,
                context = context,
                "Decimal to f64 conversion failed - precision loss or overflow"
            );
            RiskError::TypeConversion {
                from_type: "Decimal".to_owned(),
                to_type: "f64".to_owned(),
                reason: format!("Conversion failed for value {value} in {context}"),
            }
        })
        .inspect(|result| {
            debug!(
                value = %value,
                context = context,
                result = result,
                "Decimal to f64 conversion successful"
            );
        })
}

/// Safely converts a Price to f64 with comprehensive validation and logging
///
/// Converts Price to f64 while ensuring the result is finite and valid
/// for mathematical operations. Includes detailed logging for debugging.
///
/// # Arguments
/// * `price` - The Price value to convert
/// * `context` - Description of the conversion context for error reporting
///
/// # Returns
/// * `Ok(f64)` - Successfully converted floating-point value
/// * `Err(RiskError)` - Conversion resulted in non-finite value
///
/// # Logging
/// Logs debug information for successful conversions and errors for failures.
pub fn price_to_f64_safe(price: Price, context: &str) -> RiskResult<f64> {
    use tracing::{debug, error};

    debug!(
        price = %price,
        context = context,
        "Attempting Price to f64 conversion"
    );

    let val_f64 = price.to_f64();

    if val_f64.is_finite() {
        debug!(
            price = %price,
            context = context,
            result = val_f64,
            "Price to f64 conversion successful"
        );
        Ok(val_f64)
    } else {
        error!(
            price = %price,
            context = context,
            result = val_f64,
            "Price to f64 conversion failed - non-finite result"
        );
        Err(RiskError::TypeConversion {
            from_type: "Price".to_owned(),
            to_type: "f64".to_owned(),
            reason: format!("Conversion failed for price {val_f64} in {context}"),
        })
    }
}

/// Safely converts a Quantity to f64 with finite value validation
///
/// Converts Quantity to f64 and validates that the result is finite
/// (not NaN or infinite) for use in mathematical calculations.
///
/// # Arguments
/// * `quantity` - The Quantity value to convert
/// * `context` - Description of the conversion context for error reporting
///
/// # Returns
/// * `Ok(f64)` - Successfully converted finite floating-point value
/// * `Err(RiskError)` - Conversion resulted in non-finite value
pub fn quantity_to_f64_safe(quantity: Quantity, context: &str) -> RiskResult<f64> {
    let val = quantity.to_f64();
    if val.is_finite() {
        Ok(val)
    } else {
        Err(RiskError::TypeConversion {
            from_type: "Quantity".to_owned(),
            to_type: "f64".to_owned(),
            reason: format!("Conversion failed for quantity {val} in {context}"),
        })
    }
}

/// Safely converts a Price to Decimal with error handling
///
/// Converts Price to Decimal for precise financial calculations.
/// Provides detailed error information if the conversion fails.
///
/// # Arguments
/// * `price` - The Price value to convert
/// * `context` - Description of the conversion context for error reporting
///
/// # Returns
/// * `Ok(Decimal)` - Successfully converted decimal value
/// * `Err(RiskError)` - Conversion failed with detailed error information
pub fn price_to_decimal_safe(price: Price, context: &str) -> RiskResult<Decimal> {
    price.to_decimal().map_err(|e| RiskError::TypeConversion {
        from_type: "Price".to_owned(),
        to_type: "Decimal".to_owned(),
        reason: format!("Failed to convert price in {context}: {e:?}"),
    })
}

/// Safely converts a Volume to Decimal through f64 intermediate conversion
///
/// Converts Volume to Decimal by first converting to f64 and validating
/// the intermediate result before final Decimal conversion.
///
/// # Arguments
/// * `volume` - The Volume value to convert
/// * `context` - Description of the conversion context for error reporting
///
/// # Returns
/// * `Ok(Decimal)` - Successfully converted decimal value
/// * `Err(RiskError)` - Conversion failed at f64 or Decimal stage
pub fn volume_to_decimal_safe(volume: Volume, context: &str) -> RiskResult<Decimal> {
    let f64_value = volume.to_f64();
    if f64_value.is_finite() {
        f64_to_decimal_safe(f64_value, &format!("Volume conversion in {context}"))
    } else {
        Err(RiskError::TypeConversion {
            from_type: "Volume".to_owned(),
            to_type: "Decimal".to_owned(),
            reason: format!("Volume to f64 conversion failed in {context}"),
        })
    }
}

/// Pass-through function for `PnL` Decimal values with consistent API
///
/// Provides a consistent API for `PnL` decimal handling by passing through
/// the Decimal value unchanged. Maintains API consistency with other
/// conversion functions while avoiding unnecessary conversions.
///
/// # Arguments
/// * `pnl` - The `PnL` Decimal value to pass through
/// * `_context` - Context parameter (unused but maintains API consistency)
///
/// # Returns
/// * `Ok(Decimal)` - The original Decimal value unchanged
pub const fn pnl_to_decimal_safe(pnl: Decimal, _context: &str) -> RiskResult<Decimal> {
    Ok(pnl) // Pass through the Decimal value directly
}

/// Performs safe division with comprehensive validation and zero-checking
///
/// Divides two Price values while protecting against division by zero
/// and ensuring the result is finite and valid for financial calculations.
///
/// # Arguments
/// * `numerator` - The dividend (top number in division)
/// * `denominator` - The divisor (bottom number in division)
/// * `context` - Description of the division context for error reporting
///
/// # Returns
/// * `Ok(Decimal)` - Successfully computed division result
/// * `Err(RiskError)` - Division failed due to zero denominator or non-finite result
///
/// # Safety
/// - Checks for zero denominator before division
/// - Validates result is finite (not NaN or infinite)
/// - Provides detailed error context for debugging
pub fn safe_divide(numerator: Price, denominator: Price, context: &str) -> RiskResult<Decimal> {
    if denominator == Price::ZERO {
        return Err(RiskError::CalculationError(format!(
            "Division by zero in {context}"
        )));
    }

    let result = (numerator / denominator.to_f64())
        .map_err(|e| RiskError::CalculationError(format!("Division failed in {context}: {e:?}")))?;

    // Validate result - safe conversion with proper error handling
    let result_f64 = result.to_f64();
    if !result_f64.is_finite() {
        return Err(RiskError::CalculationError(format!(
            "Division resulted in non-finite value {result_f64} in {context}"
        )));
    }
    // Safe conversion back to Decimal
    FromPrimitive::from_f64(result_f64).ok_or_else(|| {
        RiskError::CalculationError(format!(
            "Failed to convert division result {result_f64} back to Decimal in {context}"
        ))
    })
}

/// Calculates percentage with safe division and automatic scaling
///
/// Computes what percentage `value` represents of `total` using safe division
/// and automatically scales the result to percentage form (0-100).
///
/// # Arguments
/// * `value` - The partial amount
/// * `total` - The total amount (100% reference)
/// * `context` - Description of the percentage calculation context
///
/// # Returns
/// * `Ok(Decimal)` - Percentage value (0-100 scale)
/// * `Err(RiskError)` - Calculation failed due to zero total or invalid result
///
/// # Example
/// If value=25 and total=100, returns Ok(25.0) representing 25%
pub fn safe_percentage(value: Price, total: Price, context: &str) -> RiskResult<Decimal> {
    let percentage = safe_divide(
        value,
        total,
        &format!("percentage calculation in {context}"),
    )?;
    Ok(percentage * Decimal::from(100))
}

/// Calculates square root with domain validation
///
/// Computes the square root of a Price value while ensuring the input
/// is non-negative (square root domain validation).
///
/// # Arguments
/// * `value` - The Price value to take square root of
/// * `context` - Description of the calculation context for error reporting
///
/// # Returns
/// * `Ok(Decimal)` - Successfully computed square root
/// * `Err(RiskError)` - Input was negative or conversion failed
///
/// # Domain
/// Only accepts non-negative values (value >= 0)
pub fn safe_sqrt(value: Price, context: &str) -> RiskResult<Decimal> {
    if value < Price::ZERO {
        return Err(RiskError::CalculationError(format!(
            "Square root of negative value {value} in {context}"
        )));
    }

    let f64_value = price_to_f64_safe(value, context)?;
    let sqrt_f64 = f64_value.sqrt();

    f64_to_decimal_safe(sqrt_f64, &format!("square root calculation in {context}"))
}

/// Calculates natural logarithm with domain validation
///
/// Computes the natural logarithm (ln) of a Price value while ensuring
/// the input is positive (logarithm domain validation).
///
/// # Arguments
/// * `value` - The Price value to take natural log of
/// * `context` - Description of the calculation context for error reporting
///
/// # Returns
/// * `Ok(Decimal)` - Successfully computed natural logarithm
/// * `Err(RiskError)` - Input was non-positive or conversion failed
///
/// # Domain
/// Only accepts positive values (value > 0)
pub fn safe_ln(value: Price, context: &str) -> RiskResult<Decimal> {
    if value <= Price::ZERO {
        return Err(RiskError::CalculationError(format!(
            "Natural log of non-positive value {value} in {context}"
        )));
    }

    let f64_value = price_to_f64_safe(value, context)?;
    let ln_f64 = f64_value.ln();

    f64_to_decimal_safe(ln_f64, &format!("natural log calculation in {context}"))
}

/// Calculates exponential function with overflow protection
///
/// Computes e^value while protecting against potential overflow conditions
/// that could result in infinite values.
///
/// # Arguments
/// * `value` - The exponent value
/// * `context` - Description of the calculation context for error reporting
///
/// # Returns
/// * `Ok(Decimal)` - Successfully computed exponential result
/// * `Err(RiskError)` - Input too large (overflow risk) or conversion failed
///
/// # Safety
/// Rejects inputs > 700.0 to prevent overflow conditions
pub fn safe_exp(value: Price, context: &str) -> RiskResult<Decimal> {
    let f64_value = price_to_f64_safe(value, context)?;

    // Check for overflow potential
    if f64_value > 700.0 {
        return Err(RiskError::CalculationError(format!(
            "Exponential overflow risk: exp({value}) in {context}"
        )));
    }

    let exp_f64 = f64_value.exp();
    f64_to_decimal_safe(exp_f64, &format!("exponential calculation in {context}"))
}

/// Calculates power function with domain and overflow validation
///
/// Computes base^exponent while validating the mathematical domain
/// and protecting against overflow conditions.
///
/// # Arguments
/// * `base` - The base value to raise to a power
/// * `exponent` - The power to raise the base to
/// * `context` - Description of the calculation context for error reporting
///
/// # Returns
/// * `Ok(Decimal)` - Successfully computed power result
/// * `Err(RiskError)` - Invalid domain (negative base with fractional exponent) or overflow
///
/// # Domain Restrictions
/// - Negative base with fractional exponent is invalid (would result in complex number)
/// - Result must be finite (not NaN or infinite)
pub fn safe_pow(base: Price, exponent: f64, context: &str) -> RiskResult<Decimal> {
    let base_f64 = price_to_f64_safe(base, context)?;

    if base_f64 < 0.0 && exponent.fract() != 0.0 {
        return Err(RiskError::CalculationError(format!(
            "Fractional power of negative base {base} ^ {exponent} in {context}"
        )));
    }

    let result_f64 = base_f64.powf(exponent);

    if !result_f64.is_finite() {
        return Err(RiskError::CalculationError(format!(
            "Power calculation resulted in non-finite value: {base} ^ {exponent} in {context}"
        )));
    }

    f64_to_decimal_safe(result_f64, &format!("power calculation in {context}"))
}

/// Validates financial amounts with context-aware rules
///
/// Performs comprehensive validation of financial amounts with different
/// rules for production vs test scenarios. Includes range checking and
/// suspicious value detection.
///
/// # Arguments
/// * `amount` - The financial amount to validate
/// * `amount_type` - Description of what this amount represents
/// * `max_value` - Optional maximum allowed value
///
/// # Returns
/// * `Ok(())` - Amount passed validation
/// * `Err(RiskError)` - Validation failed with specific reason
///
/// # Validation Rules
/// - Production: Rejects negative values (except PnL/stress contexts)
/// - Test: Allows negative values for comprehensive testing
/// - Always warns about suspiciously large values (>$1T)
/// - Checks against optional maximum value limit
pub fn validate_financial_amount(
    amount: Price,
    amount_type: &str,
    max_value: Option<Price>,
) -> RiskResult<()> {
    // Allow negative values in test scenarios (for stress testing, PnL calculations)
    // Only enforce strict positivity for production order validation
    #[cfg(not(test))]
    {
        if amount < Price::ZERO
            && !amount_type.contains("test")
            && !amount_type.contains("stress")
            && !amount_type.contains("pnl")
        {
            return Err(RiskError::ValidationError {
                message: format!("{amount_type} cannot be negative: {amount}"),
            });
        }
    }

    if amount == Price::ZERO && !amount_type.contains("test") {
        warn!("Zero {} amount detected", amount_type);
    }

    if let Some(max) = max_value {
        if amount > max {
            return Err(RiskError::SafetyLimitExceeded {
                limit_type: amount_type.to_owned(),
                current: amount.to_string(),
                maximum: max.to_string(),
            });
        }
    }

    // Check for suspiciously large values (potential data corruption)
    let trillion = Decimal::from(1_000_000_000_000_i64);
    let trillion_price = Price::from_decimal(trillion);
    if amount > trillion_price {
        warn!(
            "Suspiciously large {} amount: {} - potential data corruption",
            amount_type, amount
        );
    }

    Ok(())
}

/// Validates percentage values are within 0-100 range
///
/// Ensures percentage values are finite and within the valid
/// 0-100 percentage range for display and calculations.
///
/// # Arguments
/// * `percentage` - The percentage value to validate
/// * `percentage_type` - Description of what this percentage represents
///
/// # Returns
/// * `Ok(())` - Percentage is valid (0-100 and finite)
/// * `Err(RiskError)` - Percentage is invalid (NaN, infinite, or out of range)
pub fn validate_percentage(percentage: f64, percentage_type: &str) -> RiskResult<()> {
    if !percentage.is_finite() {
        return Err(RiskError::ValidationError {
            message: format!("{percentage_type} must be a finite number: {percentage}"),
        });
    }

    if !(0.0..=100.0).contains(&percentage) {
        return Err(RiskError::ValidationError {
            message: format!("{percentage_type} must be between 0 and 100: {percentage}%"),
        });
    }

    Ok(())
}

/// Validates ratio values are within 0-1 range
///
/// Ensures ratio values are finite and within the valid
/// 0-1 range for mathematical calculations and financial ratios.
///
/// # Arguments
/// * `ratio` - The ratio value to validate
/// * `ratio_type` - Description of what this ratio represents
///
/// # Returns
/// * `Ok(())` - Ratio is valid (0-1 and finite)
/// * `Err(RiskError)` - Ratio is invalid (NaN, infinite, or out of range)
pub fn validate_ratio(ratio: f64, ratio_type: &str) -> RiskResult<()> {
    if !ratio.is_finite() {
        return Err(RiskError::ValidationError {
            message: format!("{ratio_type} must be a finite number: {ratio}"),
        });
    }

    if !(0.0..=1.0).contains(&ratio) {
        return Err(RiskError::ValidationError {
            message: format!("{ratio_type} must be between 0 and 1: {ratio}"),
        });
    }

    Ok(())
}

/// Calculates weighted average with comprehensive input validation
///
/// Computes the weighted average of values using corresponding weights,
/// with extensive validation to ensure data integrity and mathematical validity.
///
/// # Arguments
/// * `values` - Array of values to average
/// * `weights` - Corresponding weights for each value (must be non-negative)
/// * `context` - Description of the calculation context for error reporting
///
/// # Returns
/// * `Ok(f64)` - Successfully computed weighted average
/// * `Err(RiskError)` - Validation failed or calculation error
///
/// # Validation
/// - Arrays must have same length
/// - Arrays must not be empty
/// - All values and weights must be finite
/// - All weights must be non-negative
/// - Total weight must be non-zero
///
/// # Formula
/// `weighted_average` = `Σ(value_i` × `weight_i`) / `Σ(weight_i)`
pub fn safe_weighted_average(values: &[f64], weights: &[f64], context: &str) -> RiskResult<f64> {
    if values.len() != weights.len() {
        return Err(RiskError::ValidationError {
            message: format!(
                "Values and weights length mismatch in {}: {} vs {}",
                context,
                values.len(),
                weights.len()
            ),
        });
    }

    if values.is_empty() {
        return Err(RiskError::ValidationError {
            message: format!("Empty values array in {context}"),
        });
    }

    // Validate all values are finite
    for (i, &value) in values.iter().enumerate() {
        if !value.is_finite() {
            return Err(RiskError::ValidationError {
                message: format!("Non-finite value at index {i} in {context}: {value}"),
            });
        }
    }

    for (i, &weight) in weights.iter().enumerate() {
        if !weight.is_finite() || weight < 0.0 {
            return Err(RiskError::ValidationError {
                message: format!("Invalid weight at index {i} in {context}: {weight}"),
            });
        }
    }

    let total_weight: f64 = weights.iter().sum();
    if total_weight == 0.0 {
        return Err(RiskError::ValidationError {
            message: format!("Total weight is zero in {context}"),
        });
    }

    let weighted_sum: f64 = values.iter().zip(weights.iter()).map(|(v, w)| v * w).sum();
    let result = weighted_sum / total_weight;

    if !result.is_finite() {
        return Err(RiskError::CalculationError(format!(
            "Weighted average calculation resulted in non-finite value in {context}"
        )));
    }

    debug!("Weighted average calculated in {}: {}", context, result);
    Ok(result)
}

/// Calculates Pearson correlation coefficient with comprehensive validation
///
/// Computes the linear correlation coefficient between two data series
/// with extensive validation and boundary checking.
///
/// # Arguments
/// * `x` - First data series
/// * `y` - Second data series
/// * `context` - Description of the correlation context for error reporting
///
/// # Returns
/// * `Ok(f64)` - Correlation coefficient clamped to [-1, 1] range
/// * `Err(RiskError)` - Validation failed or calculation error
///
/// # Validation
/// - Arrays must have same length
/// - Must have at least 2 data points
/// - All values must be finite
/// - Neither series can have zero variance
/// - Result must be within [-1, 1] bounds
///
/// # Formula
/// r = `Σ((x_i` - `x̄)(y_i` - ȳ)) / √(`Σ(x_i` - x̄)² × `Σ(y_i` - ȳ)²)
pub fn safe_correlation(x: &[f64], y: &[f64], context: &str) -> RiskResult<f64> {
    if x.len() != y.len() {
        return Err(RiskError::ValidationError {
            message: format!(
                "Array length mismatch in correlation calculation for {}: {} vs {}",
                context,
                x.len(),
                y.len()
            ),
        });
    }

    if x.len() < 2 {
        return Err(RiskError::ValidationError {
            message: format!("Insufficient data for correlation calculation in {}: need at least 2 points, have {}", context, x.len())
        });
    }

    // Validate all values are finite
    for (i, &val) in x.iter().enumerate() {
        if !val.is_finite() {
            return Err(RiskError::ValidationError {
                message: format!("Non-finite value in x array at index {i} for {context}: {val}"),
            });
        }
    }

    for (i, &val) in y.iter().enumerate() {
        if !val.is_finite() {
            return Err(RiskError::ValidationError {
                message: format!("Non-finite value in y array at index {i} for {context}: {val}"),
            });
        }
    }

    let n = x.len() as f64;
    let mean_x = x.iter().sum::<f64>() / n;
    let mean_y = y.iter().sum::<f64>() / n;

    let mut sum_squared_x = 0.0;
    let mut sum_squared_y = 0.0;
    let mut sum_product_xy = 0.0;

    for (xi, yi) in x.iter().zip(y.iter()) {
        let dx = xi - mean_x;
        let dy = yi - mean_y;
        sum_squared_x += dx * dx;
        sum_squared_y += dy * dy;
        sum_product_xy += dx * dy;
    }

    if sum_squared_x == 0.0 || sum_squared_y == 0.0 {
        return Err(RiskError::ValidationError {
            message: format!("Zero variance in correlation calculation for {context}"),
        });
    }

    let correlation = sum_product_xy / (sum_squared_x * sum_squared_y).sqrt();

    if !correlation.is_finite() {
        return Err(RiskError::CalculationError(format!(
            "Correlation calculation resulted in non-finite value for {context}"
        )));
    }

    // Correlation should be between -1 and 1
    if !(-1.01..=1.01).contains(&correlation) {
        // Allow small numerical errors
        return Err(RiskError::CalculationError(format!(
            "Correlation out of bounds for {context}: {correlation}"
        )));
    }

    Ok(correlation.max(-1.0).min(1.0)) // Clamp to valid range
}

#[cfg(test)]
#[allow(clippy::assertions_on_result_states)]
mod tests {
    use super::*;

    #[test]
    fn test_safe_divide() {
        let result = safe_divide(Decimal::from(10).into(), Decimal::from(2).into(), "test");
        assert!(result.is_ok());
        if let Ok(value) = result {
            assert_eq!(value, Decimal::from(5));
        }

        let error_result = safe_divide(Decimal::from(10).into(), Price::ZERO, "test");
        assert!(error_result.is_err());
    }

    #[test]
    fn test_validate_financial_amount() -> Result<(), Box<dyn std::error::Error>> {
        // Test valid positive amount
        assert!(validate_financial_amount(Price::from_f64(1000.0)?, "test_amount", None).is_ok());

        // Test that creating a negative price fails at construction time
        assert!(Price::from_f64(-100.0).is_err());

        // Test that amount exceeding max value is rejected
        assert!(validate_financial_amount(
            Price::from_f64(1000.0)?,
            "test_amount",
            Some(Price::from_f64(500.0)?)
        )
        .is_err());

        Ok(())
    }

    #[test]
    fn test_safe_weighted_average() {
        let values = vec![10.0, 20.0, 30.0];
        let weights = vec![1.0, 2.0, 3.0];
        let result = safe_weighted_average(&values, &weights, "test");
        assert!(result.is_ok());
        // Expected: (10*1 + 20*2 + 30*3) / (1+2+3) = 140/6 = 23.333...
        let expected = 140.0 / 6.0;
        if let Ok(value) = result {
            assert!((value - expected).abs() < 0.001);
        }
    }

    #[test]
    fn test_safe_correlation() {
        let x = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let y = vec![2.0, 4.0, 6.0, 8.0, 10.0]; // Perfect positive correlation
        let result = safe_correlation(&x, &y, "test");
        assert!(result.is_ok());
        if let Ok(value) = result {
            assert!((value - 1.0).abs() < 0.001); // Should be very close to 1.0
        }
    }
}
