#[cfg(all(feature = "python", feature = "cuda"))]
use crate::cuda::cuda_available;
#[cfg(all(feature = "python", feature = "cuda"))]
use crate::cuda::moving_averages::CudaEhma;
#[cfg(all(feature = "python", feature = "cuda"))]
use crate::utilities::dlpack_cuda::{make_device_array_py, DeviceArrayF32Py};
#[cfg(feature = "python")]
use numpy::{IntoPyArray, PyArray1};
#[cfg(feature = "python")]
use pyo3::exceptions::PyValueError;
#[cfg(feature = "python")]
use pyo3::prelude::*;
#[cfg(feature = "python")]
use pyo3::types::{PyDict, PyList};

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
use serde::{Deserialize, Serialize};
#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
use wasm_bindgen::prelude::*;

use crate::utilities::data_loader::{source_type, Candles};
use crate::utilities::enums::Kernel;
use crate::utilities::helpers::{
    alloc_with_nan_prefix, detect_best_batch_kernel, detect_best_kernel, init_matrix_prefixes,
    make_uninit_matrix,
};
#[cfg(feature = "python")]
use crate::utilities::kernel_validation::validate_kernel;
use aligned_vec::{AVec, CACHELINE_ALIGN};

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
use core::arch::x86_64::*;

#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;

use std::convert::AsRef;
use std::error::Error;
use std::mem::MaybeUninit;
use thiserror::Error;

impl<'a> AsRef<[f64]> for EhmaInput<'a> {
    #[inline(always)]
    fn as_ref(&self) -> &[f64] {
        match &self.data {
            EhmaData::Slice(slice) => slice,
            EhmaData::Candles { candles, source } => source_type(candles, source),
        }
    }
}

#[derive(Debug, Clone)]
pub enum EhmaData<'a> {
    Candles {
        candles: &'a Candles,
        source: &'a str,
    },
    Slice(&'a [f64]),
}

#[derive(Debug, Clone)]
pub struct EhmaOutput {
    pub values: Vec<f64>,
}

#[derive(Debug, Clone)]
#[cfg_attr(
    all(target_arch = "wasm32", feature = "wasm"),
    derive(Serialize, Deserialize)
)]
pub struct EhmaParams {
    pub period: Option<usize>,
}

impl Default for EhmaParams {
    fn default() -> Self {
        Self { period: Some(14) }
    }
}

#[derive(Debug, Clone)]
pub struct EhmaInput<'a> {
    pub data: EhmaData<'a>,
    pub params: EhmaParams,
}

impl<'a> EhmaInput<'a> {
    #[inline]
    pub fn from_candles(c: &'a Candles, s: &'a str, p: EhmaParams) -> Self {
        Self {
            data: EhmaData::Candles {
                candles: c,
                source: s,
            },
            params: p,
        }
    }

    #[inline]
    pub fn from_slice(sl: &'a [f64], p: EhmaParams) -> Self {
        Self {
            data: EhmaData::Slice(sl),
            params: p,
        }
    }

    #[inline]
    pub fn with_default_candles(c: &'a Candles) -> Self {
        Self::from_candles(c, "close", EhmaParams::default())
    }

    #[inline]
    pub fn get_period(&self) -> usize {
        self.params.period.unwrap_or(14)
    }
}

#[derive(Copy, Clone, Debug)]
pub struct EhmaBuilder {
    period: Option<usize>,
    kernel: Kernel,
}

impl Default for EhmaBuilder {
    fn default() -> Self {
        Self {
            period: None,
            kernel: Kernel::Auto,
        }
    }
}

impl EhmaBuilder {
    #[inline(always)]
    pub fn new() -> Self {
        Self::default()
    }

    #[inline(always)]
    pub fn period(mut self, n: usize) -> Self {
        self.period = Some(n);
        self
    }

    #[inline(always)]
    pub fn kernel(mut self, k: Kernel) -> Self {
        self.kernel = k;
        self
    }

    #[inline(always)]
    pub fn apply(self, c: &Candles) -> Result<EhmaOutput, EhmaError> {
        let p = EhmaParams {
            period: self.period,
        };
        let i = EhmaInput::from_candles(c, "close", p);
        ehma_with_kernel(&i, self.kernel)
    }

    #[inline(always)]
    pub fn apply_slice(self, d: &[f64]) -> Result<EhmaOutput, EhmaError> {
        let p = EhmaParams {
            period: self.period,
        };
        let i = EhmaInput::from_slice(d, p);
        ehma_with_kernel(&i, self.kernel)
    }

    #[inline(always)]
    pub fn into_stream(self) -> Result<EhmaStream, EhmaError> {
        let p = EhmaParams {
            period: self.period,
        };
        EhmaStream::try_new(p)
    }
}

#[derive(Debug, Error)]
pub enum EhmaError {
    #[error("ehma: Input data slice is empty.")]
    EmptyInputData,

    #[error("ehma: All values are NaN.")]
    AllValuesNaN,

    #[error("ehma: Invalid period: period = {period}, data length = {data_len}")]
    InvalidPeriod { period: usize, data_len: usize },

    #[error("ehma: Not enough valid data: needed = {needed}, valid = {valid}")]
    NotEnoughValidData { needed: usize, valid: usize },

    #[error("ehma: Output slice length mismatch: expected = {expected}, got = {got}")]
    OutputLengthMismatch { expected: usize, got: usize },

    #[error("ehma: Invalid range: start = {start}, end = {end}, step = {step}")]
    InvalidRange {
        start: usize,
        end: usize,
        step: usize,
    },

    #[error("ehma: Invalid kernel for batch API: {0:?}")]
    InvalidKernelForBatch(Kernel),

    #[error("ehma: size overflow while computing {what}")]
    SizeOverflow { what: &'static str },
}

#[inline]
pub fn ehma(input: &EhmaInput) -> Result<EhmaOutput, EhmaError> {
    ehma_with_kernel(input, Kernel::Auto)
}

#[inline(always)]
fn ehma_prepare<'a>(
    input: &'a EhmaInput,
    kernel: Kernel,
) -> Result<(&'a [f64], AVec<f64>, usize, usize, f64, Kernel), EhmaError> {
    let data: &[f64] = input.as_ref();
    let len = data.len();
    if len == 0 {
        return Err(EhmaError::EmptyInputData);
    }
    let first = data
        .iter()
        .position(|x| !x.is_nan())
        .ok_or(EhmaError::AllValuesNaN)?;
    let period = input.get_period();

    if period == 0 || period > len {
        return Err(EhmaError::InvalidPeriod {
            period,
            data_len: len,
        });
    }
    if len - first < period {
        return Err(EhmaError::NotEnoughValidData {
            needed: period,
            valid: len - first,
        });
    }

    let (weights, inv_coef) = build_hann_weights_rec(period);

    let chosen = match kernel {
        Kernel::Auto => match detect_best_kernel() {
            Kernel::Avx512 => Kernel::Avx2,
            k => k,
        },
        k => k,
    };

    Ok((data, weights, period, first, inv_coef, chosen))
}

#[inline(always)]
fn build_hann_weights_rec(period: usize) -> (AVec<f64>, f64) {
    use std::f64::consts::PI;
    let mut w = AVec::<f64>::with_capacity(CACHELINE_ALIGN, period);
    w.resize(period, 0.0);

    let omega = 2.0 * PI / (period as f64 + 1.0);
    let (sin_w, cos_w) = omega.sin_cos();

    let mut cm = cos_w;
    let mut sm = sin_w;
    for j in 0..period {
        w[j] = 1.0 - cm;

        let next_cm = cm * cos_w - sm * sin_w;
        let next_sm = sm * cos_w + cm * sin_w;
        cm = next_cm;
        sm = next_sm;
    }

    let inv = 1.0 / (period as f64 + 1.0);
    (w, inv)
}

pub fn ehma_with_kernel(input: &EhmaInput, kernel: Kernel) -> Result<EhmaOutput, EhmaError> {
    let (data, weights, period, first, inv_coef, chosen) = ehma_prepare(input, kernel)?;

    let mut out = alloc_with_nan_prefix(data.len(), first + period - 1);

    ehma_compute_into(data, &weights, period, first, inv_coef, chosen, &mut out);

    Ok(EhmaOutput { values: out })
}

#[inline]
pub fn ehma_into_slice(dst: &mut [f64], input: &EhmaInput, kern: Kernel) -> Result<(), EhmaError> {
    let (data, weights, period, first, inv_coef, chosen) = ehma_prepare(input, kern)?;

    if dst.len() != data.len() {
        return Err(EhmaError::OutputLengthMismatch {
            expected: data.len(),
            got: dst.len(),
        });
    }

    ehma_compute_into(data, &weights, period, first, inv_coef, chosen, dst);

    let warmup_end = first + period - 1;
    for v in &mut dst[..warmup_end] {
        *v = f64::NAN;
    }

    Ok(())
}

#[cfg(not(all(target_arch = "wasm32", feature = "wasm")))]
#[inline]
pub fn ehma_into(input: &EhmaInput, out: &mut [f64]) -> Result<(), EhmaError> {
    let (data, weights, period, first, inv_coef, chosen) = ehma_prepare(input, Kernel::Auto)?;

    if out.len() != data.len() {
        return Err(EhmaError::OutputLengthMismatch {
            expected: data.len(),
            got: out.len(),
        });
    }

    let warmup_end = first + period - 1;
    let qnan = f64::from_bits(0x7ff8_0000_0000_0000);
    let warm = warmup_end.min(out.len());
    for v in &mut out[..warm] {
        *v = qnan;
    }

    ehma_compute_into(data, &weights, period, first, inv_coef, chosen, out);

    Ok(())
}

#[inline(always)]
fn ehma_compute_into(
    data: &[f64],
    weights: &[f64],
    period: usize,
    first: usize,
    inv_coef: f64,
    kernel: Kernel,
    out: &mut [f64],
) {
    unsafe {
        #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
        {
            if matches!(kernel, Kernel::Scalar | Kernel::ScalarBatch) {
                ehma_simd128(data, weights, period, first, inv_coef, out);
                return;
            }
        }

        #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
        {
            unsafe { ehma_simd128(data, weights, period, first, inv_coef, out) }
        }

        #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
        {
            match kernel {
                Kernel::Scalar | Kernel::ScalarBatch => {
                    ehma_scalar(data, weights, period, first, inv_coef, out)
                }
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                Kernel::Avx2 | Kernel::Avx2Batch => unsafe {
                    ehma_avx2(data, weights, period, first, inv_coef, out)
                },
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                Kernel::Avx512 | Kernel::Avx512Batch => {
                    ehma_avx512(data, weights, period, first, inv_coef, out)
                }
                #[cfg(not(all(feature = "nightly-avx", target_arch = "x86_64")))]
                Kernel::Avx2 | Kernel::Avx2Batch | Kernel::Avx512 | Kernel::Avx512Batch => {
                    ehma_scalar(data, weights, period, first, inv_coef, out)
                }
                _ => unreachable!(),
            }
        }
    }
}

#[inline]
pub fn ehma_scalar(
    data: &[f64],
    weights: &[f64],
    period: usize,
    first_val: usize,
    inv_coef: f64,
    out: &mut [f64],
) {
    assert_eq!(weights.len(), period, "weights.len() must equal `period`");
    assert!(
        out.len() >= data.len(),
        "`out` must be at least as long as `data`"
    );

    for i in (first_val + period - 1)..data.len() {
        let start = i + 1 - period;
        let window = &data[start..start + period];

        let mut sum = 0.0;

        for j in 0..period {
            sum = window[j].mul_add(weights[period - 1 - j], sum);
        }

        out[i] = sum * inv_coef;
    }
}

#[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
#[inline]
unsafe fn ehma_simd128(
    data: &[f64],
    weights: &[f64],
    period: usize,
    first_val: usize,
    inv_coef: f64,
    out: &mut [f64],
) {
    use core::arch::wasm32::*;

    assert_eq!(weights.len(), period, "weights.len() must equal `period`");
    assert!(
        out.len() >= data.len(),
        "`out` must be at least as long as `data`"
    );

    const STEP: usize = 2;
    let chunks = period / STEP;
    let tail = period % STEP;

    for i in (first_val + period - 1)..data.len() {
        let start = i + 1 - period;
        let mut sum = 0.0;

        for j in 0..period {
            sum += data[start + j] * weights[period - 1 - j];
        }

        out[i] = sum * inv_coef;
    }
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline]
#[target_feature(enable = "avx2,fma")]
unsafe fn ehma_avx2(
    data: &[f64],
    weights: &[f64],
    period: usize,
    first_val: usize,
    inv_coef: f64,
    out: &mut [f64],
) {
    let p4 = period & !3;

    for i in (first_val + period - 1)..data.len() {
        let start = i + 1 - period;
        let window = &data[start..start + period];

        let mut acc = _mm256_setzero_pd();

        let mut j = 0usize;
        while j < p4 {
            let d = _mm256_loadu_pd(window.as_ptr().add(j));

            let w_block = _mm256_loadu_pd(weights.as_ptr().add(period - 4 - j));

            let w = _mm256_permute4x64_pd(w_block, 0b0001_1011);

            acc = _mm256_fmadd_pd(d, w, acc);
            j += 4;
        }

        let hi = _mm256_extractf128_pd(acc, 1);
        let lo = _mm256_castpd256_pd128(acc);
        let sum128 = _mm_add_pd(hi, lo);
        let sum64 = _mm_hadd_pd(sum128, sum128);
        let mut sum = _mm_cvtsd_f64(sum64);

        while j < period {
            let d = *window.get_unchecked(j);
            let w = *weights.get_unchecked(period - 1 - j);
            sum = d.mul_add(w, sum);
            j += 1;
        }

        out[i] = sum * inv_coef;
    }
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline]
#[target_feature(enable = "avx512f")]
pub fn ehma_avx512(
    data: &[f64],
    weights: &[f64],
    period: usize,
    first_val: usize,
    inv_coef: f64,
    out: &mut [f64],
) {
    unsafe { ehma_avx512_impl(data, weights, period, first_val, inv_coef, out) }
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline]
#[target_feature(enable = "avx512f")]
unsafe fn ehma_avx512_impl(
    data: &[f64],
    weights: &[f64],
    period: usize,
    first_val: usize,
    inv_coef: f64,
    out: &mut [f64],
) {
    let p8 = period & !7;

    let rev_idx: __m512i = _mm512_set_epi64(0, 1, 2, 3, 4, 5, 6, 7);

    for i in (first_val + period - 1)..data.len() {
        let start = i + 1 - period;
        let window = &data[start..start + period];

        let mut acc = _mm512_setzero_pd();

        let mut j = 0usize;
        while j < p8 {
            let dv = _mm512_loadu_pd(window.as_ptr().add(j));

            let w_block = _mm512_loadu_pd(weights.as_ptr().add(period - 8 - j));

            let w = _mm512_permutexvar_pd(rev_idx, w_block);

            acc = _mm512_fmadd_pd(dv, w, acc);
            j += 8;
        }

        let mut sum = _mm512_reduce_add_pd(acc);

        while j < period {
            let d = *window.get_unchecked(j);
            let w = *weights.get_unchecked(period - 1 - j);
            sum = d.mul_add(w, sum);
            j += 1;
        }

        out[i] = sum * inv_coef;
    }
}

#[derive(Debug, Clone)]
pub struct EhmaStream {
    period: usize,

    buffer: Vec<f64>,
    head: usize,
    filled: bool,

    sum_x: f64,
    z_re: f64,
    z_im: f64,

    inv_coef: f64,
    omega: f64,
    cos_w: f64,
    sin_w: f64,
    cos_wp: f64,
    sin_wp: f64,
}

impl EhmaStream {
    pub fn try_new(params: EhmaParams) -> Result<Self, EhmaError> {
        let period = params.period.unwrap_or(14);
        if period == 0 {
            return Err(EhmaError::InvalidPeriod {
                period,
                data_len: 0,
            });
        }

        use std::f64::consts::PI;

        let omega = 2.0 * PI / (period as f64 + 1.0);
        let (sin_w, cos_w) = omega.sin_cos();
        let (sin_wp, cos_wp) = (omega * period as f64).sin_cos();

        let inv_coef = 1.0 / (period as f64 + 1.0);

        Ok(Self {
            period,
            buffer: vec![f64::NAN; period],
            head: 0,
            filled: false,

            sum_x: 0.0,
            z_re: 0.0,
            z_im: 0.0,

            inv_coef,
            omega,
            cos_w,
            sin_w,
            cos_wp,
            sin_wp,
        })
    }

    #[inline(always)]
    fn recompute_full(&mut self) -> Option<f64> {
        let mut sx = 0.0;
        let mut zr = 0.0;
        let mut zi = 0.0;

        let mut cm = self.cos_w;
        let mut sm = self.sin_w;

        let mut idx = self.head;
        for _m in 1..=self.period {
            let x = self.buffer[idx];
            if !x.is_finite() {
                self.sum_x = f64::NAN;
                self.z_re = f64::NAN;
                self.z_im = f64::NAN;
                return Some(f64::NAN);
            }
            sx += x;
            zr = x.mul_add(cm, zr);
            zi = x.mul_add(sm, zi);

            let next_cm = cm * self.cos_w - sm * self.sin_w;
            let next_sm = sm * self.cos_w + cm * self.sin_w;
            cm = next_cm;
            sm = next_sm;

            idx = (idx + 1) % self.period;
        }

        self.sum_x = sx;
        self.z_re = zr;
        self.z_im = zi;
        Some((sx - zr) * self.inv_coef)
    }

    #[inline(always)]
    pub fn update(&mut self, value: f64) -> Option<f64> {
        let old = self.buffer[self.head];
        self.buffer[self.head] = value;
        self.head = (self.head + 1) % self.period;

        if !self.filled {
            if self.head == 0 {
                self.filled = true;
                return self.recompute_full();
            } else {
                return None;
            }
        }

        if !self.sum_x.is_finite()
            || !self.z_re.is_finite()
            || !self.z_im.is_finite()
            || !old.is_finite()
            || !value.is_finite()
        {
            return self.recompute_full();
        }

        self.sum_x += value - old;

        let zr_rot = self.z_re.mul_add(self.cos_w, self.z_im * self.sin_w);
        let zi_rot = self.z_im.mul_add(self.cos_w, -self.z_re * self.sin_w);

        self.z_re = (zr_rot - old) + self.cos_wp * value;
        self.z_im = zi_rot + self.sin_wp * value;

        Some((self.sum_x - self.z_re) * self.inv_coef)
    }
}

#[derive(Clone, Debug)]
pub struct EhmaBatchRange {
    pub period: (usize, usize, usize),
}

impl Default for EhmaBatchRange {
    fn default() -> Self {
        Self {
            period: (14, 263, 1),
        }
    }
}

#[derive(Clone, Debug, Default)]
pub struct EhmaBatchBuilder {
    range: EhmaBatchRange,
    kernel: Kernel,
}

impl EhmaBatchBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn kernel(mut self, k: Kernel) -> Self {
        self.kernel = k;
        self
    }

    #[inline]
    pub fn period_range(mut self, start: usize, end: usize, step: usize) -> Self {
        self.range.period = (start, end, step);
        self
    }

    #[inline]
    pub fn period_static(mut self, p: usize) -> Self {
        self.range.period = (p, p, 0);
        self
    }

    pub fn apply_slice(self, data: &[f64]) -> Result<EhmaBatchOutput, EhmaError> {
        ehma_batch_with_kernel_slice(data, &self.range, self.kernel)
    }

    pub fn apply_candles(self, c: &Candles, src: &str) -> Result<EhmaBatchOutput, EhmaError> {
        let slice = source_type(c, src);
        self.apply_slice(slice)
    }

    pub fn with_default_candles(c: &Candles) -> Result<EhmaBatchOutput, EhmaError> {
        EhmaBatchBuilder::new()
            .kernel(Kernel::Auto)
            .apply_candles(c, "close")
    }

    pub fn with_default_slice(data: &[f64], k: Kernel) -> Result<EhmaBatchOutput, EhmaError> {
        EhmaBatchBuilder::new().kernel(k).apply_slice(data)
    }
}

#[derive(Clone, Debug)]
pub struct EhmaBatchOutput {
    pub values: Vec<f64>,
    pub combos: Vec<EhmaParams>,
    pub rows: usize,
    pub cols: usize,
}

impl EhmaBatchOutput {
    pub fn row_for_params(&self, p: &EhmaParams) -> Option<usize> {
        self.combos
            .iter()
            .position(|c| c.period.unwrap_or(14) == p.period.unwrap_or(14))
    }

    pub fn values_for(&self, p: &EhmaParams) -> Option<&[f64]> {
        self.row_for_params(p).map(|row| {
            let start = row * self.cols;
            &self.values[start..start + self.cols]
        })
    }
}

#[inline(always)]
pub fn expand_grid(r: &EhmaBatchRange) -> Vec<EhmaParams> {
    let (start, end, step) = r.period;

    if step == 0 {
        return vec![EhmaParams {
            period: Some(start),
        }];
    }
    let (lo, hi) = if start <= end {
        (start, end)
    } else {
        (end, start)
    };
    let mut out = Vec::new();
    let mut p = lo;
    loop {
        out.push(EhmaParams { period: Some(p) });
        if p == hi {
            break;
        }
        match p.checked_add(step) {
            Some(next) if next > p && next <= hi => {
                p = next;
            }
            _ => {
                break;
            }
        }
    }
    out
}

#[inline(always)]
pub fn ehma_batch_slice(
    data: &[f64],
    sweep: &EhmaBatchRange,
    kern: Kernel,
) -> Result<EhmaBatchOutput, EhmaError> {
    ehma_batch_inner(data, sweep, kern, false)
}

#[inline(always)]
pub fn ehma_batch_par_slice(
    data: &[f64],
    sweep: &EhmaBatchRange,
    kern: Kernel,
) -> Result<EhmaBatchOutput, EhmaError> {
    ehma_batch_inner(data, sweep, kern, true)
}

pub fn ehma_batch_with_kernel(
    data: &[f64],
    sweep: &EhmaBatchRange,
    k: Kernel,
) -> Result<EhmaBatchOutput, EhmaError> {
    let kernel = match k {
        Kernel::Auto => match detect_best_batch_kernel() {
            Kernel::Avx512Batch => Kernel::Avx2Batch,
            other => other,
        },
        other if other.is_batch() => other,
        other => return Err(EhmaError::InvalidKernelForBatch(other)),
    };
    let simd = match kernel {
        Kernel::Avx512Batch => Kernel::Avx512,
        Kernel::Avx2Batch => Kernel::Avx2,
        Kernel::ScalarBatch => Kernel::Scalar,
        _ => unreachable!(),
    };
    ehma_batch_inner(data, sweep, simd, true)
}

pub fn ehma_batch_with_kernel_slice(
    data: &[f64],
    sweep: &EhmaBatchRange,
    k: Kernel,
) -> Result<EhmaBatchOutput, EhmaError> {
    ehma_batch_with_kernel(data, sweep, k)
}

#[inline(always)]
fn ehma_batch_inner(
    data: &[f64],
    sweep: &EhmaBatchRange,
    kern: Kernel,
    parallel: bool,
) -> Result<EhmaBatchOutput, EhmaError> {
    let combos = expand_grid(sweep);
    if combos.is_empty() {
        let (s, e, t) = sweep.period;
        return Err(EhmaError::InvalidRange {
            start: s,
            end: e,
            step: t,
        });
    }

    let cols = data.len();
    if cols == 0 {
        return Err(EhmaError::EmptyInputData);
    }

    let first = data
        .iter()
        .position(|x| !x.is_nan())
        .ok_or(EhmaError::AllValuesNaN)?;
    let max_p = combos.iter().map(|c| c.period.unwrap()).max().unwrap();
    if cols - first < max_p {
        return Err(EhmaError::NotEnoughValidData {
            needed: max_p,
            valid: cols - first,
        });
    }

    let rows = combos.len();
    let _ = rows
        .checked_mul(cols)
        .ok_or(EhmaError::SizeOverflow { what: "rows*cols" })?;
    let mut buf_mu = make_uninit_matrix(rows, cols);
    let warm: Vec<usize> = combos
        .iter()
        .map(|c| first + c.period.unwrap() - 1)
        .collect();
    init_matrix_prefixes(&mut buf_mu, cols, &warm);

    let mut guard = core::mem::ManuallyDrop::new(buf_mu);
    let out: &mut [f64] =
        unsafe { core::slice::from_raw_parts_mut(guard.as_mut_ptr() as *mut f64, guard.len()) };

    let do_row = |row: usize, row_dst: &mut [f64]| {
        let period = combos[row].period.unwrap();

        let (w, inv) = build_hann_weights_rec(period);

        unsafe { ehma_compute_into(data, &w, period, first, inv, kern, row_dst) };
    };

    if parallel {
        #[cfg(not(target_arch = "wasm32"))]
        {
            out.par_chunks_mut(cols)
                .enumerate()
                .for_each(|(row, dst)| do_row(row, dst));
        }
        #[cfg(target_arch = "wasm32")]
        {
            for (row, dst) in out.chunks_mut(cols).enumerate() {
                do_row(row, dst);
            }
        }
    } else {
        for (row, dst) in out.chunks_mut(cols).enumerate() {
            do_row(row, dst);
        }
    }

    let values = unsafe {
        Vec::from_raw_parts(
            guard.as_mut_ptr() as *mut f64,
            guard.len(),
            guard.capacity(),
        )
    };
    Ok(EhmaBatchOutput {
        values,
        combos: combos.clone(),
        rows: combos.len(),
        cols,
    })
}

#[inline]
fn round_up8(x: usize) -> usize {
    (x + 7) & !7
}

#[inline(always)]
pub fn ehma_batch_inner_into(
    data: &[f64],
    sweep: &EhmaBatchRange,
    kern: Kernel,
    parallel: bool,
    out: &mut [f64],
) -> Result<Vec<EhmaParams>, EhmaError> {
    let combos = expand_grid(sweep);
    if combos.is_empty() {
        let (s, e, t) = sweep.period;
        return Err(EhmaError::InvalidRange {
            start: s,
            end: e,
            step: t,
        });
    }

    let cols = data.len();
    if cols == 0 {
        return Err(EhmaError::EmptyInputData);
    }
    let rows = combos.len();
    let expected = rows
        .checked_mul(cols)
        .ok_or(EhmaError::SizeOverflow { what: "rows*cols" })?;
    if out.len() != expected {
        return Err(EhmaError::OutputLengthMismatch {
            expected,
            got: out.len(),
        });
    }

    let first = data
        .iter()
        .position(|x| !x.is_nan())
        .ok_or(EhmaError::AllValuesNaN)?;
    let max_p = combos
        .iter()
        .map(|c| round_up8(c.period.unwrap()))
        .max()
        .unwrap();
    if cols - first < combos.iter().map(|c| c.period.unwrap()).max().unwrap() {
        return Err(EhmaError::NotEnoughValidData {
            needed: combos.iter().map(|c| c.period.unwrap()).max().unwrap(),
            valid: cols - first,
        });
    }

    let out_mu: &mut [MaybeUninit<f64>] = unsafe {
        core::slice::from_raw_parts_mut(out.as_mut_ptr() as *mut MaybeUninit<f64>, out.len())
    };
    let warm: Vec<usize> = combos
        .iter()
        .map(|c| first + c.period.unwrap() - 1)
        .collect();
    init_matrix_prefixes(out_mu, cols, &warm);

    let cap = rows.checked_mul(max_p).ok_or(EhmaError::SizeOverflow {
        what: "rows*max_period",
    })?;
    let mut flat_w = AVec::<f64>::with_capacity(CACHELINE_ALIGN, cap);
    flat_w.resize(cap, 0.0);
    let mut inv_norms = vec![0.0f64; rows];

    for (row, prm) in combos.iter().enumerate() {
        let p = prm.period.unwrap();
        let base = row * max_p;

        let omega = std::f64::consts::PI * 2.0 / (p as f64 + 1.0);
        let (sin_w, cos_w) = omega.sin_cos();
        let (mut cm, mut sm) = (cos_w, sin_w);
        for j in 0..p {
            flat_w[base + j] = 1.0 - cm;
            let next_cm = cm * cos_w - sm * sin_w;
            let next_sm = sm * cos_w + cm * sin_w;
            cm = next_cm;
            sm = next_sm;
        }
        inv_norms[row] = 1.0 / (p as f64 + 1.0);
    }

    unsafe fn ehma_row_scalar_ptr(
        data: &[f64],
        first: usize,
        period: usize,
        w_ptr: *const f64,
        inv: f64,
        out: &mut [f64],
    ) {
        let p4 = period & !3;
        for i in (first + period - 1)..data.len() {
            let start = i + 1 - period;
            let window = &data[start..start + period];

            let mut sum = 0.0;

            for k in (0..p4).step_by(4) {
                let w = std::slice::from_raw_parts(w_ptr.add(k), 4);
                let d = &window[k..k + 4];
                sum += d[0] * w[0] + d[1] * w[1] + d[2] * w[2] + d[3] * w[3];
            }
            for k in p4..period {
                sum += window[k] * *w_ptr.add(k);
            }
            out[i] = sum * inv;
        }
    }

    let do_row = |row: usize, row_mu: &mut [MaybeUninit<f64>]| unsafe {
        let p = combos[row].period.unwrap();
        let inv = inv_norms[row];
        let w_ptr = flat_w.as_ptr().add(row * max_p);
        let row_out =
            core::slice::from_raw_parts_mut(row_mu.as_mut_ptr() as *mut f64, row_mu.len());

        #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
        {
            let w_slice = core::slice::from_raw_parts(w_ptr, p);
            ehma_simd128(data, w_slice, p, first, inv, row_out);
        }

        #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
        {
            match kern {
                Kernel::Auto | Kernel::Scalar | Kernel::ScalarBatch => {
                    ehma_row_scalar_ptr(data, first, p, w_ptr, inv, row_out)
                }
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                Kernel::Avx2 | Kernel::Avx2Batch => {
                    let w_slice = core::slice::from_raw_parts(w_ptr, p);
                    ehma_avx2(data, w_slice, p, first, inv, row_out);
                }
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                Kernel::Avx512 | Kernel::Avx512Batch => {
                    let w_slice = core::slice::from_raw_parts(w_ptr, p);
                    ehma_avx512(data, w_slice, p, first, inv, row_out);
                }
                #[cfg(not(all(feature = "nightly-avx", target_arch = "x86_64")))]
                Kernel::Avx2 | Kernel::Avx2Batch | Kernel::Avx512 | Kernel::Avx512Batch => {
                    ehma_row_scalar_ptr(data, first, p, w_ptr, inv, row_out)
                }
            }
        }
    };

    if parallel {
        #[cfg(not(target_arch = "wasm32"))]
        {
            use rayon::prelude::*;
            out_mu
                .par_chunks_mut(cols)
                .enumerate()
                .for_each(|(r, ch)| do_row(r, ch));
        }
        #[cfg(target_arch = "wasm32")]
        for (r, ch) in out_mu.chunks_mut(cols).enumerate() {
            do_row(r, ch);
        }
    } else {
        for (r, ch) in out_mu.chunks_mut(cols).enumerate() {
            do_row(r, ch);
        }
    }

    Ok(combos)
}

#[cfg(feature = "python")]
#[pyfunction(name = "ehma")]
#[pyo3(signature = (data, period, kernel=None))]
pub fn ehma_py<'py>(
    py: Python<'py>,
    data: numpy::PyReadonlyArray1<'py, f64>,
    period: usize,
    kernel: Option<&str>,
) -> PyResult<Bound<'py, numpy::PyArray1<f64>>> {
    use numpy::{IntoPyArray, PyArray1, PyArrayMethods};

    let kern = validate_kernel(kernel, false)?;
    let params = EhmaParams {
        period: Some(period),
    };

    let result_vec: Vec<f64> = if let Ok(slice_in) = data.as_slice() {
        let input = EhmaInput::from_slice(slice_in, params);
        py.allow_threads(|| ehma_with_kernel(&input, kern).map(|o| o.values))
            .map_err(|e| PyValueError::new_err(e.to_string()))?
    } else {
        let owned = data.as_array().to_owned();
        let slice_in = owned
            .as_slice()
            .expect("owned numpy array should be contiguous");
        let input = EhmaInput::from_slice(slice_in, params);
        py.allow_threads(|| ehma_with_kernel(&input, kern).map(|o| o.values))
            .map_err(|e| PyValueError::new_err(e.to_string()))?
    };

    Ok(result_vec.into_pyarray(py))
}

#[cfg(feature = "python")]
#[pyfunction(name = "ehma_batch")]
#[pyo3(signature = (data, period_range, kernel=None))]
pub fn ehma_batch_py<'py>(
    py: Python<'py>,
    data: numpy::PyReadonlyArray1<'py, f64>,
    period_range: (usize, usize, usize),
    kernel: Option<&str>,
) -> PyResult<Bound<'py, pyo3::types::PyDict>> {
    use numpy::{IntoPyArray, PyArray1, PyArrayMethods};
    let sweep = EhmaBatchRange {
        period: period_range,
    };

    let combos = expand_grid(&sweep);
    let rows = combos.len();

    let (slice_in, owned_opt);
    let cols: usize;
    if let Ok(s) = data.as_slice() {
        slice_in = s;
        owned_opt = None::<Vec<f64>>;
        cols = slice_in.len();
    } else {
        let owned = data.as_array().to_owned();
        cols = owned.len();

        owned_opt = Some(owned.into_raw_vec());

        slice_in = owned_opt.as_ref().unwrap().as_slice();
    }

    let out_arr = unsafe { PyArray1::<f64>::new(py, [rows * cols], false) };
    let slice_out = unsafe { out_arr.as_slice_mut()? };

    let kern = validate_kernel(kernel, true)?;
    let combos = py
        .allow_threads(|| {
            let batch = match kern {
                Kernel::Auto => match detect_best_batch_kernel() {
                    Kernel::Avx512Batch => Kernel::Avx2Batch,
                    other => other,
                },
                k => k,
            };
            let simd = match batch {
                Kernel::Avx512Batch => Kernel::Avx512,
                Kernel::Avx2Batch => Kernel::Avx2,
                Kernel::ScalarBatch => Kernel::Scalar,
                _ => unreachable!(),
            };
            ehma_batch_inner_into(slice_in, &sweep, simd, true, slice_out)
        })
        .map_err(|e| PyValueError::new_err(e.to_string()))?;

    let dict = PyDict::new(py);
    dict.set_item("values", out_arr.reshape((rows, cols))?)?;
    dict.set_item(
        "periods",
        combos
            .iter()
            .map(|p| p.period.unwrap() as u64)
            .collect::<Vec<_>>()
            .into_pyarray(py),
    )?;
    Ok(dict.into())
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyfunction(name = "ehma_cuda_batch_dev")]
#[pyo3(signature = (data, period_range, device_id=0))]
pub fn ehma_cuda_batch_dev_py(
    py: Python<'_>,
    data: numpy::PyReadonlyArray1<'_, f64>,
    period_range: (usize, usize, usize),
    device_id: usize,
) -> PyResult<DeviceArrayF32Py> {
    use numpy::PyArrayMethods;

    if !cuda_available() {
        return Err(PyValueError::new_err("CUDA not available"));
    }

    let slice_in = data.as_slice()?;
    let sweep = EhmaBatchRange {
        period: period_range,
    };
    let data_f32: Vec<f32> = slice_in.iter().map(|&v| v as f32).collect();

    let inner = py.allow_threads(|| {
        let cuda = CudaEhma::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        cuda.ehma_batch_dev(&data_f32, &sweep)
            .map_err(|e| PyValueError::new_err(e.to_string()))
    })?;

    Ok(make_device_array_py(device_id, inner)?)
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyfunction(name = "ehma_cuda_many_series_one_param_dev")]
#[pyo3(signature = (data_tm_f32, period, device_id=0))]
pub fn ehma_cuda_many_series_one_param_dev_py(
    py: Python<'_>,
    data_tm_f32: numpy::PyReadonlyArray2<'_, f32>,
    period: usize,
    device_id: usize,
) -> PyResult<DeviceArrayF32Py> {
    use numpy::PyUntypedArrayMethods;

    if !cuda_available() {
        return Err(PyValueError::new_err("CUDA not available"));
    }

    let flat_in = data_tm_f32.as_slice()?;
    let rows = data_tm_f32.shape()[0];
    let cols = data_tm_f32.shape()[1];
    let params = EhmaParams {
        period: Some(period),
    };

    let inner = py.allow_threads(|| {
        let cuda = CudaEhma::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        cuda.ehma_multi_series_one_param_time_major_dev(flat_in, cols, rows, &params)
            .map_err(|e| PyValueError::new_err(e.to_string()))
    })?;

    Ok(make_device_array_py(device_id, inner)?)
}

#[cfg(feature = "python")]
#[pyclass(name = "EhmaStream")]
pub struct EhmaStreamPy {
    stream: EhmaStream,
}

#[cfg(feature = "python")]
#[pymethods]
impl EhmaStreamPy {
    #[new]
    fn new(period: usize) -> PyResult<Self> {
        let params = EhmaParams {
            period: Some(period),
        };
        Ok(Self {
            stream: EhmaStream::try_new(params)
                .map_err(|e| PyValueError::new_err(e.to_string()))?,
        })
    }

    fn update(&mut self, value: f64) -> Option<f64> {
        self.stream.update(value)
    }
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn ehma_wasm(data: &[f64], period: Option<usize>) -> Result<Vec<f64>, JsValue> {
    let params = EhmaParams { period };
    let input = EhmaInput::from_slice(data, params);

    let mut output = vec![0.0; data.len()];
    ehma_into_slice(&mut output, &input, detect_best_kernel())
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    Ok(output)
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn ehma_alloc(len: usize) -> *mut f64 {
    let mut vec = Vec::<f64>::with_capacity(len);
    let ptr = vec.as_mut_ptr();
    std::mem::forget(vec);
    ptr
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn ehma_free(ptr: *mut f64, len: usize) {
    unsafe {
        let _ = Vec::from_raw_parts(ptr, len, len);
    }
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn ehma_into(
    in_ptr: *const f64,
    out_ptr: *mut f64,
    len: usize,
    period: usize,
) -> Result<(), JsValue> {
    if in_ptr.is_null() || out_ptr.is_null() {
        return Err(JsValue::from_str("null pointer passed to ehma_into"));
    }

    unsafe {
        let data = std::slice::from_raw_parts(in_ptr, len);

        if period == 0 || period > len {
            return Err(JsValue::from_str("Invalid period"));
        }

        let params = EhmaParams {
            period: Some(period),
        };
        let input = EhmaInput::from_slice(data, params);

        if in_ptr == out_ptr {
            let mut temp = vec![0.0; len];
            ehma_into_slice(&mut temp, &input, detect_best_kernel())
                .map_err(|e| JsValue::from_str(&e.to_string()))?;
            let out = std::slice::from_raw_parts_mut(out_ptr, len);
            out.copy_from_slice(&temp);
        } else {
            let out = std::slice::from_raw_parts_mut(out_ptr, len);
            ehma_into_slice(out, &input, detect_best_kernel())
                .map_err(|e| JsValue::from_str(&e.to_string()))?;
        }

        Ok(())
    }
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[derive(Serialize, Deserialize)]
pub struct EhmaBatchConfig {
    pub period_range: (usize, usize, usize),
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[derive(Serialize, Deserialize)]
pub struct EhmaBatchJsOutput {
    pub values: Vec<f64>,
    pub combos: Vec<EhmaParams>,
    pub rows: usize,
    pub cols: usize,
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen(js_name = ehma_batch)]
pub fn ehma_batch_unified_js(data: &[f64], config: JsValue) -> Result<JsValue, JsValue> {
    let cfg: EhmaBatchConfig = serde_wasm_bindgen::from_value(config)
        .map_err(|e| JsValue::from_str(&format!("Invalid config: {}", e)))?;
    let sweep = EhmaBatchRange {
        period: cfg.period_range,
    };
    let out = ehma_batch_inner(data, &sweep, detect_best_kernel(), false)
        .map_err(|e| JsValue::from_str(&e.to_string()))?;
    let js = EhmaBatchJsOutput {
        values: out.values,
        combos: out.combos,
        rows: out.rows,
        cols: out.cols,
    };
    serde_wasm_bindgen::to_value(&js)
        .map_err(|e| JsValue::from_str(&format!("Serialization error: {}", e)))
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn ehma_batch_into(
    in_ptr: *const f64,
    out_ptr: *mut f64,
    len: usize,
    p_start: usize,
    p_end: usize,
    p_step: usize,
) -> Result<usize, JsValue> {
    if in_ptr.is_null() || out_ptr.is_null() {
        return Err(JsValue::from_str("null pointer passed to ehma_batch_into"));
    }
    unsafe {
        let data = std::slice::from_raw_parts(in_ptr, len);
        let sweep = EhmaBatchRange {
            period: (p_start, p_end, p_step),
        };
        let combos = expand_grid(&sweep);
        let rows = combos.len();
        let cols = len;

        let out = std::slice::from_raw_parts_mut(out_ptr, rows * cols);

        let simd = match detect_best_batch_kernel() {
            Kernel::Avx512Batch => Kernel::Avx512,
            Kernel::Avx2Batch => Kernel::Avx2,
            _ => Kernel::Scalar,
        };

        ehma_batch_inner_into(data, &sweep, simd, false, out)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;
        Ok(rows)
    }
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn ehma_js(data: &[f64], period: usize) -> Result<Vec<f64>, JsValue> {
    ehma_wasm(data, Some(period))
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub struct EhmaWasmStream {
    inner: EhmaStream,
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
impl EhmaWasmStream {
    #[wasm_bindgen(constructor)]
    pub fn new(period: Option<usize>) -> Result<EhmaWasmStream, JsValue> {
        let params = EhmaParams { period };
        let stream = EhmaStream::try_new(params).map_err(|e| JsValue::from_str(&e.to_string()))?;
        Ok(Self { inner: stream })
    }

    pub fn update(&mut self, value: f64) -> JsValue {
        match self.inner.update(value) {
            Some(v) => JsValue::from_f64(v),
            None => JsValue::NULL,
        }
    }

    pub fn reset(&mut self) {
        self.inner.buffer.fill(f64::NAN);
        self.inner.head = 0;
        self.inner.filled = false;

        self.inner.sum_x = 0.0;
        self.inner.z_re = 0.0;
        self.inner.z_im = 0.0;
    }
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
#[deprecated(
    since = "1.0.0",
    note = "For weight reuse patterns, use the fast/unsafe API with persistent buffers"
)]
pub struct EhmaContext {
    weights: Vec<f64>,
    inv_coef: f64,
    period: usize,
    first: usize,
    kernel: Kernel,
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
#[allow(deprecated)]
impl EhmaContext {
    #[wasm_bindgen(constructor)]
    #[deprecated(
        since = "1.0.0",
        note = "For weight reuse patterns, use the fast/unsafe API with persistent buffers"
    )]
    pub fn new(period: usize) -> Result<EhmaContext, JsValue> {
        if period == 0 {
            return Err(JsValue::from_str("Invalid period: 0"));
        }

        let mut weights = Vec::with_capacity(period);
        let mut sum = 0.0;
        use std::f64::consts::PI;
        for j in 0..period {
            let i = (period - j) as f64;
            let w = 1.0 - ((2.0 * PI * i) / (period as f64 + 1.0)).cos();
            weights.push(w);
            sum += w;
        }

        let inv_coef = 1.0 / sum;

        Ok(EhmaContext {
            weights,
            inv_coef,
            period,
            first: 0,
            kernel: detect_best_kernel(),
        })
    }

    pub fn update_into(
        &self,
        in_ptr: *const f64,
        out_ptr: *mut f64,
        len: usize,
    ) -> Result<(), JsValue> {
        if len < self.period {
            return Err(JsValue::from_str(&format!(
                "Data length {} is less than period {}",
                len, self.period
            )));
        }

        let data = unsafe { std::slice::from_raw_parts(in_ptr, len) };
        let out = unsafe { std::slice::from_raw_parts_mut(out_ptr, len) };

        for i in 0..self.period - 1 {
            out[i] = f64::NAN;
        }

        #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
        unsafe {
            ehma_simd128(
                data,
                &self.weights,
                self.period,
                self.first,
                self.inv_coef,
                out,
            )
        }

        #[cfg(not(all(target_arch = "wasm32", target_feature = "simd128")))]
        match self.kernel {
            Kernel::Scalar | Kernel::ScalarBatch => ehma_scalar(
                data,
                &self.weights,
                self.period,
                self.first,
                self.inv_coef,
                out,
            ),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx2 | Kernel::Avx2Batch => unsafe {
                ehma_avx2(
                    data,
                    &self.weights,
                    self.period,
                    self.first,
                    self.inv_coef,
                    out,
                )
            },
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx512 | Kernel::Avx512Batch => unsafe {
                ehma_avx512(
                    data,
                    &self.weights,
                    self.period,
                    self.first,
                    self.inv_coef,
                    out,
                )
            },
            _ => ehma_scalar(
                data,
                &self.weights,
                self.period,
                self.first,
                self.inv_coef,
                out,
            ),
        }

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::utilities::data_loader::read_candles_from_csv;
    use std::error::Error;

    use crate::skip_if_unsupported;

    macro_rules! generate_all_ehma_tests {
        ($($test_fn:ident),*) => {
            paste::paste! {
                $(
                    #[test]
                    fn [<$test_fn _scalar_f64>]() {
                        let _ = $test_fn(stringify!([<$test_fn _scalar_f64>]), Kernel::Scalar);
                    }
                )*
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                mod avx_tests {
                    use super::*;
                    $(
                        #[test]
                        fn [<$test_fn _avx2_f64>]() {
                            let _ = $test_fn(stringify!([<$test_fn _avx2_f64>]), Kernel::Avx2);
                        }
                        #[test]
                        fn [<$test_fn _avx512_f64>]() {
                            let _ = $test_fn(stringify!([<$test_fn _avx512_f64>]), Kernel::Avx512);
                        }
                    )*
                }
                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                mod wasm_tests {
                    use super::*;
                    $(
                        #[test]
                        fn [<$test_fn _simd128_f64>]() {
                            let _ = $test_fn(stringify!([<$test_fn _simd128_f64>]), Kernel::Scalar);
                        }
                    )*
                }
            }
        };
    }

    fn check_ehma_accuracy(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let file_path = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let candles = read_candles_from_csv(file_path)?;

        let input = EhmaInput::from_candles(&candles, "close", EhmaParams::default());
        let result = ehma_with_kernel(&input, kernel)?;

        assert_eq!(result.values.len(), candles.close.len());

        for i in 0..13 {
            assert!(
                result.values[i].is_nan(),
                "[{}] Value at {} should be NaN",
                test_name,
                i
            );
        }

        for i in 13..result.values.len().min(100) {
            assert!(
                !result.values[i].is_nan(),
                "[{}] Value at {} should not be NaN",
                test_name,
                i
            );
            assert!(
                result.values[i].is_finite(),
                "[{}] Value at {} should be finite",
                test_name,
                i
            );
        }

        Ok(())
    }

    fn check_ehma_partial_params(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        let file_path = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let candles = read_candles_from_csv(file_path)?;

        let data: Vec<f64> = candles.close[0..18].to_vec();

        let params = EhmaParams { period: Some(14) };
        let input = EhmaInput::from_slice(&data, params);
        let result = ehma_with_kernel(&input, kernel)?;

        assert_eq!(result.values.len(), data.len());

        for i in 0..13 {
            assert!(
                result.values[i].is_nan(),
                "Value at index {} should be NaN",
                i
            );
        }

        for i in 13..result.values.len() {
            assert!(
                !result.values[i].is_nan(),
                "Value at index {} should not be NaN",
                i
            );
            assert!(
                result.values[i].is_finite(),
                "Value at index {} should be finite",
                i
            );
        }

        let min_data = data.iter().fold(f64::INFINITY, |a, &b| a.min(b));
        let max_data = data.iter().fold(f64::NEG_INFINITY, |a, &b| a.max(b));

        for i in 13..result.values.len() {
            let tolerance = (max_data - min_data) * 0.1;
            assert!(
                result.values[i] >= min_data - tolerance
                    && result.values[i] <= max_data + tolerance,
                "EHMA value {} at index {} is outside reasonable range [{}, {}]",
                result.values[i],
                i,
                min_data - tolerance,
                max_data + tolerance
            );
        }

        println!(
            "[{}] EHMA value at index 13: {}",
            test_name, result.values[13]
        );
        println!(
            "[{}] EHMA value at index 14: {}",
            test_name, result.values[14]
        );
        println!(
            "[{}] EHMA value at index 15: {}",
            test_name, result.values[15]
        );
        println!(
            "[{}] EHMA value at index 16: {}",
            test_name, result.values[16]
        );
        println!(
            "[{}] EHMA value at index 17: {}",
            test_name, result.values[17]
        );

        Ok(())
    }

    fn check_ehma_empty_input(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let data: Vec<f64> = vec![];
        let params = EhmaParams::default();
        let input = EhmaInput::from_slice(&data, params);
        let result = ehma_with_kernel(&input, kernel);
        assert!(
            matches!(result, Err(EhmaError::EmptyInputData)),
            "[{}] EHMA should fail with empty input",
            test_name
        );
        Ok(())
    }

    fn check_ehma_all_nan(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let data = vec![f64::NAN; 20];
        let params = EhmaParams::default();
        let input = EhmaInput::from_slice(&data, params);
        let result = ehma_with_kernel(&input, kernel);
        assert!(
            matches!(result, Err(EhmaError::AllValuesNaN)),
            "[{}] EHMA should fail with all NaN values",
            test_name
        );
        Ok(())
    }

    fn check_ehma_zero_period(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let params = EhmaParams { period: Some(0) };
        let input = EhmaInput::from_slice(&data, params);
        let result = ehma_with_kernel(&input, kernel);
        assert!(
            matches!(result, Err(EhmaError::InvalidPeriod { .. })),
            "[{}] EHMA should fail with zero period",
            test_name
        );
        Ok(())
    }

    fn check_ehma_period_exceeds_length(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let params = EhmaParams { period: Some(10) };
        let input = EhmaInput::from_slice(&data, params);
        let result = ehma_with_kernel(&input, kernel);
        assert!(
            matches!(result, Err(EhmaError::InvalidPeriod { .. })),
            "[{}] EHMA should fail when period exceeds data length",
            test_name
        );
        Ok(())
    }

    fn check_ehma_very_small_dataset(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let data = vec![42.0];
        let params = EhmaParams { period: Some(5) };
        let input = EhmaInput::from_slice(&data, params);
        let result = ehma_with_kernel(&input, kernel);
        assert!(
            matches!(
                result,
                Err(EhmaError::InvalidPeriod { .. }) | Err(EhmaError::NotEnoughValidData { .. })
            ),
            "[{}] EHMA should fail with insufficient data",
            test_name
        );
        Ok(())
    }

    fn check_ehma_default_candles(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let file_path = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let candles = read_candles_from_csv(file_path)?;

        let default_params = EhmaParams { period: None };
        let input = EhmaInput::from_candles(&candles, "close", default_params);
        let output = ehma_with_kernel(&input, kernel)?;
        assert_eq!(output.values.len(), candles.close.len());

        Ok(())
    }

    fn check_ehma_reinput(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let file_path = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let candles = read_candles_from_csv(file_path)?;

        let first_params = EhmaParams { period: Some(14) };
        let first_input = EhmaInput::from_candles(&candles, "close", first_params.clone());
        let first_result = ehma_with_kernel(&first_input, kernel)?;

        let second_input = EhmaInput::from_slice(&first_result.values, first_params);
        let second_result = ehma_with_kernel(&second_input, kernel)?;

        assert_eq!(second_result.values.len(), first_result.values.len());

        let valid_count = second_result
            .values
            .iter()
            .zip(first_result.values.iter())
            .filter(|(a, b)| !a.is_nan() && !b.is_nan() && (*a - *b).abs() > 1e-10)
            .count();

        assert!(
            valid_count > 0,
            "[{}] EHMA reinput should produce different values",
            test_name
        );

        Ok(())
    }

    fn check_ehma_nan_handling(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let file_path = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let candles = read_candles_from_csv(file_path)?;

        let input = EhmaInput::from_candles(&candles, "close", EhmaParams { period: Some(14) });
        let res = ehma_with_kernel(&input, kernel)?;
        assert_eq!(res.values.len(), candles.close.len());

        if res.values.len() > 240 {
            for (i, &val) in res.values[240..].iter().enumerate() {
                assert!(
                    !val.is_nan(),
                    "[{}] Found unexpected NaN at out-index {}",
                    test_name,
                    240 + i
                );
            }
        }
        Ok(())
    }

    fn check_ehma_streaming(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);

        let file_path = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let candles = read_candles_from_csv(file_path)?;

        let period = 14;

        let input = EhmaInput::from_candles(
            &candles,
            "close",
            EhmaParams {
                period: Some(period),
            },
        );
        let batch_output = ehma_with_kernel(&input, kernel)?.values;

        let mut stream = EhmaStream::try_new(EhmaParams {
            period: Some(period),
        })?;

        let mut stream_values = Vec::with_capacity(candles.close.len());
        for &price in &candles.close {
            match stream.update(price) {
                Some(y) => stream_values.push(y),
                None => stream_values.push(f64::NAN),
            }
        }

        assert_eq!(batch_output.len(), stream_values.len());
        for (i, (&b, &s)) in batch_output.iter().zip(stream_values.iter()).enumerate() {
            if b.is_nan() && s.is_nan() {
                continue;
            }
            let diff = (b - s).abs();
            assert!(
                diff < 1e-9,
                "[{}] EHMA streaming f64 mismatch at idx {}: batch={}, stream={}, diff={}",
                test_name,
                i,
                b,
                s,
                diff
            );
        }
        Ok(())
    }

    #[cfg(debug_assertions)]
    fn check_ehma_no_poison(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);

        let file_path = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let candles = read_candles_from_csv(file_path)?;

        let test_params = vec![
            EhmaParams::default(),
            EhmaParams { period: Some(5) },
            EhmaParams { period: Some(10) },
            EhmaParams { period: Some(20) },
            EhmaParams { period: Some(50) },
            EhmaParams { period: Some(100) },
        ];

        for (_param_idx, params) in test_params.iter().enumerate() {
            let input = EhmaInput::from_candles(&candles, "close", params.clone());
            let output = ehma_with_kernel(&input, kernel)?;

            for (i, &val) in output.values.iter().enumerate() {
                if val.is_nan() {
                    continue;
                }

                let bits = val.to_bits();

                if bits == 0x11111111_11111111 {
                    panic!(
                        "[{}] Found alloc_with_nan_prefix poison value {} (0x{:016X}) at index {} \
                        with params: period={}",
                        test_name,
                        val,
                        bits,
                        i,
                        params.period.unwrap_or(14)
                    );
                }

                if bits == 0x22222222_22222222 {
                    panic!(
                        "[{}] Found init_matrix_prefixes poison value {} (0x{:016X}) at index {} \
                        with params: period={}",
                        test_name,
                        val,
                        bits,
                        i,
                        params.period.unwrap_or(14)
                    );
                }

                if bits == 0x33333333_33333333 {
                    panic!(
                        "[{}] Found make_uninit_matrix poison value {} (0x{:016X}) at index {} \
                        with params: period={}",
                        test_name,
                        val,
                        bits,
                        i,
                        params.period.unwrap_or(14)
                    );
                }
            }
        }

        Ok(())
    }

    #[cfg(not(debug_assertions))]
    fn check_ehma_no_poison(_test_name: &str, _kernel: Kernel) -> Result<(), Box<dyn Error>> {
        Ok(())
    }

    generate_all_ehma_tests!(
        check_ehma_partial_params,
        check_ehma_accuracy,
        check_ehma_default_candles,
        check_ehma_zero_period,
        check_ehma_period_exceeds_length,
        check_ehma_very_small_dataset,
        check_ehma_empty_input,
        check_ehma_all_nan,
        check_ehma_reinput,
        check_ehma_nan_handling,
        check_ehma_streaming,
        check_ehma_no_poison
    );

    fn check_batch_default_row(test: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test);

        let file = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let c = read_candles_from_csv(file)?;

        let output = EhmaBatchBuilder::new()
            .kernel(kernel)
            .apply_candles(&c, "close")?;

        let def = EhmaParams::default();
        let row = output.values_for(&def).expect("default row missing");

        assert_eq!(row.len(), c.close.len());
        assert!(row
            .iter()
            .skip(def.period.unwrap() - 1)
            .any(|v| v.is_finite()));

        Ok(())
    }

    fn check_batch_sweep(test: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test);

        let file = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let c = read_candles_from_csv(file)?;

        let output = EhmaBatchBuilder::new()
            .kernel(kernel)
            .period_range(10, 20, 2)
            .apply_candles(&c, "close")?;

        assert_eq!(output.rows, 6);
        assert_eq!(output.cols, c.close.len());
        for (i, p) in output.combos.iter().enumerate() {
            assert_eq!(p.period.unwrap(), 10 + 2 * i);
        }

        Ok(())
    }

    #[cfg(debug_assertions)]
    fn check_batch_no_poison(test: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test);

        let file = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let c = read_candles_from_csv(file)?;

        let test_configs = vec![(5, 15, 5), (10, 30, 10), (14, 14, 1), (20, 50, 15)];

        for (_cfg_idx, &(start, stop, step)) in test_configs.iter().enumerate() {
            let output = EhmaBatchBuilder::new()
                .kernel(kernel)
                .period_range(start, stop, step)
                .apply_candles(&c, "close")?;

            for idx in 0..output.values.len() {
                let val = output.values[idx];
                if val.is_nan() {
                    continue;
                }

                let bits = val.to_bits();

                if bits == 0x11111111_11111111
                    || bits == 0x22222222_22222222
                    || bits == 0x33333333_33333333
                {
                    panic!(
                        "[{}] Found poison value {} (0x{:016X}) at index {}",
                        test, val, bits, idx
                    );
                }
            }
        }

        Ok(())
    }

    #[cfg(not(debug_assertions))]
    fn check_batch_no_poison(_test: &str, _kernel: Kernel) -> Result<(), Box<dyn Error>> {
        Ok(())
    }

    macro_rules! gen_batch_tests {
        ($fn_name:ident) => {
            paste::paste! {
                #[test] fn [<$fn_name _scalar>]() {
                    let _ = $fn_name(stringify!([<$fn_name _scalar>]), Kernel::ScalarBatch);
                }
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                #[test] fn [<$fn_name _avx2>]() {
                    let _ = $fn_name(stringify!([<$fn_name _avx2>]), Kernel::Avx2Batch);
                }
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                #[test] fn [<$fn_name _avx512>]() {
                    let _ = $fn_name(stringify!([<$fn_name _avx512>]), Kernel::Avx512Batch);
                }
                #[test] fn [<$fn_name _auto_detect>]() {
                    let _ = $fn_name(stringify!([<$fn_name _auto_detect>]), Kernel::Auto);
                }
            }
        };
    }

    gen_batch_tests!(check_batch_default_row);
    gen_batch_tests!(check_batch_sweep);
    gen_batch_tests!(check_batch_no_poison);

    #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
    #[test]
    fn test_ehma_simd128_correctness() {
        let data = vec![
            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0,
        ];
        let period = 10;

        let params = EhmaParams {
            period: Some(period),
        };
        let input = EhmaInput::from_slice(&data, params);
        let scalar_output = ehma_with_kernel(&input, Kernel::Scalar).unwrap();

        let simd128_output = ehma_with_kernel(&input, Kernel::Scalar).unwrap();

        assert_eq!(scalar_output.values.len(), simd128_output.values.len());
        for (i, (scalar_val, simd_val)) in scalar_output
            .values
            .iter()
            .zip(simd128_output.values.iter())
            .enumerate()
        {
            assert!(
                (scalar_val - simd_val).abs() < 1e-10,
                "SIMD128 mismatch at index {}: scalar={}, simd128={}",
                i,
                scalar_val,
                simd_val
            );
        }
    }

    #[test]
    fn check_ehma_batch_inner_into_warm_and_no_poison() -> Result<(), Box<dyn std::error::Error>> {
        use crate::utilities::data_loader::read_candles_from_csv;
        let file = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let c = read_candles_from_csv(file)?;
        let sweep = EhmaBatchRange {
            period: (10, 14, 2),
        };
        let combos = expand_grid(&sweep);
        let rows = combos.len();
        let cols = c.close.len();
        let mut out = vec![0.0f64; rows * cols];

        ehma_batch_inner_into(&c.close, &sweep, Kernel::Scalar, true, &mut out)?;

        for (r, p) in combos.iter().enumerate() {
            let warm = p.period.unwrap() - 1;
            assert!(out[r * cols..r * cols + warm].iter().all(|v| v.is_nan()));
        }

        #[cfg(debug_assertions)]
        for &v in &out {
            if v.is_nan() {
                continue;
            }
            let b = v.to_bits();
            assert!(
                b != 0x11111111_11111111 && b != 0x22222222_22222222 && b != 0x33333333_33333333
            );
        }
        Ok(())
    }

    #[test]
    fn test_ehma_weight_debug() {
        let period = 14;
        let mut weights = vec![0.0; period];
        let mut coef_sum = 0.0;

        use std::f64::consts::PI;

        println!("Current weight calculation (i from 1 to period):");
        for i in 1..=period {
            let cosine = 1.0 - ((2.0 * PI * i as f64) / (period + 1) as f64).cos();
            weights[period - i] = cosine;
            coef_sum += cosine;
            println!(
                "  i={}, cosine={:.8}, stored at index {}",
                i,
                cosine,
                period - i
            );
        }

        println!("\nFinal weights array (index -> value):");
        for (idx, w) in weights.iter().enumerate() {
            println!("  weights[{}] = {:.8}", idx, w);
        }

        println!("\nSum of weights: {:.8}", coef_sum);
        println!("Normalization factor: {:.8}", 1.0 / coef_sum);

        let mut weights2 = vec![0.0; period];
        let mut coef_sum2 = 0.0;

        println!("\nAlternative weight calculation (reversed storage):");
        for i in 1..=period {
            let cosine = 1.0 - ((2.0 * PI * i as f64) / (period + 1) as f64).cos();
            weights2[i - 1] = cosine;
            coef_sum2 += cosine;
            println!("  i={}, cosine={:.8}, stored at index {}", i, cosine, i - 1);
        }

        println!("\nAlternative weights array:");
        for (idx, w) in weights2.iter().enumerate() {
            println!("  weights2[{}] = {:.8}", idx, w);
        }
    }

    #[test]
    fn test_ehma_reference_values() {
        let data = vec![
            59500.0, 59450.0, 59420.0, 59380.0, 59350.0, 59320.0, 59310.0, 59300.0, 59280.0,
            59260.0, 59250.0, 59240.0, 59230.0, 59220.0, 59210.0, 59200.0, 59190.0, 59180.0,
        ];

        let params = EhmaParams { period: Some(14) };
        let input = EhmaInput::from_slice(&data, params);
        let result = ehma(&input).expect("EHMA calculation failed");

        let expected_values = vec![
            59309.74802712,
            59291.69687546,
            59275.88831852,
            59261.82816317,
            59249.06571993,
        ];

        for (i, &expected) in expected_values.iter().enumerate() {
            let idx = 13 + i;
            assert!(
                (result.values[idx] - expected).abs() < 0.0001,
                "Value at index {} should be {:.8}, got {:.8}",
                idx,
                expected,
                result.values[idx]
            );
        }

        assert_eq!(result.values.len(), data.len());

        for i in 0..13.min(result.values.len()) {
            assert!(
                result.values[i].is_nan(),
                "Value at index {} should be NaN",
                i
            );
        }

        for i in 13..result.values.len() {
            assert!(
                !result.values[i].is_nan(),
                "Value at index {} should not be NaN",
                i
            );
            assert!(
                result.values[i].is_finite(),
                "Value at index {} should be finite",
                i
            );
        }
    }

    #[test]
    fn test_ehma_pinescript_parity() {
        println!("\n=== EHMA PineScript Parity Investigation ===\n");

        let file_path = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let candles = read_candles_from_csv(file_path).expect("Failed to load CSV");

        let close: Vec<f64> = candles.close[0..100.min(candles.close.len())].to_vec();

        println!(
            "Using CSV data - first 5 values: {:?}",
            &close[..5.min(close.len())]
        );
        println!("Total data points loaded: {}", close.len());

        let pine_refs = vec![
            59417.85296671,
            59307.66635431,
            59222.28072230,
            59171.41684053,
            59153.35666389,
        ];

        let period = 14;

        println!("Test 1: Standard EHMA with close prices");
        let params = EhmaParams {
            period: Some(period),
        };
        let input = EhmaInput::from_slice(&close, params.clone());
        let result1 = ehma(&input).expect("EHMA calculation failed");

        println!("  Values from index 13-30:");
        for i in 13..30.min(result1.values.len()) {
            if !result1.values[i].is_nan() {
                println!("    Index {}: {:.8}", i, result1.values[i]);

                for (ref_idx, &ref_val) in pine_refs.iter().enumerate() {
                    let diff = (result1.values[i] - ref_val).abs();
                    if diff < 1.0 {
                        println!(
                            "      -> Very close to Reference[{}]! (diff: {:.8})",
                            ref_idx, diff
                        );
                    }
                }
            }
        }

        println!("\nTest 2: EHMA with PineScript warmup (zero-padding)");
        let mut padded = Vec::with_capacity(period - 1 + close.len());
        padded.resize(period - 1, 0.0);
        padded.extend_from_slice(&close);

        let input2 = EhmaInput::from_slice(&padded, params.clone());
        let result2 = ehma(&input2).expect("EHMA calculation failed");
        let out2 = &result2.values[(period - 1)..];

        for i in 0..pine_refs.len().min(out2.len()) {
            println!("  Index {}: {:.8}", i, out2[i]);
        }

        println!("\nTest 3: EHMA with non-repaint shift (1-bar historical lag)");
        let hist = &close[..close.len().saturating_sub(1)];
        let input3 = EhmaInput::from_slice(hist, params.clone());
        let result3 = ehma(&input3).expect("EHMA calculation failed");

        let mut out3 = vec![f64::NAN; close.len()];
        out3[1..1 + result3.values.len()].copy_from_slice(&result3.values);

        for i in 14..(14 + pine_refs.len()).min(out3.len()) {
            if !out3[i].is_nan() {
                println!("  Index {}: {:.8}", i, out3[i]);
            }
        }

        println!("\nTest 4: Simulated HLCC4 source");
        let mut hlcc4 = vec![];
        for i in 0..close.len() {
            let high = close[i] * 1.001;
            let low = close[i] * 0.999;
            let hlcc4_val = (high + low + close[i] + close[i]) / 4.0;
            hlcc4.push(hlcc4_val);
        }

        let input4 = EhmaInput::from_slice(&hlcc4, params.clone());
        let result4 = ehma(&input4).expect("EHMA calculation failed");

        for i in 13..(13 + pine_refs.len()).min(result4.values.len()) {
            println!("  Index {}: {:.8}", i, result4.values[i]);
        }

        println!("\nTest 5: Zero-padded + non-repaint shift");
        let mut padded5 = Vec::with_capacity(period - 1 + close.len() - 1);
        padded5.resize(period - 1, 0.0);
        padded5.extend_from_slice(&close[..close.len() - 1]);

        let input5 = EhmaInput::from_slice(&padded5, params.clone());
        let result5 = ehma(&input5).expect("EHMA calculation failed");
        let mut out5 = vec![f64::NAN; close.len()];
        let tmp5 = &result5.values[(period - 1)..];
        if tmp5.len() > 0 {
            out5[1..1 + tmp5.len()].copy_from_slice(tmp5);
        }

        for i in 0..pine_refs.len().min(out5.len()) {
            if !out5[i].is_nan() {
                println!("  Index {}: {:.8}", i, out5[i]);
            }
        }

        println!("\n=== Comparison with PineScript Reference Values ===");
        for (i, ref_val) in pine_refs.iter().enumerate() {
            println!("Reference[{}]: {:.8}", i, ref_val);

            if 13 + i < result1.values.len() {
                let diff1 = (result1.values[13 + i] - ref_val).abs();
                println!("  Test 1 diff: {:.8}", diff1);
            }

            if i < out2.len() {
                let diff2 = (out2[i] - ref_val).abs();
                println!("  Test 2 diff: {:.8}", diff2);
            }

            if 14 + i < out3.len() && !out3[14 + i].is_nan() {
                let diff3 = (out3[14 + i] - ref_val).abs();
                println!("  Test 3 diff: {:.8}", diff3);
            }

            if 13 + i < result4.values.len() {
                let diff4 = (result4.values[13 + i] - ref_val).abs();
                println!("  Test 4 diff: {:.8}", diff4);
            }

            if i < out5.len() && !out5[i].is_nan() {
                let diff5 = (out5[i] - ref_val).abs();
                println!("  Test 5 diff: {:.8}", diff5);
            }
        }

        println!("\n=== Searching for exact matches ===");
        for (ref_idx, &ref_val) in pine_refs.iter().enumerate() {
            println!("Looking for Reference[{}] = {:.8}", ref_idx, ref_val);

            for (idx, &val) in result1.values.iter().enumerate() {
                if !val.is_nan() {
                    let diff = (val - ref_val).abs();
                    if diff < 0.01 {
                        println!(
                            "  Found close match in Test 1 at index {}: {} (diff: {})",
                            idx, val, diff
                        );
                    }
                }
            }
        }
    }

    #[test]
    fn test_ehma_into_matches_api() {
        let n = 256usize;
        let mut data = Vec::with_capacity(n);
        for i in 0..n {
            let x = i as f64;

            data.push((x * 0.03125).sin() * 2.0 + (x * 0.001));
        }

        let input = EhmaInput::from_slice(&data, EhmaParams::default());

        let baseline = ehma(&input).expect("ehma baseline should succeed").values;

        let mut out = vec![0.0; data.len()];
        #[cfg(not(all(target_arch = "wasm32", feature = "wasm")))]
        {
            ehma_into(&input, &mut out).expect("ehma_into should succeed");
        }
        #[cfg(all(target_arch = "wasm32", feature = "wasm"))]
        {
            ehma_into_slice(&mut out, &input, detect_best_kernel()).expect("ehma_into_slice ok");
        }

        assert_eq!(baseline.len(), out.len());

        fn eq_or_both_nan(a: f64, b: f64) -> bool {
            (a.is_nan() && b.is_nan()) || (a - b).abs() <= 1e-12
        }

        for (i, (&a, &b)) in baseline.iter().zip(out.iter()).enumerate() {
            assert!(
                eq_or_both_nan(a, b),
                "divergence at index {}: baseline={}, into={}",
                i,
                a,
                b
            );
        }
    }
}
