#[cfg(all(feature = "python", feature = "cuda"))]
use crate::cuda::cuda_available;
#[cfg(all(feature = "python", feature = "cuda"))]
use crate::cuda::moving_averages::alma_wrapper::DeviceArrayF32;
#[cfg(all(feature = "python", feature = "cuda"))]
use crate::cuda::moving_averages::CudaEpma;
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(all(feature = "nightly-avx", target_arch = "x86_64"))]
use core::arch::x86_64::*;
#[cfg(all(feature = "python", feature = "cuda"))]
use cust::context::Context;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use std::convert::AsRef;
use std::mem::{ManuallyDrop, MaybeUninit};
#[cfg(all(feature = "python", feature = "cuda"))]
use std::sync::Arc;
use thiserror::Error;

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

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

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

#[derive(Debug, Clone)]
#[cfg_attr(
    all(target_arch = "wasm32", feature = "wasm"),
    derive(serde::Serialize, serde::Deserialize)
)]
pub struct EpmaParams {
    pub period: Option<usize>,
    pub offset: Option<usize>,
}
impl Default for EpmaParams {
    fn default() -> Self {
        Self {
            period: Some(11),
            offset: Some(4),
        }
    }
}

#[derive(Debug, Clone)]
pub struct EpmaInput<'a> {
    pub data: EpmaData<'a>,
    pub params: EpmaParams,
}

impl<'a> EpmaInput<'a> {
    #[inline]
    pub fn from_candles(c: &'a Candles, s: &'a str, p: EpmaParams) -> Self {
        Self {
            data: EpmaData::Candles {
                candles: c,
                source: s,
            },
            params: p,
        }
    }
    #[inline]
    pub fn from_slice(sl: &'a [f64], p: EpmaParams) -> Self {
        Self {
            data: EpmaData::Slice(sl),
            params: p,
        }
    }
    #[inline]
    pub fn with_default_candles(c: &'a Candles) -> Self {
        Self::from_candles(c, "close", EpmaParams::default())
    }
    #[inline]
    pub fn get_period(&self) -> usize {
        self.params.period.unwrap_or(11)
    }
    #[inline]
    pub fn get_offset(&self) -> usize {
        self.params.offset.unwrap_or(4)
    }
}

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

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

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

    #[error("epma: Invalid offset: {offset}")]
    InvalidOffset { offset: usize },

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

    #[error("epma: output length mismatch: expected = {expected}, got = {got}")]
    OutputLengthMismatch { expected: usize, got: usize },

    #[error("epma: Invalid kernel for batch operation: got {0:?}")]
    InvalidKernelForBatch(Kernel),

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

    #[error("epma: size overflow computing rows*cols: rows={rows}, cols={cols}")]
    SizeOverflow { rows: usize, cols: usize },
}

#[derive(Copy, Clone, Debug)]
pub struct EpmaBuilder {
    period: Option<usize>,
    offset: Option<usize>,
    kernel: Kernel,
}
impl Default for EpmaBuilder {
    fn default() -> Self {
        Self {
            period: None,
            offset: None,
            kernel: Kernel::Auto,
        }
    }
}
impl EpmaBuilder {
    #[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 offset(mut self, o: usize) -> Self {
        self.offset = Some(o);
        self
    }
    #[inline(always)]
    pub fn kernel(mut self, k: Kernel) -> Self {
        self.kernel = k;
        self
    }
    #[inline(always)]
    pub fn apply(self, c: &Candles) -> Result<EpmaOutput, EpmaError> {
        let p = EpmaParams {
            period: self.period,
            offset: self.offset,
        };
        let i = EpmaInput::from_candles(c, "close", p);
        epma_with_kernel(&i, self.kernel)
    }
    #[inline(always)]
    pub fn apply_slice(self, d: &[f64]) -> Result<EpmaOutput, EpmaError> {
        let p = EpmaParams {
            period: self.period,
            offset: self.offset,
        };
        let i = EpmaInput::from_slice(d, p);
        epma_with_kernel(&i, self.kernel)
    }
    #[inline(always)]
    pub fn into_stream(self) -> Result<EpmaStream, EpmaError> {
        let p = EpmaParams {
            period: self.period,
            offset: self.offset,
        };
        EpmaStream::try_new(p)
    }
}

#[inline]
pub fn epma(input: &EpmaInput) -> Result<EpmaOutput, EpmaError> {
    epma_with_kernel(input, Kernel::Auto)
}

#[inline(always)]
fn epma_prepare<'a>(
    input: &'a EpmaInput,
    kernel: Kernel,
) -> Result<(&'a [f64], usize, usize, usize, usize, Kernel), EpmaError> {
    let data: &[f64] = input.as_ref();
    let len = data.len();
    if len == 0 {
        return Err(EpmaError::EmptyInputData);
    }

    let first = data
        .iter()
        .position(|x| !x.is_nan())
        .ok_or(EpmaError::AllValuesNaN)?;
    let period = input.get_period();
    let offset = input.get_offset();

    if offset >= period {
        return Err(EpmaError::InvalidOffset { offset });
    }

    if period < 2 || period > len {
        return Err(EpmaError::InvalidPeriod {
            period,
            data_len: len,
        });
    }
    let needed = period + offset + 1;
    if (len - first) < needed {
        return Err(EpmaError::NotEnoughValidData {
            needed,
            valid: len - first,
        });
    }

    let chosen = match kernel {
        Kernel::Auto => detect_best_kernel(),
        other => other,
    };
    let warmup = first + period + offset + 1;

    Ok((data, period, offset, first, warmup, chosen))
}

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

        match kernel {
            Kernel::Scalar | Kernel::ScalarBatch => epma_scalar(data, period, offset, first, out),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx2 | Kernel::Avx2Batch => epma_avx2(data, period, offset, first, out),
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx512 | Kernel::Avx512Batch => epma_avx512(data, period, offset, first, out),
            #[cfg(not(all(feature = "nightly-avx", target_arch = "x86_64")))]
            Kernel::Avx2 | Kernel::Avx2Batch | Kernel::Avx512 | Kernel::Avx512Batch => {
                epma_scalar(data, period, offset, first, out)
            }
            _ => unreachable!(),
        }
    }
}

pub fn epma_with_kernel(input: &EpmaInput, kernel: Kernel) -> Result<EpmaOutput, EpmaError> {
    let (data, period, offset, first, warmup, chosen) = epma_prepare(input, kernel)?;

    let mut out = alloc_with_nan_prefix(data.len(), warmup);
    epma_compute_into(data, period, offset, first, chosen, &mut out);

    Ok(EpmaOutput { values: out })
}

#[cfg(not(all(target_arch = "wasm32", feature = "wasm")))]
#[inline]
pub fn epma_into(input: &EpmaInput, out: &mut [f64]) -> Result<(), EpmaError> {
    let (data, period, offset, first, warmup, chosen) = epma_prepare(input, Kernel::Auto)?;

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

    let w = warmup.min(out.len());
    for v in &mut out[..w] {
        *v = f64::from_bits(0x7ff8_0000_0000_0000);
    }

    epma_compute_into(data, period, offset, first, chosen, out);
    Ok(())
}

#[inline]
pub fn epma_into_slice(dst: &mut [f64], input: &EpmaInput, kern: Kernel) -> Result<(), EpmaError> {
    let (data, period, offset, first, warmup, chosen) = epma_prepare(input, kern)?;

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

    epma_compute_into(data, period, offset, first, chosen, dst);

    for v in &mut dst[..warmup] {
        *v = f64::from_bits(0x7ff8_0000_0000_0000);
    }

    Ok(())
}

#[inline(always)]
pub fn epma_scalar(
    data: &[f64],
    period: usize,
    offset: usize,
    first_valid: usize,
    out: &mut [f64],
) {
    let n = data.len();
    let p1 = period - 1;

    let c0 = 2.0 - (offset as f64);
    let p1f = p1 as f64;
    let weight_sum = p1f.mul_add(c0, 0.5 * (p1f - 1.0) * p1f);
    let inv = 1.0 / weight_sum;

    for j in (first_valid + period + offset + 1)..n {
        let start = j + 1 - p1;

        let mut sum = 0.0;
        let mut i = 0usize;
        let mut wi = c0;

        while i + 3 < p1 {
            sum = data[start + i].mul_add(wi, sum);
            sum = data[start + i + 1].mul_add(wi + 1.0, sum);
            sum = data[start + i + 2].mul_add(wi + 2.0, sum);
            sum = data[start + i + 3].mul_add(wi + 3.0, sum);
            i += 4;
            wi += 4.0;
        }

        while i < p1 {
            sum = data[start + i].mul_add(wi, sum);
            i += 1;
            wi += 1.0;
        }

        out[j] = sum * inv;
    }
}

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

    const STEP: usize = 2;
    let n = data.len();
    let p1 = period - 1;

    let mut weights = Vec::with_capacity(p1);
    let mut weight_sum = 0.0;
    for i in 0..p1 {
        let w = (period as i32 - i as i32 - offset as i32) as f64;
        weights.push(w);
        weight_sum += w;
    }

    let chunks = p1 / STEP;
    let tail = p1 % STEP;

    for j in (first_valid + period + offset + 1)..n {
        let start = j + 1 - p1;
        let mut acc = f64x2_splat(0.0);

        for blk in 0..chunks {
            let idx = blk * STEP;

            let w0 = weights[p1 - 1 - idx];
            let w1 = weights[p1 - 2 - idx];
            let w = f64x2(w0, w1);

            let d = v128_load(data.as_ptr().add(start + idx) as *const v128);
            acc = f64x2_add(acc, f64x2_mul(d, w));
        }

        let mut sum = f64x2_extract_lane::<0>(acc) + f64x2_extract_lane::<1>(acc);

        if tail != 0 {
            sum += data[start + p1 - 1] * weights[0];
        }

        out[j] = sum / weight_sum;
    }
}

#[inline(always)]
fn build_weights_rev(period: usize, offset: usize) -> (Vec<f64>, f64) {
    let p1 = period - 1;
    let mut w = Vec::with_capacity(p1);
    let mut sum = 0.0;

    for k in 0..p1 {
        let val = (period as isize - (p1 - 1 - k) as isize - offset as isize) as f64;
        w.push(val);
        sum += val;
    }
    (w, sum)
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[target_feature(enable = "avx2,fma")]
pub unsafe fn epma_avx2(
    data: &[f64],
    period: usize,
    offset: usize,
    first_valid: usize,
    out: &mut [f64],
) {
    const STEP: usize = 4;
    let p1 = period - 1;
    let chunks = p1 / STEP;
    let tail = p1 % STEP;
    let mask = match tail {
        0 => _mm256_setzero_si256(),
        1 => _mm256_setr_epi64x(-1, 0, 0, 0),
        2 => _mm256_setr_epi64x(-1, -1, 0, 0),
        3 => _mm256_setr_epi64x(-1, -1, -1, 0),
        _ => unreachable!(),
    };

    let c0 = 2.0 - (offset as f64);
    let p1f = p1 as f64;
    let wsum = p1f.mul_add(c0, 0.5 * (p1f - 1.0) * p1f);
    let inv = 1.0 / wsum;

    let ramp = _mm256_setr_pd(0.0, 1.0, 2.0, 3.0);

    for j in (first_valid + period + offset + 1)..data.len() {
        let start = j + 1 - p1;
        let base_ptr = data.as_ptr().add(start);

        let mut acc = _mm256_setzero_pd();

        for blk in 0..chunks {
            let base_w = c0 + (blk * STEP) as f64;
            let w = _mm256_add_pd(_mm256_set1_pd(base_w), ramp);
            let d = _mm256_loadu_pd(base_ptr.add(blk * STEP));
            acc = _mm256_fmadd_pd(d, w, acc);
        }

        if tail != 0 {
            let base_w = c0 + (chunks * STEP) as f64;
            let w_t = _mm256_add_pd(_mm256_set1_pd(base_w), ramp);
            let d_t = _mm256_maskload_pd(base_ptr.add(chunks * STEP), mask);
            acc = _mm256_fmadd_pd(d_t, w_t, acc);
        }

        let hi = _mm256_extractf128_pd(acc, 1);
        let lo = _mm256_castpd256_pd128(acc);
        let s2 = _mm_add_pd(hi, lo);
        let s1 = _mm_add_pd(s2, _mm_unpackhi_pd(s2, s2));
        *out.get_unchecked_mut(j) = _mm_cvtsd_f64(s1) * inv;
    }
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline]
pub unsafe fn epma_avx512(
    data: &[f64],
    period: usize,
    offset: usize,
    first_valid: usize,
    out: &mut [f64],
) {
    if period <= 32 {
        epma_avx512_short(data, period, offset, first_valid, out)
    } else {
        epma_avx512_long(data, period, offset, first_valid, out)
    }
}
#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[target_feature(enable = "avx512f,avx512dq,fma")]
#[inline]
unsafe fn epma_avx512_short(
    data: &[f64],
    period: usize,
    offset: usize,
    first_valid: usize,
    out: &mut [f64],
) {
    const STEP: usize = 8;
    let p1 = period - 1;
    let chunks = p1 / STEP;
    let tail = p1 % STEP;
    let tmask: __mmask8 = (1u8 << tail).wrapping_sub(1);

    let c0 = 2.0 - (offset as f64);
    let p1f = p1 as f64;
    let wsum = p1f.mul_add(c0, 0.5 * (p1f - 1.0) * p1f);
    let inv = 1.0 / wsum;

    let ramp = _mm512_setr_pd(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0);

    for j in (first_valid + period + offset + 1)..data.len() {
        let start = j + 1 - p1;
        let base_ptr = data.as_ptr().add(start);
        let mut acc = _mm512_setzero_pd();

        for blk in 0..chunks {
            let base_w = c0 + (blk * STEP) as f64;
            let w = _mm512_add_pd(_mm512_set1_pd(base_w), ramp);
            let d = _mm512_loadu_pd(base_ptr.add(blk * STEP));
            acc = _mm512_fmadd_pd(d, w, acc);
        }

        if tail != 0 {
            let base_w = c0 + (chunks * STEP) as f64;
            let w_t = _mm512_add_pd(_mm512_set1_pd(base_w), ramp);
            let d_t = _mm512_maskz_loadu_pd(tmask, base_ptr.add(chunks * STEP));
            acc = _mm512_fmadd_pd(d_t, w_t, acc);
        }

        *out.get_unchecked_mut(j) = _mm512_reduce_add_pd(acc) * inv;
    }
}
#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[target_feature(enable = "avx512f,avx512dq,fma")]
unsafe fn epma_avx512_long(
    data: &[f64],
    period: usize,
    offset: usize,
    first_valid: usize,
    out: &mut [f64],
) {
    const STEP: usize = 8;
    let p1 = period - 1;
    let n_chunks = p1 / STEP;
    let tail_len = p1 % STEP;
    let paired = n_chunks & !3;
    let tmask: __mmask8 = (1u8 << tail_len).wrapping_sub(1);

    let c0 = 2.0 - (offset as f64);
    let p1f = p1 as f64;
    let wsum = p1f.mul_add(c0, 0.5 * (p1f - 1.0) * p1f);
    let inv = 1.0 / wsum;

    let ramp = _mm512_setr_pd(0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0);

    for j in (first_valid + period + offset + 1)..data.len() {
        let start_ptr = data.as_ptr().add(j + 1 - p1);

        let mut s0 = _mm512_setzero_pd();
        let mut s1 = _mm512_setzero_pd();
        let mut s2 = _mm512_setzero_pd();
        let mut s3 = _mm512_setzero_pd();

        let mut blk = 0usize;
        while blk < paired {
            let base0 = c0 + ((blk + 0) * STEP) as f64;
            let base1 = c0 + ((blk + 1) * STEP) as f64;
            let base2 = c0 + ((blk + 2) * STEP) as f64;
            let base3 = c0 + ((blk + 3) * STEP) as f64;

            let w0 = _mm512_add_pd(_mm512_set1_pd(base0), ramp);
            let w1 = _mm512_add_pd(_mm512_set1_pd(base1), ramp);
            let w2 = _mm512_add_pd(_mm512_set1_pd(base2), ramp);
            let w3 = _mm512_add_pd(_mm512_set1_pd(base3), ramp);

            let d0 = _mm512_loadu_pd(start_ptr.add((blk + 0) * STEP));
            let d1 = _mm512_loadu_pd(start_ptr.add((blk + 1) * STEP));
            let d2 = _mm512_loadu_pd(start_ptr.add((blk + 2) * STEP));
            let d3 = _mm512_loadu_pd(start_ptr.add((blk + 3) * STEP));

            s0 = _mm512_fmadd_pd(d0, w0, s0);
            s1 = _mm512_fmadd_pd(d1, w1, s1);
            s2 = _mm512_fmadd_pd(d2, w2, s2);
            s3 = _mm512_fmadd_pd(d3, w3, s3);

            blk += 4;
        }

        while blk < n_chunks {
            let base = c0 + (blk * STEP) as f64;
            let w = _mm512_add_pd(_mm512_set1_pd(base), ramp);
            let d = _mm512_loadu_pd(start_ptr.add(blk * STEP));
            s0 = _mm512_fmadd_pd(d, w, s0);
            blk += 1;
        }

        if tail_len != 0 {
            let base = c0 + (n_chunks * STEP) as f64;
            let w_t = _mm512_add_pd(_mm512_set1_pd(base), ramp);
            let d_t = _mm512_maskz_loadu_pd(tmask, start_ptr.add(n_chunks * STEP));
            s0 = _mm512_fmadd_pd(d_t, w_t, s0);
        }

        let total = _mm512_add_pd(_mm512_add_pd(s0, s1), _mm512_add_pd(s2, s3));
        *out.get_unchecked_mut(j) = _mm512_reduce_add_pd(total) * inv;
    }
}

#[derive(Debug, Clone)]
pub struct EpmaStream {
    period: usize,
    offset: usize,
    p1: usize,

    buffer: Vec<f64>,
    head: usize,

    seen: usize,
    included: usize,

    sum: f64,
    sum_c: f64,
    ramp: f64,
    ramp_c: f64,

    c0: f64,
    inv_wsum: f64,
}

#[inline(always)]
fn kahan_add(sum: &mut f64, c: &mut f64, x: f64) {
    let y = x - *c;
    let t = *sum + y;
    *c = (t - *sum) - y;
    *sum = t;
}

impl EpmaStream {
    pub fn try_new(params: EpmaParams) -> Result<Self, EpmaError> {
        let period = params.period.unwrap_or(11);
        let offset = params.offset.unwrap_or(4);

        if period < 2 {
            return Err(EpmaError::InvalidPeriod {
                period,
                data_len: 0,
            });
        }
        if offset >= period {
            return Err(EpmaError::InvalidOffset { offset });
        }

        let p1 = period - 1;
        let c0 = 2.0 - (offset as f64);

        let p1f = p1 as f64;
        let wsum = p1f.mul_add(c0, 0.5 * (p1f - 1.0) * p1f);
        let inv_wsum = 1.0 / wsum;

        Ok(Self {
            period,
            offset,
            p1,

            buffer: vec![0.0; period],
            head: 0,

            seen: 0,
            included: 0,

            sum: 0.0,
            sum_c: 0.0,
            ramp: 0.0,
            ramp_c: 0.0,

            c0,
            inv_wsum,
        })
    }

    #[inline(always)]
    pub fn update(&mut self, value: f64) -> Option<f64> {
        let p = self.period;
        let p1m1 = (self.p1 - 1) as f64;

        let idx_out = (self.head + 1) % p;
        let x_out = if self.included == self.p1 {
            self.buffer[idx_out]
        } else {
            0.0
        };

        self.buffer[self.head] = value;
        self.head = (self.head + 1) % p;
        self.seen += 1;

        if self.included < self.p1 {
            let m = self.included as f64;

            kahan_add(&mut self.sum, &mut self.sum_c, value);

            kahan_add(&mut self.ramp, &mut self.ramp_c, m * value);

            self.included += 1;
        } else {
            let s_old = self.sum;

            let delta_s = value - x_out;
            kahan_add(&mut self.sum, &mut self.sum_c, delta_s);

            let delta_r = p1m1.mul_add(value, x_out - s_old);
            kahan_add(&mut self.ramp, &mut self.ramp_c, delta_r);
        }

        if self.seen <= (self.period + self.offset + 1) {
            return Some(value);
        }

        let num = self.c0.mul_add(self.sum, self.ramp);
        Some(num * self.inv_wsum)
    }
}

#[derive(Clone, Debug)]
pub struct EpmaBatchRange {
    pub period: (usize, usize, usize),
    pub offset: (usize, usize, usize),
}
impl Default for EpmaBatchRange {
    fn default() -> Self {
        Self {
            period: (11, 260, 1),
            offset: (4, 4, 0),
        }
    }
}
#[derive(Clone, Debug, Default)]
pub struct EpmaBatchBuilder {
    range: EpmaBatchRange,
    kernel: Kernel,
}
impl EpmaBatchBuilder {
    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
    }
    #[inline]
    pub fn offset_range(mut self, start: usize, end: usize, step: usize) -> Self {
        self.range.offset = (start, end, step);
        self
    }
    #[inline]
    pub fn offset_static(mut self, o: usize) -> Self {
        self.range.offset = (o, o, 0);
        self
    }
    pub fn apply_slice(self, data: &[f64]) -> Result<EpmaBatchOutput, EpmaError> {
        epma_batch_with_kernel(data, &self.range, self.kernel)
    }
    pub fn with_default_slice(data: &[f64], k: Kernel) -> Result<EpmaBatchOutput, EpmaError> {
        EpmaBatchBuilder::new().kernel(k).apply_slice(data)
    }
    pub fn apply_candles(self, c: &Candles, src: &str) -> Result<EpmaBatchOutput, EpmaError> {
        let slice = source_type(c, src);
        self.apply_slice(slice)
    }
    pub fn with_default_candles(c: &Candles) -> Result<EpmaBatchOutput, EpmaError> {
        EpmaBatchBuilder::new()
            .kernel(Kernel::Auto)
            .apply_candles(c, "close")
    }
}

#[derive(Clone, Debug)]
pub struct EpmaBatchOutput {
    pub values: Vec<f64>,
    pub combos: Vec<EpmaParams>,
    pub rows: usize,
    pub cols: usize,
}
impl EpmaBatchOutput {
    pub fn row_for_params(&self, p: &EpmaParams) -> Option<usize> {
        self.combos.iter().position(|c| {
            c.period.unwrap_or(11) == p.period.unwrap_or(11)
                && c.offset.unwrap_or(4) == p.offset.unwrap_or(4)
        })
    }
    pub fn values_for(&self, p: &EpmaParams) -> 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: &EpmaBatchRange) -> Vec<EpmaParams> {
    fn axis_usize((start, end, step): (usize, usize, usize)) -> Vec<usize> {
        if step == 0 {
            return vec![start];
        }
        let (lo, hi) = if start <= end {
            (start, end)
        } else {
            (end, start)
        };
        (lo..=hi).step_by(step).collect()
    }
    let periods = axis_usize(r.period);
    let offsets = axis_usize(r.offset);
    let mut out = Vec::with_capacity(periods.len() * offsets.len());
    for &p in &periods {
        for &o in &offsets {
            out.push(EpmaParams {
                period: Some(p),
                offset: Some(o),
            });
        }
    }
    out
}

#[inline(always)]
pub fn epma_batch_with_kernel(
    data: &[f64],
    sweep: &EpmaBatchRange,
    k: Kernel,
) -> Result<EpmaBatchOutput, EpmaError> {
    let kernel = match k {
        Kernel::Auto => detect_best_batch_kernel(),
        other if other.is_batch() => other,
        other => return Err(EpmaError::InvalidKernelForBatch(other)),
    };
    let simd = match kernel {
        #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
        Kernel::Avx512Batch => Kernel::Avx512,
        #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
        Kernel::Avx2Batch => Kernel::Avx2,
        Kernel::ScalarBatch => Kernel::Scalar,
        _ => unreachable!(),
    };

    epma_batch_par_slice(data, sweep, simd)
}

#[inline(always)]
pub fn epma_batch_slice(
    data: &[f64],
    sweep: &EpmaBatchRange,
    kern: Kernel,
) -> Result<EpmaBatchOutput, EpmaError> {
    epma_batch_inner(data, sweep, kern, false)
}
#[inline(always)]
pub fn epma_batch_par_slice(
    data: &[f64],
    sweep: &EpmaBatchRange,
    kern: Kernel,
) -> Result<EpmaBatchOutput, EpmaError> {
    epma_batch_inner(data, sweep, kern, true)
}
#[inline(always)]
fn epma_batch_inner(
    data: &[f64],
    sweep: &EpmaBatchRange,
    kern: Kernel,
    parallel: bool,
) -> Result<EpmaBatchOutput, EpmaError> {
    let combos = expand_grid(sweep);
    let rows = combos.len();
    let cols = data.len();

    let _total_cells = rows
        .checked_mul(cols)
        .ok_or(EpmaError::SizeOverflow { rows, cols })?;

    let mut buf_mu = make_uninit_matrix(rows, cols);

    let combos = epma_batch_inner_into_uninit(data, sweep, kern, parallel, &mut buf_mu)?;

    let values = unsafe {
        let mut buf_guard = ManuallyDrop::new(buf_mu);
        Vec::from_raw_parts(
            buf_guard.as_mut_ptr() as *mut f64,
            buf_guard.len(),
            buf_guard.capacity(),
        )
    };

    Ok(EpmaBatchOutput {
        values,
        combos,
        rows,
        cols,
    })
}

#[cfg(any(feature = "python", feature = "wasm"))]
#[inline(always)]
pub fn epma_batch_inner_into(
    data: &[f64],
    sweep: &EpmaBatchRange,
    kern: Kernel,
    parallel: bool,
    out: &mut [f64],
) -> Result<Vec<EpmaParams>, EpmaError> {
    let buf_mu = unsafe {
        std::slice::from_raw_parts_mut(out.as_mut_ptr() as *mut MaybeUninit<f64>, out.len())
    };
    epma_batch_inner_into_uninit(data, sweep, kern, parallel, buf_mu)
}

#[inline(always)]
fn epma_batch_inner_into_uninit(
    data: &[f64],
    sweep: &EpmaBatchRange,
    kern: Kernel,
    parallel: bool,
    buf_mu: &mut [MaybeUninit<f64>],
) -> Result<Vec<EpmaParams>, EpmaError> {
    if data.is_empty() {
        return Err(EpmaError::EmptyInputData);
    }
    let combos = expand_grid(sweep);
    if combos.is_empty() {
        return Err(EpmaError::InvalidRange {
            start: 0,
            end: 0,
            step: 0,
        });
    }
    for c in &combos {
        let p = c.period.unwrap();
        let o = c.offset.unwrap();
        if p < 2 {
            return Err(EpmaError::InvalidPeriod {
                period: p,
                data_len: data.len(),
            });
        }
        if o >= p {
            return Err(EpmaError::InvalidOffset { offset: o });
        }
    }
    let first = data
        .iter()
        .position(|x| !x.is_nan())
        .ok_or(EpmaError::AllValuesNaN)?;
    let max_p = combos.iter().map(|c| c.period.unwrap()).max().unwrap();
    let max_off = combos.iter().map(|c| c.offset.unwrap()).max().unwrap();
    let needed = max_p + max_off + 1;
    if data.len() - first < needed {
        return Err(EpmaError::NotEnoughValidData {
            needed,
            valid: data.len() - first,
        });
    }
    let cols = data.len();

    let warm: Vec<usize> = combos
        .iter()
        .map(|c| first + c.period.unwrap() + c.offset.unwrap() + 1)
        .collect();
    init_matrix_prefixes(buf_mu, cols, &warm);

    let use_prefix = {
        #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
        {
            matches!(kern, Kernel::Avx2 | Kernel::Avx512)
        }
        #[cfg(not(all(feature = "nightly-avx", target_arch = "x86_64")))]
        {
            false
        }
    };

    if use_prefix {
        let mut p: Vec<f64> = Vec::with_capacity(cols);
        let mut q: Vec<f64> = Vec::with_capacity(cols);
        let mut ps = 0.0f64;
        let mut qs = 0.0f64;
        for (idx, &x) in data.iter().enumerate() {
            ps += x;
            qs = (idx as f64).mul_add(x, qs);
            p.push(ps);
            q.push(qs);
        }

        let do_row_prefix = |row: usize, dst_mu: &mut [MaybeUninit<f64>]| {
            let period = combos[row].period.unwrap();
            let offset = combos[row].offset.unwrap();
            let p1 = period - 1;
            let c0 = 2.0 - (offset as f64);
            let p1f = p1 as f64;
            let wsum = p1f.mul_add(c0, 0.5 * (p1f - 1.0) * p1f);
            let inv = 1.0 / wsum;
            let warmup = warm[row];

            let dst = unsafe {
                core::slice::from_raw_parts_mut(dst_mu.as_mut_ptr() as *mut f64, dst_mu.len())
            };

            for j in warmup..cols {
                let a = j + 1 - p1;
                let b = j;

                let sumx = if a > 0 { p[b] - p[a - 1] } else { p[b] };

                let sum_abs = if a > 0 { q[b] - q[a - 1] } else { q[b] };

                let sum_ix = sum_abs - (a as f64) * sumx;
                let y = (c0 * sumx + sum_ix) * inv;

                unsafe {
                    *dst.get_unchecked_mut(j) = y;
                }
            }
        };

        if parallel {
            #[cfg(not(target_arch = "wasm32"))]
            {
                buf_mu
                    .par_chunks_mut(cols)
                    .enumerate()
                    .for_each(|(row, slice)| do_row_prefix(row, slice));
            }
            #[cfg(target_arch = "wasm32")]
            {
                for (row, slice) in buf_mu.chunks_mut(cols).enumerate() {
                    do_row_prefix(row, slice);
                }
            }
        } else {
            for (row, slice) in buf_mu.chunks_mut(cols).enumerate() {
                do_row_prefix(row, slice);
            }
        }
    } else {
        let do_row = |row: usize, dst_mu: &mut [MaybeUninit<f64>]| unsafe {
            let period = combos[row].period.unwrap();
            let offset = combos[row].offset.unwrap();

            let dst =
                core::slice::from_raw_parts_mut(dst_mu.as_mut_ptr() as *mut f64, dst_mu.len());

            match kern {
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                Kernel::Avx512 => epma_row_avx512(data, first, period, offset, dst),
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                Kernel::Avx2 => epma_row_avx2(data, first, period, offset, dst),
                _ => epma_row_scalar(data, first, period, offset, dst),
            }
        };

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

    Ok(combos)
}

#[inline(always)]
unsafe fn epma_row_scalar(
    data: &[f64],
    first: usize,
    period: usize,
    offset: usize,
    out: &mut [f64],
) {
    epma_scalar(data, period, offset, first, out);
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[target_feature(enable = "avx2,fma")]
unsafe fn epma_row_avx2(data: &[f64], first: usize, period: usize, offset: usize, out: &mut [f64]) {
    epma_avx2(data, period, offset, first, out);
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[target_feature(enable = "avx512f,avx512dq,fma")]
unsafe fn epma_row_avx512(
    data: &[f64],
    first: usize,
    period: usize,
    offset: usize,
    out: &mut [f64],
) {
    epma_avx512(data, period, offset, first, out);
}

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

    #[test]
    fn test_epma_into_matches_api() -> Result<(), Box<dyn Error>> {
        let mut data = Vec::with_capacity(256);
        data.extend_from_slice(&[f64::NAN, f64::NAN, f64::NAN]);
        for i in 0..253u32 {
            let v = (i as f64).sin() * 10.0 + (i as f64) * 0.01;
            data.push(v);
        }

        let input = EpmaInput::from_slice(&data, EpmaParams::default());

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

        let mut out = vec![0.0; data.len()];
        #[cfg(not(all(target_arch = "wasm32", feature = "wasm")))]
        {
            epma_into(&input, &mut out)?;
        }

        fn eq_or_both_nan(a: f64, b: f64) -> bool {
            (a.is_nan() && b.is_nan()) || (a == b)
        }

        assert_eq!(baseline.len(), out.len());
        for i in 0..out.len() {
            assert!(
                eq_or_both_nan(baseline[i], out[i]),
                "Mismatch at {}: baseline={} out={}",
                i,
                baseline[i],
                out[i]
            );
        }
        Ok(())
    }

    fn check_epma_partial_params(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::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 = EpmaParams {
            period: None,
            offset: None,
        };
        let input = EpmaInput::from_candles(&candles, "close", default_params);
        let output = epma_with_kernel(&input, kernel)?;
        assert_eq!(output.values.len(), candles.close.len());
        Ok(())
    }
    fn check_epma_accuracy(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::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 = EpmaParams::default();
        let input = EpmaInput::from_candles(&candles, "close", default_params);
        let result = epma_with_kernel(&input, kernel)?;
        let expected_last_five = [59174.48, 59201.04, 59167.60, 59200.32, 59117.04];
        let start_index = result.values.len().saturating_sub(5);
        let result_last_five = &result.values[start_index..];
        for (i, &value) in result_last_five.iter().enumerate() {
            assert!(
                (value - expected_last_five[i]).abs() < 1e-1,
                "[{}] EPMA {:?} mismatch at idx {}: got {}, expected {}",
                test_name,
                kernel,
                i,
                value,
                expected_last_five[i]
            );
        }
        Ok(())
    }
    fn check_epma_default_candles(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::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 = EpmaInput::with_default_candles(&candles);
        match input.data {
            EpmaData::Candles { source, .. } => assert_eq!(source, "close"),
            _ => panic!("Expected EpmaData::Candles"),
        }
        let output = epma_with_kernel(&input, kernel)?;
        assert_eq!(output.values.len(), candles.close.len());
        Ok(())
    }
    fn check_epma_zero_period(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::Error>> {
        skip_if_unsupported!(kernel, test_name);
        let input_data = [10.0, 20.0, 30.0];
        let params = EpmaParams {
            period: Some(0),
            offset: None,
        };
        let input = EpmaInput::from_slice(&input_data, params);
        let res = epma_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] EPMA should fail with zero period",
            test_name
        );
        Ok(())
    }
    fn check_epma_period_exceeds_length(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::Error>> {
        skip_if_unsupported!(kernel, test_name);
        let data_small = [10.0, 20.0, 30.0];
        let params = EpmaParams {
            period: Some(10),
            offset: None,
        };
        let input = EpmaInput::from_slice(&data_small, params);
        let res = epma_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] EPMA should fail with period exceeding length",
            test_name
        );
        Ok(())
    }
    fn check_epma_very_small_dataset(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::Error>> {
        skip_if_unsupported!(kernel, test_name);
        let single_point = [42.0];
        let params = EpmaParams {
            period: Some(9),
            offset: None,
        };
        let input = EpmaInput::from_slice(&single_point, params);
        let res = epma_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] EPMA should fail with insufficient data",
            test_name
        );
        Ok(())
    }
    fn check_epma_empty_input(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::Error>> {
        skip_if_unsupported!(kernel, test_name);
        let empty: [f64; 0] = [];
        let input = EpmaInput::from_slice(&empty, EpmaParams::default());
        let res = epma_with_kernel(&input, kernel);
        assert!(
            matches!(res, Err(EpmaError::EmptyInputData)),
            "[{}] EPMA should fail with empty input",
            test_name
        );
        Ok(())
    }
    fn check_epma_invalid_offset(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::Error>> {
        skip_if_unsupported!(kernel, test_name);
        let data = [1.0, 2.0, 3.0, 4.0];
        let params = EpmaParams {
            period: Some(3),
            offset: Some(3),
        };
        let input = EpmaInput::from_slice(&data, params);
        let res = epma_with_kernel(&input, kernel);
        assert!(
            matches!(res, Err(EpmaError::InvalidOffset { .. })),
            "[{}] EPMA should fail with invalid offset",
            test_name
        );
        Ok(())
    }
    fn check_epma_property(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::Error>> {
        use proptest::prelude::*;
        skip_if_unsupported!(kernel, test_name);

        let strat = (2usize..=50).prop_flat_map(|period| {
            (
                prop::collection::vec(
                    (-1e6f64..1e6f64).prop_filter("finite", |x| x.is_finite()),
                    (period * 2 + 10)..500,
                ),
                Just(period),
                0usize..period,
            )
        });

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


				let EpmaOutput { values: out } = epma_with_kernel(&input, kernel).unwrap();


				let EpmaOutput { values: ref_out } = epma_with_kernel(&input, Kernel::Scalar).unwrap();


				let first_valid = data.iter().position(|x| !x.is_nan()).unwrap_or(0);


				let warmup = first_valid + period + offset + 1;


				for i in 0..warmup.min(out.len()) {
					prop_assert!(
						out[i].is_nan(),
						"[{}] Expected NaN during warmup at index {}, got {}",
						test_name,
						i,
						out[i]
					);
				}



				if warmup < out.len() && data[warmup].is_finite() {

					let p1 = period - 1;
					let mut weight_sum = 0.0;
					for i in 0..p1 {
						let w = (period as i32 - i as i32 - offset as i32) as f64;
						weight_sum += w;
					}


					if weight_sum.abs() > 1e-10 {
						prop_assert!(
							!out[warmup].is_nan(),
							"[{}] Expected valid value at warmup index {}, got NaN",
							test_name,
							warmup
						);
					}
				}



				let p1 = period - 1;
				let mut weight_sum = 0.0;
				for i in 0..p1 {
					let w = (period as i32 - i as i32 - offset as i32) as f64;
					weight_sum += w;
				}

				if weight_sum.abs() > 1e-10 {

					for i in warmup..data.len() {
						let y = out[i];
						prop_assert!(
							y.is_finite(),
							"[{}] EPMA output at index {} is not finite: {} (period={}, offset={}, weight_sum={})",
							test_name,
							i,
							y,
							period,
							offset,
							weight_sum
						);
					}
				} else {


					for i in warmup..data.len() {
						let both_nan = out[i].is_nan() && ref_out[i].is_nan();
						let both_inf = out[i].is_infinite() && ref_out[i].is_infinite();
						prop_assert!(
							both_nan || both_inf,
							"[{}] With weight_sum=0, expected consistent NaN or Inf at index {} but got: kernel={}, scalar={} (period={}, offset={})",
							test_name,
							i,
							out[i],
							ref_out[i],
							period,
							offset
						);
					}
				}




				if period == 2 && offset == 0 && warmup < data.len() {

					for i in warmup..data.len() {
						if data[i].is_finite() {

							prop_assert!(
								(out[i] - data[i]).abs() < 1e-9,
								"[{}] Period=2,offset=0 mismatch at {}: got {}, expected {}",
								test_name,
								i,
								out[i],
								data[i]
							);
						}
					}
				}



				if data.windows(2).all(|w| (w[0] - w[1]).abs() < 1e-12) && data.iter().any(|x| x.is_finite() && x.abs() > 1e-10) {
					let constant = *data.iter().find(|x| x.is_finite()).unwrap();

					let p1 = period - 1;
					let mut weight_sum = 0.0;
					for i in 0..p1 {
						let w = (period as i32 - i as i32 - offset as i32) as f64;
						weight_sum += w;
					}

					if weight_sum.abs() > 1e-10 {
						for i in warmup..data.len() {
							prop_assert!(
								(out[i] - constant).abs() < 1e-9,
								"[{}] Constant data mismatch at {}: got {}, expected {}",
								test_name,
								i,
								out[i],
								constant
							);
						}
					}
				}



				for i in (warmup.saturating_add(1))..data.len() {
					let y = out[i];
					let r = ref_out[i];

					if !y.is_finite() || !r.is_finite() {
						prop_assert!(
							y.to_bits() == r.to_bits(),
							"[{}] finite/NaN mismatch at idx {}: {} vs {}",
							test_name,
							i,
							y,
							r
						);
						continue;
					}

					let ulp_diff: u64 = y.to_bits().abs_diff(r.to_bits());

					let rel_error = if r.abs() > 1e-10 {
						(y - r).abs() / r.abs()
					} else {
						(y - r).abs()
					};


					prop_assert!(
						rel_error <= 1e-4 || (y - r).abs() <= 1e-9 || ulp_diff <= 100,
						"[{}] Kernel mismatch at idx {}: {} vs {} (ULP={}, rel_err={})",
						test_name,
						i,
						y,
						r,
						ulp_diff,
						rel_error
					);
				}



				if warmup + 5 < data.len() {

					let p1 = period - 1;
					let mut weights = Vec::with_capacity(p1);
					let mut weight_sum = 0.0;
					for i in 0..p1 {
						let w = (period as i32 - i as i32 - offset as i32) as f64;
						weights.push(w);
						weight_sum += w;
					}


					if weight_sum.abs() > 1e-10 {

						for idx in [warmup + 1, warmup + 2, data.len() - 1].iter().copied() {
							if idx > warmup && idx < data.len() {
								let start = idx + 1 - p1;
								let mut expected_sum = 0.0;
								for i in 0..p1 {
									expected_sum += data[start + i] * weights[p1 - 1 - i];
								}
								let expected = expected_sum / weight_sum;


								if out[idx].is_finite() && expected.is_finite() {

									let tolerance = if expected.abs() > 1000.0 {
										expected.abs() * 1e-12
									} else {
										1e-9
									};
									prop_assert!(
										(out[idx] - expected).abs() < tolerance,
										"[{}] EPMA formula mismatch at {}: got {}, expected {} (diff: {})",
										test_name,
										idx,
										out[idx],
										expected,
										(out[idx] - expected).abs()
									);
								} else {

									prop_assert!(
										out[idx].is_nan() == expected.is_nan() &&
										out[idx].is_infinite() == expected.is_infinite(),
										"[{}] EPMA formula NaN/Inf mismatch at {}: got {}, expected {}",
										test_name,
										idx,
										out[idx],
										expected
									);
								}
							}
						}
					}
				}




				if offset == period - 1 && warmup < data.len() && weight_sum.abs() > 1e-10 {

					for i in warmup..data.len() {
						prop_assert!(
							out[i].is_finite(),
							"[{}] Edge case offset={} produced non-finite at {}",
							test_name,
							offset,
							i
						);
					}
				}

				Ok(())
			})
			.unwrap();

        Ok(())
    }
    fn check_epma_invalid_params(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::Error>> {
        skip_if_unsupported!(kernel, test_name);

        let zero_weight_cases = vec![(4, 3), (5, 3), (6, 4), (8, 6)];

        for (period, offset) in zero_weight_cases {
            let p1 = period - 1;
            let mut weight_sum = 0.0;
            for i in 0..p1 {
                let w = (period as i32 - i as i32 - offset as i32) as f64;
                weight_sum += w;
            }

            let data = vec![1.0; period * 2];
            let params = EpmaParams {
                period: Some(period),
                offset: Some(offset),
            };
            let input = EpmaInput::from_slice(&data, params);

            if weight_sum.abs() < 1e-10 {
                let out = epma_with_kernel(&input, kernel)?;
                let scalar_out = epma_with_kernel(&input, Kernel::Scalar)?;

                let warmup = period + offset + 1;
                for i in warmup..data.len() {
                    let both_nan = out.values[i].is_nan() && scalar_out.values[i].is_nan();
                    let both_inf =
                        out.values[i].is_infinite() && scalar_out.values[i].is_infinite();
                    assert!(
						both_nan || both_inf,
						"[{}] Period={}, Offset={} (weight_sum=0) should produce consistent NaN or Inf, got kernel={}, scalar={}",
						test_name,
						period,
						offset,
						out.values[i],
						scalar_out.values[i]
					);
                }
            }
        }

        Ok(())
    }

    fn check_epma_reinput(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::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 = EpmaParams {
            period: Some(9),
            offset: None,
        };
        let first_input = EpmaInput::from_candles(&candles, "close", first_params);
        let first_result = epma_with_kernel(&first_input, kernel)?;
        let second_params = EpmaParams {
            period: Some(3),
            offset: None,
        };
        let second_input = EpmaInput::from_slice(&first_result.values, second_params);
        let second_result = epma_with_kernel(&second_input, kernel)?;
        assert_eq!(second_result.values.len(), first_result.values.len());
        Ok(())
    }
    fn check_epma_nan_handling(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::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 params = EpmaParams {
            period: Some(11),
            offset: Some(4),
        };
        let input = EpmaInput::from_candles(&candles, "close", params.clone());
        let res = epma_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_epma_streaming(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::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 = 11;
        let offset = 4;
        let input = EpmaInput::from_candles(
            &candles,
            "close",
            EpmaParams {
                period: Some(period),
                offset: Some(offset),
            },
        );
        let batch_output = epma_with_kernel(&input, kernel)?.values;
        let mut stream = EpmaStream::try_new(EpmaParams {
            period: Some(period),
            offset: Some(offset),
        })?;
        let mut stream_values = Vec::with_capacity(candles.close.len());
        for &price in &candles.close {
            match stream.update(price) {
                Some(val) => stream_values.push(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()
            .skip(period + offset + 1)
        {
            if b.is_nan() && s.is_nan() {
                continue;
            }
            let diff = (b - s).abs();
            assert!(
                diff < 1e-9,
                "[{}] EPMA streaming f64 mismatch at idx {}: batch={}, stream={}, diff={}",
                test_name,
                i,
                b,
                s,
                diff
            );
        }
        Ok(())
    }

    #[cfg(debug_assertions)]
    fn check_epma_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_cases = vec![
            EpmaParams::default(),
            EpmaParams {
                period: Some(2),
                offset: Some(0),
            },
            EpmaParams {
                period: Some(5),
                offset: Some(1),
            },
            EpmaParams {
                period: Some(10),
                offset: Some(3),
            },
            EpmaParams {
                period: Some(10),
                offset: Some(9),
            },
            EpmaParams {
                period: Some(20),
                offset: Some(5),
            },
            EpmaParams {
                period: Some(30),
                offset: Some(10),
            },
            EpmaParams {
                period: Some(15),
                offset: Some(14),
            },
        ];

        for params in test_cases {
            let input = EpmaInput::from_candles(&candles, "close", params.clone());
            let output = epma_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={:?}, offset={:?}",
                        test_name, val, bits, i, params.period, params.offset
                    );
                }

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

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

        Ok(())
    }

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

    macro_rules! generate_all_epma_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);
                    }
                    #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                    #[test]
                    fn [<$test_fn _avx512_f64>]() {
                        let _ = $test_fn(stringify!([<$test_fn _avx512_f64>]), Kernel::Avx512);
                    }
                )*

                #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
                $(
                    #[test]
                    fn [<$test_fn _simd128_f64>]() {
                        let _ = $test_fn(stringify!([<$test_fn _simd128_f64>]), Kernel::Scalar);
                    }
                )*
            }
        }
    }
    generate_all_epma_tests!(
        check_epma_partial_params,
        check_epma_accuracy,
        check_epma_default_candles,
        check_epma_zero_period,
        check_epma_period_exceeds_length,
        check_epma_very_small_dataset,
        check_epma_empty_input,
        check_epma_invalid_offset,
        check_epma_invalid_params,
        check_epma_reinput,
        check_epma_nan_handling,
        check_epma_streaming,
        check_epma_property,
        check_epma_no_poison
    );

    #[cfg(all(target_arch = "wasm32", target_feature = "simd128"))]
    #[test]
    fn test_epma_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,
        ];
        let period = 5;
        let offset = 2;

        let params = EpmaParams {
            period: Some(period),
            offset: Some(offset),
        };
        let input = EpmaInput::from_slice(&data, params);

        let mut scalar_out = vec![0.0; data.len()];
        epma_scalar(&data, period, offset, 0, &mut scalar_out);

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

        let warmup = period + offset + 1;
        for i in warmup..data.len() {
            assert!(
                (scalar_out[i] - simd128_output.values[i]).abs() < 1e-10,
                "SIMD128 mismatch at index {}: scalar={}, simd128={}",
                i,
                scalar_out[i],
                simd128_output.values[i]
            );
        }
    }

    fn check_batch_default_row(
        test: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::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 = EpmaBatchBuilder::new()
            .kernel(kernel)
            .apply_candles(&c, "close")?;
        let def = EpmaParams::default();
        let row = output.values_for(&def).expect("default row missing");
        assert_eq!(row.len(), c.close.len());
        let expected = [59174.48, 59201.04, 59167.60, 59200.32, 59117.04];
        let start = row.len() - 5;
        for (i, &v) in row[start..].iter().enumerate() {
            assert!(
                (v - expected[i]).abs() < 1e-1,
                "[{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 test_configs = vec![
            ((2, 5, 1), (0, 2, 1)),
            ((10, 20, 5), (0, 19, 3)),
            ((20, 30, 2), (5, 15, 5)),
            ((15, 25, 5), (10, 14, 2)),
            ((5, 10, 1), (0, 9, 1)),
        ];

        for (period_range, offset_range) in test_configs {
            let output = EpmaBatchBuilder::new()
                .kernel(kernel)
                .period_range(period_range.0, period_range.1, period_range.2)
                .offset_range(offset_range.0, offset_range.1, offset_range.2)
                .apply_candles(&c, "close")?;

            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 params = &output.combos[row];

                if bits == 0x11111111_11111111 {
                    panic!(
                        "[{}] Found alloc_with_nan_prefix poison value {} (0x{:016X}) at row {} col {} (params: period={:?}, offset={:?})",
                        test, val, bits, row, col, params.period, params.offset
                    );
                }

                if bits == 0x22222222_22222222 {
                    panic!(
                        "[{}] Found init_matrix_prefixes poison value {} (0x{:016X}) at row {} col {} (params: period={:?}, offset={:?})",
                        test, val, bits, row, col, params.period, params.offset
                    );
                }

                if bits == 0x33333333_33333333 {
                    panic!(
                        "[{}] Found make_uninit_matrix poison value {} (0x{:016X}) at row {} col {} (params: period={:?}, offset={:?})",
                        test, val, bits, row, col, params.period, params.offset
                    );
                }
            }
        }

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

    #[test]
    fn test_invalid_output_len_error() {
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let params = EpmaParams {
            period: Some(3),
            offset: Some(1),
        };
        let input = EpmaInput::from_slice(&data, params);
        let mut wrong_size_dst = vec![0.0; 3];

        let result = epma_into_slice(&mut wrong_size_dst, &input, Kernel::Scalar);
        assert!(result.is_err());

        match result {
            Err(EpmaError::OutputLengthMismatch { expected, got }) => {
                assert_eq!(expected, 5);
                assert_eq!(got, 3);
            }
            _ => panic!("Expected OutputLengthMismatch error"),
        }
    }

    #[test]
    fn test_invalid_kernel_error() {
        let data = vec![1.0, 2.0, 3.0, 4.0, 5.0];
        let sweep = EpmaBatchRange {
            period: (3, 5, 1),
            offset: (1, 2, 1),
        };

        let result = epma_batch_with_kernel(&data, &sweep, Kernel::Scalar);
        assert!(result.is_err());

        match result {
            Err(EpmaError::InvalidKernelForBatch(k)) => {
                assert_eq!(k, Kernel::Scalar);
            }
            _ => panic!("Expected InvalidKernelForBatch error"),
        }
    }
}

#[cfg(feature = "python")]
use crate::utilities::kernel_validation::validate_kernel;
#[cfg(all(feature = "python", feature = "cuda"))]
use numpy::PyReadonlyArray2;
#[cfg(feature = "python")]
use numpy::PyUntypedArrayMethods;
#[cfg(feature = "python")]
use numpy::{IntoPyArray, PyArray1, PyArrayMethods};
#[cfg(feature = "python")]
use pyo3::{exceptions::PyValueError, prelude::*, types::PyDict};

#[cfg(feature = "python")]
#[pyfunction(name = "epma")]
#[pyo3(signature = (data, period, offset, kernel=None))]
pub fn epma_py<'py>(
    py: Python<'py>,
    data: numpy::PyReadonlyArray1<'py, f64>,
    period: usize,
    offset: usize,
    kernel: Option<&str>,
) -> PyResult<Bound<'py, numpy::PyArray1<f64>>> {
    let slice_in = data.as_slice()?;
    let params = EpmaParams {
        period: Some(period),
        offset: Some(offset),
    };
    let input = EpmaInput::from_slice(slice_in, params);
    let kern = validate_kernel(kernel, false)?;

    let out_arr = unsafe { PyArray1::<f64>::new(py, [slice_in.len()], false) };
    let out_slice = unsafe { out_arr.as_slice_mut()? };

    py.allow_threads(|| epma_into_slice(out_slice, &input, kern))
        .map_err(|e| PyValueError::new_err(e.to_string()))?;

    Ok(out_arr)
}

#[cfg(feature = "python")]
#[pyfunction(name = "epma_batch")]
#[pyo3(signature = (data, period_range, offset_range, kernel=None))]
pub fn epma_batch_py<'py>(
    py: Python<'py>,
    data: numpy::PyReadonlyArray1<'py, f64>,
    period_range: (usize, usize, usize),
    offset_range: (usize, usize, usize),
    kernel: Option<&str>,
) -> PyResult<Bound<'py, pyo3::types::PyDict>> {
    let slice_in = data.as_slice()?;

    let sweep = EpmaBatchRange {
        period: period_range,
        offset: offset_range,
    };

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

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

    let kern = validate_kernel(kernel, true)?;

    let combos_done = py
        .allow_threads(|| {
            let actual = match kern {
                Kernel::Auto => detect_best_batch_kernel(),
                k => k,
            };
            let simd = match actual {
                Kernel::Avx512Batch => Kernel::Avx512,
                Kernel::Avx2Batch => Kernel::Avx2,
                Kernel::ScalarBatch => Kernel::Scalar,
                _ => unreachable!(),
            };
            epma_batch_inner_into(slice_in, &sweep, simd, true, out_slice)
        })
        .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_done
            .iter()
            .map(|p| p.period.unwrap() as u64)
            .collect::<Vec<_>>()
            .into_pyarray(py),
    )?;
    dict.set_item(
        "offsets",
        combos_done
            .iter()
            .map(|p| p.offset.unwrap() as u64)
            .collect::<Vec<_>>()
            .into_pyarray(py),
    )?;
    Ok(dict)
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyfunction(name = "epma_cuda_batch_dev")]
#[pyo3(signature = (data_f32, period_range, offset_range, device_id=0))]
pub fn epma_cuda_batch_dev_py(
    py: Python<'_>,
    data_f32: numpy::PyReadonlyArray1<'_, f32>,
    period_range: (usize, usize, usize),
    offset_range: (usize, usize, usize),
    device_id: usize,
) -> PyResult<EpmaDeviceArrayF32Py> {
    if !cuda_available() {
        return Err(PyValueError::new_err("CUDA not available"));
    }

    let sweep = EpmaBatchRange {
        period: period_range,
        offset: offset_range,
    };
    let slice_in = data_f32.as_slice()?;

    let (inner, ctx_guard, dev_id) = py.allow_threads(|| {
        let cuda = CudaEpma::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        let ctx = cuda.context_arc();
        let arr = cuda
            .epma_batch_dev(slice_in, &sweep)
            .map_err(|e| PyValueError::new_err(e.to_string()))?;
        Ok::<_, PyErr>((arr, ctx, device_id as u32))
    })?;

    Ok(EpmaDeviceArrayF32Py {
        inner,
        _ctx_guard: ctx_guard,
        device_id: dev_id as u32,
    })
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyfunction(name = "epma_cuda_many_series_one_param_dev")]
#[pyo3(signature = (data_tm_f32, period, offset, device_id=0))]
pub fn epma_cuda_many_series_one_param_dev_py(
    py: Python<'_>,
    data_tm_f32: PyReadonlyArray2<'_, f32>,
    period: usize,
    offset: usize,
    device_id: usize,
) -> PyResult<EpmaDeviceArrayF32Py> {
    if !cuda_available() {
        return Err(PyValueError::new_err("CUDA not available"));
    }

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

    let (inner, ctx_guard, dev_id) = py.allow_threads(|| {
        let cuda = CudaEpma::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        let ctx = cuda.context_arc();
        let arr = cuda
            .epma_many_series_one_param_time_major_dev(flat_in, cols, rows, &params)
            .map_err(|e| PyValueError::new_err(e.to_string()))?;
        Ok::<_, PyErr>((arr, ctx, device_id as u32))
    })?;

    Ok(EpmaDeviceArrayF32Py {
        inner,
        _ctx_guard: ctx_guard,
        device_id: dev_id as u32,
    })
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyclass(module = "ta_indicators.cuda", unsendable)]
pub struct EpmaDeviceArrayF32Py {
    pub(crate) inner: DeviceArrayF32,
    pub(crate) _ctx_guard: Arc<Context>,
    pub(crate) device_id: u32,
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pymethods]
impl EpmaDeviceArrayF32Py {
    #[getter]
    fn __cuda_array_interface__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
        let d = PyDict::new(py);

        d.set_item("shape", (self.inner.rows, self.inner.cols))?;
        d.set_item("typestr", "<f4")?;
        d.set_item(
            "strides",
            (
                self.inner.cols * std::mem::size_of::<f32>(),
                std::mem::size_of::<f32>(),
            ),
        )?;
        d.set_item("data", (self.inner.device_ptr() as usize, false))?;

        d.set_item("version", 3)?;
        Ok(d)
    }

    fn __dlpack_device__(&self) -> (i32, i32) {
        (2, self.device_id as i32)
    }

    #[pyo3(signature = (stream=None, max_version=None, dl_device=None, copy=None))]
    fn __dlpack__<'py>(
        &mut self,
        py: Python<'py>,
        stream: Option<pyo3::PyObject>,
        max_version: Option<pyo3::PyObject>,
        dl_device: Option<pyo3::PyObject>,
        copy: Option<pyo3::PyObject>,
    ) -> PyResult<PyObject> {
        use crate::utilities::dlpack_cuda::export_f32_cuda_dlpack_2d;

        let (kdl, alloc_dev) = self.__dlpack_device__();
        if let Some(dev_obj) = dl_device.as_ref() {
            if let Ok((dev_ty, dev_id)) = dev_obj.extract::<(i32, i32)>(py) {
                if dev_ty != kdl || dev_id != alloc_dev {
                    let wants_copy = copy
                        .as_ref()
                        .and_then(|c| c.extract::<bool>(py).ok())
                        .unwrap_or(false);
                    if wants_copy {
                        return Err(PyValueError::new_err(
                            "device copy not implemented for __dlpack__",
                        ));
                    } else {
                        return Err(PyValueError::new_err("dl_device mismatch for __dlpack__"));
                    }
                }
            }
        }

        let _ = stream;

        let dummy = cust::memory::DeviceBuffer::from_slice(&[])
            .map_err(|e| PyValueError::new_err(e.to_string()))?;
        let inner = std::mem::replace(
            &mut self.inner,
            DeviceArrayF32 {
                buf: dummy,
                rows: 0,
                cols: 0,
            },
        );

        let rows = inner.rows;
        let cols = inner.cols;
        let buf = inner.buf;

        let max_version_bound = max_version.map(|obj| obj.into_bound(py));

        export_f32_cuda_dlpack_2d(py, buf, rows, cols, alloc_dev, max_version_bound)
    }
}

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

#[cfg(feature = "python")]
#[pymethods]
impl EpmaStreamPy {
    #[new]
    fn new(period: usize, offset: usize) -> PyResult<Self> {
        let params = EpmaParams {
            period: Some(period),
            offset: Some(offset),
        };
        let stream =
            EpmaStream::try_new(params).map_err(|e| PyValueError::new_err(e.to_string()))?;
        Ok(Self { stream })
    }
    fn update(&mut self, value: f64) -> Option<f64> {
        self.stream.update(value)
    }
}

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

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn epma_js(data: &[f64], period: usize, offset: usize) -> Result<Vec<f64>, JsValue> {
    let params = EpmaParams {
        period: Some(period),
        offset: Some(offset),
    };
    let input = EpmaInput::from_slice(data, params);
    let mut out = vec![0.0; data.len()];
    epma_into_slice(&mut out, &input, detect_best_kernel())
        .map_err(|e| JsValue::from_str(&e.to_string()))?;
    Ok(out)
}

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

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

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

    let sweep = EpmaBatchRange {
        period: cfg.period_range,
        offset: cfg.offset_range,
    };
    let out = epma_batch_inner(data, &sweep, detect_best_kernel(), false)
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

    serde_wasm_bindgen::to_value(&EpmaBatchJsOutput {
        values: out.values,
        combos: out.combos,
        rows: out.rows,
        cols: out.cols,
    })
    .map_err(|e| JsValue::from_str(&format!("Serialization error: {}", e)))
}

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

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn epma_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 epma_into(
    in_ptr: *const f64,
    out_ptr: *mut f64,
    len: usize,
    period: usize,
    offset: usize,
) -> Result<(), JsValue> {
    if in_ptr.is_null() || out_ptr.is_null() {
        return Err(JsValue::from_str("null pointer"));
    }
    unsafe {
        let data = std::slice::from_raw_parts(in_ptr, len);
        let params = EpmaParams {
            period: Some(period),
            offset: Some(offset),
        };
        let input = EpmaInput::from_slice(data, params);

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

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn epma_batch_into(
    in_ptr: *const f64,
    out_ptr: *mut f64,
    len: usize,
    p_start: usize,
    p_end: usize,
    p_step: usize,
    o_start: usize,
    o_end: usize,
    o_step: usize,
) -> Result<usize, JsValue> {
    if in_ptr.is_null() || out_ptr.is_null() {
        return Err(JsValue::from_str("null pointer"));
    }
    unsafe {
        let data = std::slice::from_raw_parts(in_ptr, len);
        let sweep = EpmaBatchRange {
            period: (p_start, p_end, p_step),
            offset: (o_start, o_end, o_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);

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