#[cfg(all(feature = "python", feature = "cuda"))]
use crate::utilities::dlpack_cuda::{make_device_array_py, DeviceArrayF32Py};
#[cfg(feature = "python")]
use crate::utilities::kernel_validation::validate_kernel;
#[cfg(all(feature = "python", feature = "cuda"))]
use numpy::PyUntypedArrayMethods;
#[cfg(feature = "python")]
use numpy::{IntoPyArray, PyArray1, PyArrayMethods, PyReadonlyArray1};
#[cfg(feature = "python")]
use pyo3::exceptions::PyValueError;
#[cfg(feature = "python")]
use pyo3::prelude::*;
#[cfg(feature = "python")]
use pyo3::types::PyDict;
#[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,
};
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;
#[cfg(all(feature = "python", feature = "cuda"))]
use std::sync::Arc;
use thiserror::Error;

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

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

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

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

impl Default for KurtosisParams {
    fn default() -> Self {
        Self { period: Some(5) }
    }
}

#[derive(Debug, Clone)]
pub struct KurtosisInput<'a> {
    pub data: KurtosisData<'a>,
    pub params: KurtosisParams,
}

impl<'a> KurtosisInput<'a> {
    #[inline]
    pub fn from_candles(c: &'a Candles, s: &'a str, p: KurtosisParams) -> Self {
        Self {
            data: KurtosisData::Candles {
                candles: c,
                source: s,
            },
            params: p,
        }
    }
    #[inline]
    pub fn from_slice(sl: &'a [f64], p: KurtosisParams) -> Self {
        Self {
            data: KurtosisData::Slice(sl),
            params: p,
        }
    }
    #[inline]
    pub fn with_default_candles(c: &'a Candles) -> Self {
        Self::from_candles(c, "hl2", KurtosisParams::default())
    }
    #[inline]
    pub fn get_period(&self) -> usize {
        self.params.period.unwrap_or(5)
    }
}

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

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

impl KurtosisBuilder {
    #[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<KurtosisOutput, KurtosisError> {
        let p = KurtosisParams {
            period: self.period,
        };
        let i = KurtosisInput::from_candles(c, "hl2", p);
        kurtosis_with_kernel(&i, self.kernel)
    }
    #[inline(always)]
    pub fn apply_slice(self, d: &[f64]) -> Result<KurtosisOutput, KurtosisError> {
        let p = KurtosisParams {
            period: self.period,
        };
        let i = KurtosisInput::from_slice(d, p);
        kurtosis_with_kernel(&i, self.kernel)
    }
    #[inline(always)]
    pub fn into_stream(self) -> Result<KurtosisStream, KurtosisError> {
        let p = KurtosisParams {
            period: self.period,
        };
        KurtosisStream::try_new(p)
    }
}

#[derive(Debug, Error)]
pub enum KurtosisError {
    #[error("kurtosis: Input data slice is empty.")]
    EmptyInputData,
    #[error("kurtosis: All values are NaN.")]
    AllValuesNaN,
    #[error("kurtosis: Invalid period: period = {period}, data length = {data_len}")]
    InvalidPeriod { period: usize, data_len: usize },
    #[error("kurtosis: Not enough valid data: needed = {needed}, valid = {valid}")]
    NotEnoughValidData { needed: usize, valid: usize },
    #[error("kurtosis: Output length mismatch: expected {expected}, got {got}")]
    OutputLengthMismatch { expected: usize, got: usize },
    #[error("kurtosis: Invalid range: start={start}, end={end}, step={step}")]
    InvalidRange {
        start: String,
        end: String,
        step: String,
    },
    #[error("kurtosis: Invalid kernel for batch: {0:?}")]
    InvalidKernelForBatch(Kernel),
    #[error("kurtosis: Invalid period (zero or missing).")]
    ZeroOrMissingPeriod,
}

#[inline]
pub fn kurtosis(input: &KurtosisInput) -> Result<KurtosisOutput, KurtosisError> {
    kurtosis_with_kernel(input, Kernel::Auto)
}

pub fn kurtosis_with_kernel(
    input: &KurtosisInput,
    kernel: Kernel,
) -> Result<KurtosisOutput, KurtosisError> {
    let data: &[f64] = input.as_ref();

    if data.is_empty() {
        return Err(KurtosisError::EmptyInputData);
    }

    let first = data
        .iter()
        .position(|x| !x.is_nan())
        .ok_or(KurtosisError::AllValuesNaN)?;

    let len = data.len();
    let period = input.get_period();

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

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

    let chosen = match kernel {
        Kernel::Auto => Kernel::Scalar,
        other => other,
    };

    unsafe {
        match chosen {
            Kernel::Scalar | Kernel::ScalarBatch => kurtosis_scalar(data, period, first, &mut out),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx2 | Kernel::Avx2Batch => kurtosis_avx2(data, period, first, &mut out),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx512 | Kernel::Avx512Batch => kurtosis_avx512(data, period, first, &mut out),
            #[cfg(not(all(feature = "nightly-avx", target_arch = "x86_64")))]
            Kernel::Avx2 | Kernel::Avx2Batch | Kernel::Avx512 | Kernel::Avx512Batch => {
                kurtosis_scalar(data, period, first, &mut out)
            }
            _ => unreachable!(),
        }
    }
    Ok(KurtosisOutput { values: out })
}

#[inline]
pub fn kurtosis_into_slice(
    dst: &mut [f64],
    input: &KurtosisInput,
    kernel: Kernel,
) -> Result<(), KurtosisError> {
    let data: &[f64] = input.as_ref();

    if data.is_empty() {
        return Err(KurtosisError::EmptyInputData);
    }

    let first = data
        .iter()
        .position(|x| !x.is_nan())
        .ok_or(KurtosisError::AllValuesNaN)?;

    let len = data.len();
    let period = input.get_period();

    if period == 0 || period > len {
        return Err(KurtosisError::InvalidPeriod {
            period,
            data_len: len,
        });
    }
    if (len - first) < period {
        return Err(KurtosisError::NotEnoughValidData {
            needed: period,
            valid: len - first,
        });
    }
    if dst.len() != data.len() {
        return Err(KurtosisError::OutputLengthMismatch {
            expected: data.len(),
            got: dst.len(),
        });
    }

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

    let chosen = match kernel {
        Kernel::Auto => Kernel::Scalar,
        other => other,
    };

    unsafe {
        match chosen {
            Kernel::Scalar | Kernel::ScalarBatch => kurtosis_scalar(data, period, first, dst),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx2 | Kernel::Avx2Batch => kurtosis_avx2(data, period, first, dst),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx512 | Kernel::Avx512Batch => kurtosis_avx512(data, period, first, dst),
            #[cfg(not(all(feature = "nightly-avx", target_arch = "x86_64")))]
            Kernel::Avx2 | Kernel::Avx2Batch | Kernel::Avx512 | Kernel::Avx512Batch => {
                kurtosis_scalar(data, period, first, dst)
            }
            _ => unreachable!(),
        }
    }

    Ok(())
}

#[cfg(not(all(target_arch = "wasm32", feature = "wasm")))]
#[inline]
pub fn kurtosis_into(input: &KurtosisInput, out: &mut [f64]) -> Result<(), KurtosisError> {
    kurtosis_into_slice(out, input, Kernel::Auto)?;

    let data: &[f64] = input.as_ref();
    let first = data
        .iter()
        .position(|x| !x.is_nan())
        .ok_or(KurtosisError::AllValuesNaN)?;
    let warmup_end = first + input.get_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;
    }

    Ok(())
}

#[inline]
pub fn kurtosis_scalar(data: &[f64], period: usize, first: usize, out: &mut [f64]) {
    for i in (first + period - 1)..data.len() {
        let start = i + 1 - period;
        let window = &data[start..start + period];

        let mut has_nan = false;
        let mut sum = 0.0f64;
        for &v in window {
            if v.is_nan() {
                has_nan = true;
                break;
            }
            sum += v;
        }
        if has_nan {
            out[i] = f64::NAN;
            continue;
        }
        let n = period as f64;
        let mean = sum / n;
        let mut m2 = 0.0;
        let mut m4 = 0.0;
        for &val in window {
            let diff = val - mean;
            let d2 = diff * diff;
            m2 += d2;
            m4 += d2 * d2;
        }
        m2 /= n;
        m4 /= n;

        if m2.abs() < f64::EPSILON {
            out[i] = f64::NAN;
        } else {
            out[i] = (m4 / (m2 * m2)) - 3.0;
        }
    }
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline]
pub fn kurtosis_avx512(data: &[f64], period: usize, first: usize, out: &mut [f64]) {
    if period <= 32 {
        unsafe { kurtosis_avx512_short(data, period, first, out) }
    } else {
        unsafe { kurtosis_avx512_long(data, period, first, out) }
    }
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline]
pub fn kurtosis_avx2(data: &[f64], period: usize, first: usize, out: &mut [f64]) {
    unsafe { kurtosis_scalar(data, period, first, out) }
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline]
pub unsafe fn kurtosis_avx512_short(data: &[f64], period: usize, first: usize, out: &mut [f64]) {
    kurtosis_scalar(data, period, first, out)
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline]
pub unsafe fn kurtosis_avx512_long(data: &[f64], period: usize, first: usize, out: &mut [f64]) {
    kurtosis_scalar(data, period, first, out)
}

#[inline(always)]
pub fn kurtosis_batch_with_kernel(
    data: &[f64],
    sweep: &KurtosisBatchRange,
    k: Kernel,
) -> Result<KurtosisBatchOutput, KurtosisError> {
    let kernel = match k {
        Kernel::Auto => Kernel::ScalarBatch,
        other if other.is_batch() => other,
        other => return Err(KurtosisError::InvalidKernelForBatch(other)),
    };
    let simd = match kernel {
        Kernel::Avx512Batch => Kernel::Avx512,
        Kernel::Avx2Batch => Kernel::Avx2,
        Kernel::ScalarBatch => Kernel::Scalar,
        _ => unreachable!(),
    };
    kurtosis_batch_par_slice(data, sweep, simd)
}

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

impl Default for KurtosisBatchRange {
    fn default() -> Self {
        Self {
            period: (5, 254, 1),
        }
    }
}

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

impl KurtosisBatchBuilder {
    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<KurtosisBatchOutput, KurtosisError> {
        kurtosis_batch_with_kernel(data, &self.range, self.kernel)
    }
    pub fn with_default_slice(
        data: &[f64],
        k: Kernel,
    ) -> Result<KurtosisBatchOutput, KurtosisError> {
        KurtosisBatchBuilder::new().kernel(k).apply_slice(data)
    }
    pub fn apply_candles(
        self,
        c: &Candles,
        src: &str,
    ) -> Result<KurtosisBatchOutput, KurtosisError> {
        let slice = source_type(c, src);
        self.apply_slice(slice)
    }
    pub fn with_default_candles(c: &Candles) -> Result<KurtosisBatchOutput, KurtosisError> {
        KurtosisBatchBuilder::new()
            .kernel(Kernel::Auto)
            .apply_candles(c, "hl2")
    }
}

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

impl KurtosisBatchOutput {
    pub fn row_for_params(&self, p: &KurtosisParams) -> Option<usize> {
        self.combos
            .iter()
            .position(|c| c.period.unwrap_or(5) == p.period.unwrap_or(5))
    }
    pub fn values_for(&self, p: &KurtosisParams) -> Option<&[f64]> {
        self.row_for_params(p).map(|row| {
            let start = row * self.cols;
            &self.values[start..start + self.cols]
        })
    }
}

#[inline(always)]
fn expand_grid(r: &KurtosisBatchRange) -> Result<Vec<KurtosisParams>, KurtosisError> {
    fn axis_usize((start, end, step): (usize, usize, usize)) -> Result<Vec<usize>, KurtosisError> {
        if step == 0 || start == end {
            return Ok(vec![start]);
        }
        if start < end {
            return Ok((start..=end).step_by(step.max(1)).collect());
        }
        let mut v = Vec::new();
        let mut x = start as isize;
        let end_i = end as isize;
        let st = (step as isize).max(1);
        while x >= end_i {
            v.push(x as usize);
            x -= st;
        }
        if v.is_empty() {
            return Err(KurtosisError::InvalidRange {
                start: start.to_string(),
                end: end.to_string(),
                step: step.to_string(),
            });
        }
        Ok(v)
    }
    let periods = axis_usize(r.period)?;
    let mut out = Vec::with_capacity(periods.len());
    for &p in &periods {
        out.push(KurtosisParams { period: Some(p) });
    }
    Ok(out)
}

#[inline(always)]
pub fn kurtosis_batch_slice(
    data: &[f64],
    sweep: &KurtosisBatchRange,
    kern: Kernel,
) -> Result<KurtosisBatchOutput, KurtosisError> {
    kurtosis_batch_inner(data, sweep, kern, false)
}

#[inline(always)]
pub fn kurtosis_batch_par_slice(
    data: &[f64],
    sweep: &KurtosisBatchRange,
    kern: Kernel,
) -> Result<KurtosisBatchOutput, KurtosisError> {
    kurtosis_batch_inner(data, sweep, kern, true)
}

#[inline(always)]
fn kurtosis_batch_inner(
    data: &[f64],
    sweep: &KurtosisBatchRange,
    kern: Kernel,
    parallel: bool,
) -> Result<KurtosisBatchOutput, KurtosisError> {
    let combos = expand_grid(sweep)?;
    if combos.is_empty() {
        return Err(KurtosisError::InvalidRange {
            start: "range".into(),
            end: "range".into(),
            step: "empty".into(),
        });
    }

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

    let rows = combos.len();
    let cols = data.len();
    let _ = rows
        .checked_mul(cols)
        .ok_or_else(|| KurtosisError::InvalidRange {
            start: rows.to_string(),
            end: cols.to_string(),
            step: "rows*cols".into(),
        })?;
    let mut values_mu = make_uninit_matrix(rows, cols);

    let warmup_periods: Vec<usize> = combos
        .iter()
        .map(|c| first + c.period.unwrap() - 1)
        .collect();
    init_matrix_prefixes(&mut values_mu, cols, &warmup_periods);

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

    let do_row = |row: usize, out_row: &mut [f64]| unsafe {
        let period = combos[row].period.unwrap();
        match kern {
            Kernel::Scalar => kurtosis_row_scalar(data, first, period, out_row),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx2 => kurtosis_row_avx2(data, first, period, out_row),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx512 => kurtosis_row_avx512(data, first, period, out_row),
            _ => unreachable!(),
        }
    };

    if parallel {
        #[cfg(not(target_arch = "wasm32"))]
        {
            values
                .par_chunks_mut(cols)
                .enumerate()
                .for_each(|(row, slice)| do_row(row, slice));
        }

        #[cfg(target_arch = "wasm32")]
        {
            for (row, slice) in values.chunks_mut(cols).enumerate() {
                do_row(row, slice);
            }
        }
    } else {
        for (row, slice) in values.chunks_mut(cols).enumerate() {
            do_row(row, slice);
        }
    }

    let values_vec = unsafe {
        Vec::from_raw_parts(
            values_guard.as_mut_ptr() as *mut f64,
            values_guard.len(),
            values_guard.capacity(),
        )
    };

    Ok(KurtosisBatchOutput {
        values: values_vec,
        combos,
        rows,
        cols,
    })
}

#[inline(always)]
fn kurtosis_batch_inner_into(
    data: &[f64],
    sweep: &KurtosisBatchRange,
    kern: Kernel,
    parallel: bool,
    out: &mut [f64],
) -> Result<Vec<KurtosisParams>, KurtosisError> {
    let combos = expand_grid(sweep)?;
    if combos.is_empty() {
        return Err(KurtosisError::InvalidRange {
            start: "range".into(),
            end: "range".into(),
            step: "empty".into(),
        });
    }

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

    let rows = combos.len();
    let cols = data.len();
    let expected = rows
        .checked_mul(cols)
        .ok_or_else(|| KurtosisError::InvalidRange {
            start: rows.to_string(),
            end: cols.to_string(),
            step: "rows*cols".into(),
        })?;
    if out.len() != expected {
        return Err(KurtosisError::OutputLengthMismatch {
            expected,
            got: out.len(),
        });
    }

    for (row, combo) in combos.iter().enumerate() {
        let period = combo.period.unwrap();
        let warmup = first + period - 1;
        let row_start = row * cols;
        for i in 0..warmup.min(cols) {
            out[row_start + i] = f64::NAN;
        }
    }

    let out_uninit = unsafe {
        std::slice::from_raw_parts_mut(
            out.as_mut_ptr() as *mut std::mem::MaybeUninit<f64>,
            out.len(),
        )
    };

    let do_row = |row: usize, dst_mu: &mut [std::mem::MaybeUninit<f64>]| unsafe {
        let period = combos[row].period.unwrap();
        let dst = core::slice::from_raw_parts_mut(dst_mu.as_mut_ptr() as *mut f64, dst_mu.len());

        match kern {
            Kernel::Scalar => kurtosis_row_scalar(data, first, period, dst),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx2 => kurtosis_row_avx2(data, first, period, dst),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx512 => kurtosis_row_avx512(data, first, period, dst),
            _ => unreachable!(),
        }
    };

    if parallel {
        #[cfg(not(target_arch = "wasm32"))]
        {
            out_uninit
                .par_chunks_mut(cols)
                .enumerate()
                .for_each(|(row, slice)| do_row(row, slice));
        }

        #[cfg(target_arch = "wasm32")]
        {
            for (row, slice) in out_uninit.chunks_mut(cols).enumerate() {
                do_row(row, slice);
            }
        }
    } else {
        for (row, slice) in out_uninit.chunks_mut(cols).enumerate() {
            do_row(row, slice);
        }
    }

    Ok(combos)
}

#[inline(always)]
unsafe fn kurtosis_row_scalar(data: &[f64], first: usize, period: usize, out: &mut [f64]) {
    kurtosis_scalar(data, period, first, out)
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline(always)]
unsafe fn kurtosis_row_avx2(data: &[f64], first: usize, period: usize, out: &mut [f64]) {
    kurtosis_scalar(data, period, first, out)
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline(always)]
unsafe fn kurtosis_row_avx512(data: &[f64], first: usize, period: usize, out: &mut [f64]) {
    kurtosis_avx512(data, period, first, out)
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline(always)]
unsafe fn kurtosis_row_avx512_short(data: &[f64], first: usize, period: usize, out: &mut [f64]) {
    kurtosis_avx512_short(data, period, first, out)
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline(always)]
unsafe fn kurtosis_row_avx512_long(data: &[f64], first: usize, period: usize, out: &mut [f64]) {
    kurtosis_avx512_long(data, period, first, out)
}

#[derive(Debug, Clone)]
pub struct KurtosisStream {
    period: usize,
    buffer: Vec<f64>,
    head: usize,
    filled: bool,

    nan_count: usize,
    mean: f64,
    c2: f64,
    c3: f64,
    c4: f64,
    inv_n: f64,
    moments_valid: bool,
    rebuild_ctr: usize,
}

impl KurtosisStream {
    pub fn try_new(params: KurtosisParams) -> Result<Self, KurtosisError> {
        let period = params.period.unwrap_or(5);
        if period == 0 {
            return Err(KurtosisError::InvalidPeriod {
                period,
                data_len: 0,
            });
        }
        Ok(Self {
            period,
            buffer: vec![f64::NAN; period],
            head: 0,
            filled: false,
            nan_count: 0,
            mean: 0.0,
            c2: 0.0,
            c3: 0.0,
            c4: 0.0,
            inv_n: 1.0 / (period as f64),
            moments_valid: false,
            rebuild_ctr: 0,
        })
    }

    #[inline(always)]
    pub fn update(&mut self, value: f64) -> Option<f64> {
        let old = self.buffer[self.head];
        self.buffer[self.head] = value;
        self.head += 1;
        if self.head == self.period {
            self.head = 0;
            if !self.filled {
                self.filled = true;
                self.nan_count = self.buffer.iter().filter(|v| v.is_nan()).count();
                if self.nan_count > 0 {
                    self.moments_valid = false;
                    return Some(f64::NAN);
                }
                self.recompute_moments_from_ring();
                return Some(self.finalize_kurtosis());
            }
        }

        if !self.filled {
            return None;
        }

        if old.is_nan() {
            self.nan_count = self.nan_count.saturating_sub(1);
        }
        if value.is_nan() {
            self.nan_count += 1;
        }

        if self.nan_count > 0 {
            self.moments_valid = false;
            return Some(f64::NAN);
        }

        if !self.moments_valid {
            self.recompute_moments_from_ring();
            return Some(self.finalize_kurtosis());
        }

        self.rebuild_ctr += 1;
        if self.rebuild_ctr >= 1 {
            self.recompute_moments_from_ring();
            return Some(self.finalize_kurtosis());
        }

        let n = self.period as f64;
        let diff = value - old;
        let d = diff * self.inv_n;
        let mu_new = self.mean + d;

        let diff2 = diff * diff;
        let d2 = d * d;
        let inv_n2 = self.inv_n * self.inv_n;
        let inv_n3 = inv_n2 * self.inv_n;
        let d3 = diff * diff2 * inv_n2;
        let d4 = diff2 * diff2 * inv_n3;

        let c2s = self.c2 + diff2 * self.inv_n;
        let c3s = self.c3 - 3.0 * d * self.c2 - d3;
        let c4s = self.c4 - 4.0 * d * self.c3 + 6.0 * d2 * self.c2 + d4;

        let dy = old - mu_new;
        let dx = value - mu_new;
        let dy2 = dy * dy;
        let dx2 = dx * dx;

        self.c2 = c2s - dy2 + dx2;
        self.c3 = c3s - dy * dy2 + dx * dx2;
        self.c4 = c4s - (dy2 * dy2) + (dx2 * dx2);
        self.mean = mu_new;

        Some(self.finalize_kurtosis())
    }

    #[inline(always)]
    fn finalize_kurtosis(&self) -> f64 {
        let c2 = self.c2;
        let c4 = self.c4;
        let n = self.period as f64;
        if c2.abs() < f64::EPSILON * n {
            return f64::NAN;
        }
        (c4 * n) / (c2 * c2) - 3.0
    }

    #[inline(always)]
    fn recompute_moments_from_ring(&mut self) {
        debug_assert!(self.nan_count == 0);

        let n = self.period as f64;

        let mut sum = 0.0;
        for k in 0..self.period {
            let idx = (self.head + k) % self.period;
            sum += self.buffer[idx];
        }
        let mean = sum / n;

        let mut c2 = 0.0;
        let mut c3 = 0.0;
        let mut c4 = 0.0;
        for k in 0..self.period {
            let idx = (self.head + k) % self.period;
            let v = self.buffer[idx];
            let d = v - mean;
            let d2 = d * d;
            c2 += d2;
            c3 += d * d2;
            c4 += d2 * d2;
        }
        self.mean = mean;
        self.c2 = c2;
        self.c3 = c3;
        self.c4 = c4;
        self.moments_valid = true;
        self.rebuild_ctr = 0;
    }
}

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

    fn check_kurtosis_partial_params(
        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 = KurtosisParams { period: None };
        let input = KurtosisInput::from_candles(&candles, "close", default_params);
        let output = kurtosis_with_kernel(&input, kernel)?;
        assert_eq!(output.values.len(), candles.close.len());

        Ok(())
    }

    #[test]
    fn test_kurtosis_into_matches_api() -> Result<(), Box<dyn Error>> {
        let len = 256usize;
        let mut data = Vec::with_capacity(len);
        for i in 0..len {
            let x = i as f64;

            let v = (x * 0.01).sin() * 2.0 + (x * 0.007).cos() * 0.5 + x * 1e-3;
            data.push(v);
        }

        let input = KurtosisInput::from_slice(&data, KurtosisParams::default());

        let baseline = kurtosis(&input)?.values;

        let mut out = vec![0.0f64; len];
        #[cfg(not(all(target_arch = "wasm32", feature = "wasm")))]
        {
            kurtosis_into(&input, &mut out)?;
        }
        #[cfg(all(target_arch = "wasm32", feature = "wasm"))]
        {
            kurtosis_into_slice(&mut out, &input, Kernel::Auto)?;
        }

        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 in 0..len {
            assert!(
                eq_or_both_nan(baseline[i], out[i]),
                "mismatch at index {}: baseline={}, into={}",
                i,
                baseline[i],
                out[i]
            );
        }

        Ok(())
    }

    fn check_kurtosis_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 = KurtosisInput::from_candles(&candles, "hl2", KurtosisParams::default());
        let result = kurtosis_with_kernel(&input, kernel)?;
        let expected_last_five = [
            -0.5438903789933454,
            -1.6848139264816433,
            -1.6331336745945797,
            -0.6130805596586351,
            -0.027802601135927585,
        ];
        let start = result.values.len().saturating_sub(5);
        for (i, &val) in result.values[start..].iter().enumerate() {
            let diff = (val - expected_last_five[i]).abs();
            assert!(
                diff < 1e-6,
                "[{}] KURTOSIS {:?} mismatch at idx {}: got {}, expected {}",
                test_name,
                kernel,
                i,
                val,
                expected_last_five[i]
            );
        }
        Ok(())
    }

    fn check_kurtosis_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 input = KurtosisInput::with_default_candles(&candles);
        match input.data {
            KurtosisData::Candles { source, .. } => assert_eq!(source, "hl2"),
            _ => panic!("Expected KurtosisData::Candles"),
        }
        let output = kurtosis_with_kernel(&input, kernel)?;
        assert_eq!(output.values.len(), candles.close.len());

        Ok(())
    }

    fn check_kurtosis_zero_period(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let input_data = [10.0, 20.0, 30.0];
        let params = KurtosisParams { period: Some(0) };
        let input = KurtosisInput::from_slice(&input_data, params);
        let res = kurtosis_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] KURTOSIS should fail with zero period",
            test_name
        );
        Ok(())
    }

    fn check_kurtosis_period_exceeds_length(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let data_small = [10.0, 20.0, 30.0];
        let params = KurtosisParams { period: Some(10) };
        let input = KurtosisInput::from_slice(&data_small, params);
        let res = kurtosis_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] KURTOSIS should fail with period exceeding length",
            test_name
        );
        Ok(())
    }

    fn check_kurtosis_very_small_dataset(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let single_point = [42.0];
        let params = KurtosisParams { period: Some(5) };
        let input = KurtosisInput::from_slice(&single_point, params);
        let res = kurtosis_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] KURTOSIS should fail with insufficient data",
            test_name
        );
        Ok(())
    }

    fn check_kurtosis_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 = KurtosisParams { period: Some(5) };
        let first_input = KurtosisInput::from_candles(&candles, "close", first_params);
        let first_result = kurtosis_with_kernel(&first_input, kernel)?;

        let second_params = KurtosisParams { period: Some(5) };
        let second_input = KurtosisInput::from_slice(&first_result.values, second_params);
        let second_result = kurtosis_with_kernel(&second_input, kernel)?;

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

    fn check_kurtosis_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 =
            KurtosisInput::from_candles(&candles, "close", KurtosisParams { period: Some(5) });
        let res = kurtosis_with_kernel(&input, kernel)?;
        assert_eq!(res.values.len(), candles.close.len());
        if res.values.len() > 20 {
            for (i, &val) in res.values[20..].iter().enumerate() {
                assert!(
                    !val.is_nan(),
                    "[{}] Found unexpected NaN at out-index {}",
                    test_name,
                    20 + i
                );
            }
        }
        Ok(())
    }

    fn check_kurtosis_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 = 5;

        let input = KurtosisInput::from_candles(
            &candles,
            "close",
            KurtosisParams {
                period: Some(period),
            },
        );
        let batch_output = kurtosis_with_kernel(&input, kernel)?.values;

        let mut stream = KurtosisStream::try_new(KurtosisParams {
            period: Some(period),
        })?;
        let mut stream_values = Vec::with_capacity(candles.close.len());
        for &price in &candles.close {
            match stream.update(price) {
                Some(kurtosis_val) => stream_values.push(kurtosis_val),
                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,
                "[{}] KURTOSIS streaming f64 mismatch at idx {}: batch={}, stream={}, diff={}",
                test_name,
                i,
                b,
                s,
                diff
            );
        }
        Ok(())
    }

    #[cfg(debug_assertions)]
    fn check_kurtosis_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![
            KurtosisParams::default(),
            KurtosisParams { period: Some(2) },
            KurtosisParams { period: Some(3) },
            KurtosisParams { period: Some(4) },
            KurtosisParams { period: Some(5) },
            KurtosisParams { period: Some(7) },
            KurtosisParams { period: Some(10) },
            KurtosisParams { period: Some(14) },
            KurtosisParams { period: Some(20) },
            KurtosisParams { period: Some(30) },
            KurtosisParams { period: Some(50) },
            KurtosisParams { period: Some(100) },
            KurtosisParams { period: Some(200) },
            KurtosisParams { period: Some(6) },
            KurtosisParams { period: Some(25) },
            KurtosisParams { period: Some(75) },
        ];

        for (param_idx, params) in test_params.iter().enumerate() {
            let input = KurtosisInput::from_candles(&candles, "close", params.clone());
            let output = kurtosis_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={} (param set {})",
                        test_name,
                        val,
                        bits,
                        i,
                        params.period.unwrap_or(5),
                        param_idx
                    );
                }

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

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

        Ok(())
    }

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

    macro_rules! generate_all_kurtosis_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"))]
                $(
                    #[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(feature = "proptest")]
    fn check_kurtosis_property(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        use proptest::prelude::*;
        skip_if_unsupported!(kernel, test_name);

        let strat = (2usize..=50)
            .prop_flat_map(|period| {
                let min_len = period * 2;
                let max_len = 400.min(period * 20);
                (min_len..=max_len, Just(period))
            })
            .prop_flat_map(|(len, period)| {
                (
                    proptest::collection::vec(
                        (50.0f64..150.0f64).prop_flat_map(move |base| {
                            let trend = (-0.01f64..0.01f64);
                            trend.prop_flat_map(move |t| {
                                let noise = (-2.0f64..2.0f64);
                                noise.prop_map(move |n| base * (1.0 + t) + n)
                            })
                        }),
                        len,
                    ),
                    Just(period),
                )
            });

        proptest::test_runner::TestRunner::default().run(&strat, |(data, period)| {
            let params = KurtosisParams {
                period: Some(period),
            };
            let input = KurtosisInput::from_slice(&data, params.clone());

            let result = kurtosis_with_kernel(&input, kernel)?;
            let scalar_result = kurtosis_with_kernel(&input, Kernel::Scalar)?;

            prop_assert_eq!(result.values.len(), data.len(), "Output length mismatch");

            let warmup_end = period - 1;
            for i in 0..warmup_end.min(data.len()) {
                prop_assert!(
                    result.values[i].is_nan(),
                    "Expected NaN during warmup at index {}",
                    i
                );
            }

            for i in warmup_end..data.len() {
                let window_start = i + 1 - period;
                let window = &data[window_start..=i];
                let has_nan = window.iter().any(|x| x.is_nan());

                if has_nan {
                    prop_assert!(
                        result.values[i].is_nan(),
                        "Expected NaN when window contains NaN at index {}",
                        i
                    );
                } else {
                    prop_assert!(
                        result.values[i].is_finite() || result.values[i].is_nan(),
                        "Expected finite or NaN value at index {}, got {}",
                        i,
                        result.values[i]
                    );
                }
            }

            for i in warmup_end..data.len() {
                let val = result.values[i];
                let scalar_val = scalar_result.values[i];

                if val.is_nan() && scalar_val.is_nan() {
                    continue;
                }

                if val.is_finite() && scalar_val.is_finite() {
                    let val_bits = val.to_bits();
                    let scalar_bits = scalar_val.to_bits();
                    let ulp_diff = val_bits.abs_diff(scalar_bits);

                    prop_assert!(
                        (val - scalar_val).abs() <= 1e-9 || ulp_diff <= 5,
                        "Kernel mismatch at index {}: {} vs {} (ULP diff: {})",
                        i,
                        val,
                        scalar_val,
                        ulp_diff
                    );
                } else {
                    prop_assert_eq!(
                        val.is_nan(),
                        scalar_val.is_nan(),
                        "NaN mismatch at index {}",
                        i
                    );
                }
            }

            let constant_data = vec![42.0; data.len()];
            let constant_input = KurtosisInput::from_slice(&constant_data, params.clone());
            let constant_result = kurtosis_with_kernel(&constant_input, kernel)?;

            for i in warmup_end..constant_data.len() {
                prop_assert!(
                    constant_result.values[i].is_nan(),
                    "Expected NaN for constant values at index {}, got {}",
                    i,
                    constant_result.values[i]
                );
            }

            if period >= 30 && data.len() >= 100 {
                let stable_start = data.len() / 4;
                let stable_end = data.len() * 3 / 4;
                let stable_kurtosis: Vec<f64> = result.values[stable_start..stable_end]
                    .iter()
                    .filter(|x| x.is_finite())
                    .copied()
                    .collect();

                if stable_kurtosis.len() > 10 {
                    let mean_kurtosis =
                        stable_kurtosis.iter().sum::<f64>() / stable_kurtosis.len() as f64;

                    prop_assert!(
							mean_kurtosis >= -0.5 && mean_kurtosis <= 0.5,
							"Mean kurtosis {} outside expected range [-0.5, 0.5] for pseudo-normal data", mean_kurtosis
						);
                }
            }

            for i in warmup_end..data.len() {
                if result.values[i].is_finite() {
                    prop_assert!(
                        result.values[i] >= -2.0 - 1e-10,
                        "Kurtosis {} at index {} violates theoretical minimum of -2.0",
                        result.values[i],
                        i
                    );
                }
            }

            if data.len() > period * 2 && period >= 3 {
                let mut outlier_data = data.clone();
                let mid = data.len() / 2;
                if mid >= period {
                    let window_start = mid - period + 1;
                    let window = &data[window_start..=mid];
                    let mean = window.iter().sum::<f64>() / period as f64;
                    let variance =
                        window.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / period as f64;
                    let std_dev = variance.sqrt();

                    outlier_data[mid] = mean + std_dev * 10.0;

                    let outlier_input = KurtosisInput::from_slice(&outlier_data, params.clone());
                    let outlier_result = kurtosis_with_kernel(&outlier_input, kernel)?;

                    if result.values[mid].is_finite()
                        && outlier_result.values[mid].is_finite()
                        && std_dev > 0.01
                    {
                        prop_assert!(
                            outlier_result.values[mid] > result.values[mid],
                            "Outlier should increase kurtosis: original {}, with outlier {}",
                            result.values[mid],
                            outlier_result.values[mid]
                        );

                        let kurtosis_increase = outlier_result.values[mid] - result.values[mid];
                        prop_assert!(
								kurtosis_increase > 0.5,
								"Outlier should substantially increase kurtosis: increase of {} is too small",
								kurtosis_increase
							);
                    }
                }
            }

            if period >= 4 {
                let uniform_data: Vec<f64> = (0..data.len())
                    .map(|i| {
                        let base = (i / period) as f64 * 10.0;

                        base + ((i % period) as f64) * 0.001
                    })
                    .collect();

                let uniform_input = KurtosisInput::from_slice(&uniform_data, params.clone());
                let uniform_result = kurtosis_with_kernel(&uniform_input, kernel)?;

                let check_start = warmup_end + period;
                let check_end = (check_start + 5).min(uniform_data.len());

                for i in check_start..check_end {
                    if uniform_result.values[i].is_finite() {
                        prop_assert!(
								uniform_result.values[i] < 0.0,
								"Nearly uniform distribution should have negative excess kurtosis at index {}, got {}",
								i, uniform_result.values[i]
							);
                    }
                }
            }

            Ok(())
        })?;

        Ok(())
    }

    generate_all_kurtosis_tests!(
        check_kurtosis_partial_params,
        check_kurtosis_accuracy,
        check_kurtosis_default_candles,
        check_kurtosis_zero_period,
        check_kurtosis_period_exceeds_length,
        check_kurtosis_very_small_dataset,
        check_kurtosis_reinput,
        check_kurtosis_nan_handling,
        check_kurtosis_streaming,
        check_kurtosis_no_poison
    );

    #[cfg(feature = "proptest")]
    generate_all_kurtosis_tests!(check_kurtosis_property);

    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 = KurtosisBatchBuilder::new()
            .kernel(kernel)
            .apply_candles(&c, "hl2")?;

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

        assert_eq!(row.len(), c.close.len());

        let expected = [
            -0.5438903789933454,
            -1.6848139264816433,
            -1.6331336745945797,
            -0.6130805596586351,
            -0.027802601135927585,
        ];
        let start = row.len() - 5;
        for (i, &v) in row[start..].iter().enumerate() {
            assert!(
                (v - expected[i]).abs() < 1e-6,
                "[{test}] default-row mismatch at idx {i}: {v} vs {expected:?}"
            );
        }
        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 data = source_type(&c, "close");

        let test_configs = vec![
            (2, 10, 2),
            (5, 25, 5),
            (30, 60, 15),
            (2, 5, 1),
            (10, 20, 2),
            (20, 50, 10),
            (5, 5, 0),
        ];

        for (cfg_idx, &(p_start, p_end, p_step)) in test_configs.iter().enumerate() {
            let output = KurtosisBatchBuilder::new()
                .kernel(kernel)
                .period_range(p_start, p_end, p_step)
                .apply_slice(data)?;

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

                let bits = val.to_bits();
                let row = idx / output.cols;
                let col = idx % output.cols;
                let combo = &output.combos[row];

                if bits == 0x11111111_11111111 {
                    panic!(
                        "[{}] Config {}: Found alloc_with_nan_prefix poison value {} (0x{:016X}) \
						 at row {} col {} (flat index {}) with params: period={}",
                        test,
                        cfg_idx,
                        val,
                        bits,
                        row,
                        col,
                        idx,
                        combo.period.unwrap_or(5)
                    );
                }

                if bits == 0x22222222_22222222 {
                    panic!(
                        "[{}] Config {}: Found init_matrix_prefixes poison value {} (0x{:016X}) \
						 at row {} col {} (flat index {}) with params: period={}",
                        test,
                        cfg_idx,
                        val,
                        bits,
                        row,
                        col,
                        idx,
                        combo.period.unwrap_or(5)
                    );
                }

                if bits == 0x33333333_33333333 {
                    panic!(
                        "[{}] Config {}: Found make_uninit_matrix poison value {} (0x{:016X}) \
						 at row {} col {} (flat index {}) with params: period={}",
                        test,
                        cfg_idx,
                        val,
                        bits,
                        row,
                        col,
                        idx,
                        combo.period.unwrap_or(5)
                    );
                }
            }
        }

        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_no_poison);
}

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

    let slice_in = data.as_slice()?;
    let kern = validate_kernel(kernel, false)?;

    let params = KurtosisParams {
        period: Some(period),
    };
    let input = KurtosisInput::from_slice(slice_in, params);

    let result_vec: Vec<f64> = py
        .allow_threads(|| kurtosis_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")]
#[pyclass(name = "KurtosisStream")]
pub struct KurtosisStreamPy {
    stream: KurtosisStream,
}

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

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

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

    let slice_in = data.as_slice()?;
    let kern = validate_kernel(kernel, true)?;

    let sweep = KurtosisBatchRange {
        period: period_range,
    };

    let combos = expand_grid(&sweep).map_err(|e| PyValueError::new_err(e.to_string()))?;
    let rows = combos.len();
    let cols = slice_in.len();

    let expected = rows
        .checked_mul(cols)
        .ok_or_else(|| PyValueError::new_err("kurtosis: rows*cols overflow"))?;
    let out_arr = unsafe { PyArray1::<f64>::new(py, [expected], false) };
    let slice_out = unsafe { out_arr.as_slice_mut()? };

    let combos = py
        .allow_threads(|| {
            let kernel = match kern {
                Kernel::Auto => detect_best_batch_kernel(),
                k => k,
            };
            let simd = match kernel {
                Kernel::Avx512Batch => Kernel::Avx512,
                Kernel::Avx2Batch => Kernel::Avx2,
                Kernel::ScalarBatch => Kernel::Scalar,
                _ => unreachable!(),
            };
            kurtosis_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)
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyfunction(name = "kurtosis_cuda_batch_dev")]
#[pyo3(signature = (data_f32, period_range, device_id=0))]
pub fn kurtosis_cuda_batch_dev_py(
    py: Python<'_>,
    data_f32: numpy::PyReadonlyArray1<'_, f32>,
    period_range: (usize, usize, usize),
    device_id: usize,
) -> PyResult<DeviceArrayF32Py> {
    use crate::cuda::cuda_available;
    use crate::cuda::kurtosis_wrapper::CudaKurtosis;
    if !cuda_available() {
        return Err(PyValueError::new_err("CUDA not available"));
    }
    let slice_in = data_f32.as_slice()?;
    let sweep = KurtosisBatchRange {
        period: period_range,
    };
    let inner = py.allow_threads(|| {
        let cuda =
            CudaKurtosis::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        let (dev, _combos) = cuda
            .kurtosis_batch_dev(slice_in, &sweep)
            .map_err(|e| PyValueError::new_err(e.to_string()))?;
        Ok::<_, PyErr>(dev)
    })?;
    let handle = make_device_array_py(device_id, inner)?;
    Ok(handle)
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyfunction(name = "kurtosis_cuda_many_series_one_param_dev")]
#[pyo3(signature = (data_tm_f32, period, device_id=0))]
pub fn kurtosis_cuda_many_series_one_param_dev_py(
    py: Python<'_>,
    data_tm_f32: numpy::PyReadonlyArray2<'_, f32>,
    period: usize,
    device_id: usize,
) -> PyResult<DeviceArrayF32Py> {
    use crate::cuda::cuda_available;
    use crate::cuda::kurtosis_wrapper::CudaKurtosis;
    if !cuda_available() {
        return Err(PyValueError::new_err("CUDA not available"));
    }
    let shape = data_tm_f32.shape();
    if shape.len() != 2 {
        return Err(PyValueError::new_err("expected 2D time-major array"));
    }
    let rows = shape[0];
    let cols = shape[1];
    let slice_in = data_tm_f32.as_slice()?;
    let inner = py.allow_threads(|| {
        let cuda =
            CudaKurtosis::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        let dev = cuda
            .kurtosis_many_series_one_param_time_major_dev(slice_in, cols, rows, period)
            .map_err(|e| PyValueError::new_err(e.to_string()))?;
        Ok::<_, PyErr>(dev)
    })?;
    let handle = make_device_array_py(device_id, inner)?;
    Ok(handle)
}

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

    let mut output = vec![0.0; data.len()];

    kurtosis_into_slice(&mut output, &input, Kernel::Auto)
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    Ok(output)
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn kurtosis_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 kurtosis_free(ptr: *mut f64, len: usize) {
    if !ptr.is_null() {
        unsafe {
            let _ = Vec::from_raw_parts(ptr, len, len);
        }
    }
}

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn kurtosis_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 kurtosis_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 = KurtosisParams {
            period: Some(period),
        };
        let input = KurtosisInput::from_slice(data, params);

        if in_ptr == out_ptr {
            let mut temp = vec![0.0; len];
            kurtosis_into_slice(&mut temp, &input, Kernel::Auto)
                .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);
            kurtosis_into_slice(out, &input, Kernel::Auto)
                .map_err(|e| JsValue::from_str(&e.to_string()))?;
        }

        Ok(())
    }
}

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

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

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen(js_name = kurtosis_batch)]
pub fn kurtosis_batch_unified_js(data: &[f64], config: JsValue) -> Result<JsValue, JsValue> {
    let config: KurtosisBatchConfig = serde_wasm_bindgen::from_value(config)
        .map_err(|e| JsValue::from_str(&format!("Invalid config: {}", e)))?;

    let sweep = KurtosisBatchRange {
        period: config.period_range,
    };

    let output = kurtosis_batch_with_kernel(data, &sweep, Kernel::Auto)
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    let js_output = KurtosisBatchJsOutput {
        values: output.values,
        periods: output.combos.iter().map(|c| c.period.unwrap()).collect(),
        combos: output.combos,
        rows: output.rows,
        cols: output.cols,
    };

    serde_wasm_bindgen::to_value(&js_output)
        .map_err(|e| JsValue::from_str(&format!("Serialization error: {}", e)))
}

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

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

        let sweep = KurtosisBatchRange {
            period: (period_start, period_end, period_step),
        };

        let combos = expand_grid(&sweep).map_err(|e| JsValue::from_str(&e.to_string()))?;
        let rows = combos.len();
        let cols = len;

        let expected = rows.checked_mul(cols).ok_or_else(|| {
            JsValue::from_str(
                &KurtosisError::InvalidRange {
                    start: rows.to_string(),
                    end: cols.to_string(),
                    step: "rows*cols".into(),
                }
                .to_string(),
            )
        })?;
        let out = std::slice::from_raw_parts_mut(out_ptr, expected);

        let kernel = detect_best_kernel();
        kurtosis_batch_inner_into(data, &sweep, kernel, false, out)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;

        Ok(rows)
    }
}
