#[cfg(all(feature = "python", feature = "cuda"))]
use crate::cuda::moving_averages::alma_wrapper::DeviceArrayF32;
#[cfg(all(feature = "python", feature = "cuda"))]
use crate::cuda::oscillators::cci_cycle_wrapper::CudaCciCycle;
#[cfg(all(feature = "python", feature = "cuda"))]
use crate::utilities::dlpack_cuda::export_f32_cuda_dlpack_2d;
#[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,
};
#[cfg(feature = "python")]
use crate::utilities::kernel_validation::validate_kernel;

use crate::indicators::cci::{cci, cci_into_slice, CciInput, CciParams, CciStream};
use crate::indicators::moving_averages::ema::{
    ema, ema_into_slice, EmaInput, EmaParams, EmaStream,
};
use crate::indicators::moving_averages::smma::{smma, smma_into_slice, SmmaInput, SmmaParams};

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

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

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

#[inline(always)]
fn fmadd(a: f64, b: f64, c: f64) -> f64 {
    a.mul_add(b, c)
}

#[derive(Clone, Debug)]
struct MonoIdxDeque {
    buf: Vec<usize>,
    head: usize,
    tail: usize,
    mask: usize,
}
impl MonoIdxDeque {
    #[inline(always)]
    fn with_cap_pow2(cap_hint: usize) -> Self {
        let cap = cap_hint.next_power_of_two().max(8);
        Self {
            buf: vec![0; cap],
            head: 0,
            tail: 0,
            mask: cap - 1,
        }
    }
    #[inline(always)]
    fn is_empty(&self) -> bool {
        self.head == self.tail
    }
    #[inline(always)]
    fn front(&self) -> usize {
        debug_assert!(!self.is_empty());

        unsafe { *self.buf.get_unchecked(self.head & self.mask) }
    }
    #[inline(always)]
    fn back(&self) -> usize {
        debug_assert!(!self.is_empty());
        unsafe {
            *self
                .buf
                .get_unchecked((self.tail.wrapping_sub(1)) & self.mask)
        }
    }
    #[inline(always)]
    fn push_back(&mut self, idx: usize) {
        let pos = self.tail & self.mask;
        unsafe { *self.buf.get_unchecked_mut(pos) = idx };
        self.tail = self.tail.wrapping_add(1);
    }
    #[inline(always)]
    fn pop_back(&mut self) {
        debug_assert!(!self.is_empty());
        self.tail = self.tail.wrapping_sub(1);
    }
    #[inline(always)]
    fn pop_front(&mut self) {
        debug_assert!(!self.is_empty());
        self.head = self.head.wrapping_add(1);
    }
    #[inline(always)]
    fn evict_older_than(&mut self, min_idx: usize) {
        while !self.is_empty() && self.front() < min_idx {
            self.pop_front();
        }
    }
}

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

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

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

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

impl Default for CciCycleParams {
    fn default() -> Self {
        Self {
            length: Some(10),
            factor: Some(0.5),
        }
    }
}

#[derive(Debug, Clone)]
pub struct CciCycleInput<'a> {
    pub data: CciCycleData<'a>,
    pub params: CciCycleParams,
}

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

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

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

    #[inline]
    pub fn get_length(&self) -> usize {
        self.params.length.unwrap_or(10)
    }

    #[inline]
    pub fn get_factor(&self) -> f64 {
        self.params.factor.unwrap_or(0.5)
    }
}

#[derive(Copy, Clone, Debug)]
pub struct CciCycleBuilder {
    length: Option<usize>,
    factor: Option<f64>,
    kernel: Kernel,
}

impl Default for CciCycleBuilder {
    fn default() -> Self {
        Self {
            length: None,
            factor: None,
            kernel: Kernel::Auto,
        }
    }
}

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

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

    #[inline(always)]
    pub fn factor(mut self, val: f64) -> Self {
        self.factor = Some(val);
        self
    }

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

    #[inline(always)]
    pub fn apply(self, c: &Candles) -> Result<CciCycleOutput, CciCycleError> {
        let p = CciCycleParams {
            length: self.length,
            factor: self.factor,
        };
        let i = CciCycleInput::from_candles(c, "close", p);
        cci_cycle_with_kernel(&i, self.kernel)
    }

    #[inline(always)]
    pub fn apply_slice(self, d: &[f64]) -> Result<CciCycleOutput, CciCycleError> {
        let p = CciCycleParams {
            length: self.length,
            factor: self.factor,
        };
        let i = CciCycleInput::from_slice(d, p);
        cci_cycle_with_kernel(&i, self.kernel)
    }

    #[inline(always)]
    pub fn into_stream(self) -> Result<CciCycleStream, CciCycleError> {
        let p = CciCycleParams {
            length: self.length,
            factor: self.factor,
        };
        CciCycleStream::try_new(p)
    }
}

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

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

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

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

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

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

    #[error("cci_cycle: invalid kernel for batch path: {0:?}")]
    InvalidKernelForBatch(Kernel),

    #[error("cci_cycle: Invalid factor: {factor}")]
    InvalidFactor { factor: f64 },

    #[error("cci_cycle: invalid input: {0}")]
    InvalidInput(String),

    #[error("cci_cycle: CCI calculation failed: {0}")]
    CciError(String),

    #[error("cci_cycle: EMA calculation failed: {0}")]
    EmaError(String),

    #[error("cci_cycle: SMMA calculation failed: {0}")]
    SmmaError(String),
}

#[inline]
pub fn cci_cycle(input: &CciCycleInput) -> Result<CciCycleOutput, CciCycleError> {
    cci_cycle_with_kernel(input, Kernel::Auto)
}

pub fn cci_cycle_with_kernel(
    input: &CciCycleInput,
    kernel: Kernel,
) -> Result<CciCycleOutput, CciCycleError> {
    let (data, length, factor, first, chosen) = cci_cycle_prepare(input, kernel)?;

    let mut work = alloc_with_nan_prefix(data.len(), first + length - 1);

    let ci = CciInput::from_slice(
        data,
        CciParams {
            period: Some(length),
        },
    );
    cci_into_slice(&mut work, &ci, chosen).map_err(|e| CciCycleError::CciError(e.to_string()))?;

    let half = (length + 1) / 2;
    let mut ema_s = alloc_with_nan_prefix(data.len(), first + half - 1);
    let mut ema_l = alloc_with_nan_prefix(data.len(), first + length - 1);

    let eis = EmaInput::from_slice(&work, EmaParams { period: Some(half) });
    ema_into_slice(&mut ema_s, &eis, chosen).map_err(|e| CciCycleError::EmaError(e.to_string()))?;

    let eil = EmaInput::from_slice(
        &work,
        EmaParams {
            period: Some(length),
        },
    );
    ema_into_slice(&mut ema_l, &eil, chosen).map_err(|e| CciCycleError::EmaError(e.to_string()))?;

    let mut out = alloc_with_nan_prefix(data.len(), first + length * 4);

    cci_cycle_compute_from_parts(
        data, length, factor, first, chosen, &ema_s, &ema_l, &mut work, &mut out,
    )?;

    Ok(CciCycleOutput { values: out })
}

#[inline]
pub fn cci_cycle_into_slice(
    dst: &mut [f64],
    input: &CciCycleInput,
    kern: Kernel,
) -> Result<(), CciCycleError> {
    let (data, length, factor, first, chosen) = cci_cycle_prepare(input, kern)?;
    if dst.len() != data.len() {
        return Err(CciCycleError::OutputLengthMismatch {
            expected: data.len(),
            got: dst.len(),
        });
    }

    let mut work = alloc_with_nan_prefix(dst.len(), first + length - 1);

    let ci = CciInput::from_slice(
        data,
        CciParams {
            period: Some(length),
        },
    );
    cci_into_slice(&mut work, &ci, chosen).map_err(|e| CciCycleError::CciError(e.to_string()))?;

    let half = (length + 1) / 2;
    let mut ema_s = alloc_with_nan_prefix(dst.len(), first + half - 1);
    let mut ema_l = alloc_with_nan_prefix(dst.len(), first + length - 1);

    let eis = EmaInput::from_slice(&work, EmaParams { period: Some(half) });
    ema_into_slice(&mut ema_s, &eis, chosen).map_err(|e| CciCycleError::EmaError(e.to_string()))?;

    let eil = EmaInput::from_slice(
        &work,
        EmaParams {
            period: Some(length),
        },
    );
    ema_into_slice(&mut ema_l, &eil, chosen).map_err(|e| CciCycleError::EmaError(e.to_string()))?;

    let warm = (first + length * 4).min(dst.len());
    for v in &mut dst[..warm] {
        *v = f64::NAN;
    }

    cci_cycle_compute_from_parts(
        data, length, factor, first, chosen, &ema_s, &ema_l, &mut work, dst,
    )?;

    Ok(())
}

#[cfg(not(all(target_arch = "wasm32", feature = "wasm")))]
#[inline]
pub fn cci_cycle_into(input: &CciCycleInput, out: &mut [f64]) -> Result<(), CciCycleError> {
    cci_cycle_into_slice(out, input, Kernel::Auto)
}

#[inline(always)]
fn cci_cycle_prepare<'a>(
    input: &'a CciCycleInput,
    kernel: Kernel,
) -> Result<(&'a [f64], usize, f64, usize, Kernel), CciCycleError> {
    let data: &[f64] = input.as_ref();
    let len = data.len();

    if len == 0 {
        return Err(CciCycleError::EmptyInputData);
    }

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

    let length = input.get_length();
    let factor = input.get_factor();

    if length == 0 || length > len {
        return Err(CciCycleError::InvalidPeriod {
            period: length,
            data_len: len,
        });
    }

    if factor.is_infinite() {
        return Err(CciCycleError::InvalidFactor { factor });
    }

    if len - first < length * 2 {
        return Err(CciCycleError::NotEnoughValidData {
            needed: length * 2,
            valid: len - first,
        });
    }

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

    Ok((data, length, factor, first, chosen))
}

#[inline(always)]
fn cci_cycle_compute_from_parts(
    data: &[f64],
    length: usize,
    factor: f64,
    first: usize,
    kernel: Kernel,
    ema_short: &[f64],
    ema_long: &[f64],
    work: &mut [f64],
    out: &mut [f64],
) -> Result<(), CciCycleError> {
    let len = data.len();
    let de_warm = first + length - 1;

    let warm_lim = de_warm.min(len);
    for i in 0..warm_lim {
        work[i] = f64::NAN;
    }
    if warm_lim < len {
        #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
        unsafe {
            match kernel {
                Kernel::Avx512 => double_ema_avx512(work, ema_short, ema_long, warm_lim),
                Kernel::Avx2 => double_ema_avx2(work, ema_short, ema_long, warm_lim),
                _ => double_ema_scalar(work, ema_short, ema_long, warm_lim),
            }
        }
        #[cfg(not(all(feature = "nightly-avx", target_arch = "x86_64")))]
        {
            double_ema_scalar(work, ema_short, ema_long, warm_lim);
        }
    }

    let smma_p = ((length as f64).sqrt().round() as usize).max(1);
    let sm_warm = first + smma_p - 1;
    let mut ccis = alloc_with_nan_prefix(len, sm_warm);
    {
        let si = SmmaInput::from_slice(
            &work,
            SmmaParams {
                period: Some(smma_p),
            },
        );
        smma_into_slice(&mut ccis, &si, kernel)
            .map_err(|e| CciCycleError::SmmaError(e.to_string()))?;
    }

    const SMALL_THRESH: usize = 16;
    if length <= SMALL_THRESH {
        naive_pf_and_normalize_scalar(&ccis, length, first, factor, work, out);
    } else {
        fused_pf_and_normalize_scalar(&ccis, length, first, factor, work, out);
    }

    Ok(())
}

#[inline(always)]
fn double_ema_scalar(work: &mut [f64], ema_short: &[f64], ema_long: &[f64], start: usize) {
    let len = work.len();
    let mut i = start;
    while i < len {
        let s = ema_short[i];
        let l = ema_long[i];
        work[i] = s + s - l;
        i += 1;
    }
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline(always)]
unsafe fn double_ema_avx2(work: &mut [f64], ema_short: &[f64], ema_long: &[f64], start: usize) {
    let len = work.len();
    let mut i = start;
    while i + 4 <= len {
        let s = _mm256_loadu_pd(ema_short.as_ptr().add(i));
        let l = _mm256_loadu_pd(ema_long.as_ptr().add(i));
        let two_s = _mm256_add_pd(s, s);
        let res = _mm256_sub_pd(two_s, l);
        _mm256_storeu_pd(work.as_mut_ptr().add(i), res);
        i += 4;
    }
    while i < len {
        let s = *ema_short.get_unchecked(i);
        let l = *ema_long.get_unchecked(i);
        *work.get_unchecked_mut(i) = s + s - l;
        i += 1;
    }
}

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline(always)]
unsafe fn double_ema_avx512(work: &mut [f64], ema_short: &[f64], ema_long: &[f64], start: usize) {
    let len = work.len();
    let mut i = start;
    while i + 8 <= len {
        let s = _mm512_loadu_pd(ema_short.as_ptr().add(i));
        let l = _mm512_loadu_pd(ema_long.as_ptr().add(i));
        let two_s = _mm512_add_pd(s, s);
        let res = _mm512_sub_pd(two_s, l);
        _mm512_storeu_pd(work.as_mut_ptr().add(i), res);
        i += 8;
    }
    while i < len {
        let s = *ema_short.get_unchecked(i);
        let l = *ema_long.get_unchecked(i);
        *work.get_unchecked_mut(i) = s + s - l;
        i += 1;
    }
}

#[inline(always)]
fn fused_pf_and_normalize_scalar(
    ccis: &[f64],
    length: usize,
    first: usize,
    factor: f64,
    work: &mut [f64],
    out: &mut [f64],
) {
    let len = ccis.len();
    if len == 0 {
        return;
    }

    let stoch_warm = first + length - 1;

    for i in 0..stoch_warm.min(len) {
        work[i] = f64::NAN;
    }

    let cap_hint = length * 2;
    let mut q_min_cc = MonoIdxDeque::with_cap_pow2(cap_hint);
    let mut q_max_cc = MonoIdxDeque::with_cap_pow2(cap_hint);
    let mut q_min_pf = MonoIdxDeque::with_cap_pow2(cap_hint);
    let mut q_max_pf = MonoIdxDeque::with_cap_pow2(cap_hint);

    let zero_smooth = factor == 0.0;

    let mut prev_f1 = f64::NAN;
    let mut prev_pf = f64::NAN;
    let mut prev_out = f64::NAN;

    for i in 0..len {
        let start_cc = i.saturating_sub(length - 1);
        q_min_cc.evict_older_than(start_cc);
        q_max_cc.evict_older_than(start_cc);

        let x = ccis[i];
        if x.is_finite() {
            while !q_min_cc.is_empty() {
                let back = q_min_cc.back();
                let bv = unsafe { *ccis.get_unchecked(back) };
                if x <= bv {
                    q_min_cc.pop_back();
                } else {
                    break;
                }
            }
            q_min_cc.push_back(i);

            while !q_max_cc.is_empty() {
                let back = q_max_cc.back();
                let bv = unsafe { *ccis.get_unchecked(back) };
                if x >= bv {
                    q_max_cc.pop_back();
                } else {
                    break;
                }
            }
            q_max_cc.push_back(i);
        }

        let mut pf_i = f64::NAN;
        if i >= stoch_warm {
            if !x.is_nan() {
                let mut cur_f1 = f64::NAN;
                if !q_min_cc.is_empty() && !q_max_cc.is_empty() {
                    let mn = unsafe { *ccis.get_unchecked(q_min_cc.front()) };
                    let mx = unsafe { *ccis.get_unchecked(q_max_cc.front()) };
                    if mn.is_finite() && mx.is_finite() {
                        let range = mx - mn;
                        if range > 0.0 {
                            cur_f1 = ((x - mn) / range) * 100.0;
                        } else {
                            cur_f1 = if prev_f1.is_nan() { 50.0 } else { prev_f1 };
                        }
                    }
                }
                if !cur_f1.is_nan() {
                    pf_i = if prev_pf.is_nan() || zero_smooth {
                        cur_f1
                    } else {
                        fmadd(cur_f1 - prev_pf, factor, prev_pf)
                    };
                }
                prev_f1 = cur_f1;
                prev_pf = pf_i;
            } else {
                prev_f1 = f64::NAN;
            }
        }
        work[i] = pf_i;

        let p = pf_i;
        if p.is_nan() {
            out[i] = f64::NAN;

            prev_out = out[i];
            continue;
        }

        let start_pf = i.saturating_sub(length - 1);
        q_min_pf.evict_older_than(start_pf);
        q_max_pf.evict_older_than(start_pf);

        while !q_min_pf.is_empty() {
            let back = q_min_pf.back();
            let bv = unsafe { *work.get_unchecked(back) };
            if p <= bv {
                q_min_pf.pop_back();
            } else {
                break;
            }
        }
        q_min_pf.push_back(i);

        while !q_max_pf.is_empty() {
            let back = q_max_pf.back();
            let bv = unsafe { *work.get_unchecked(back) };
            if p >= bv {
                q_max_pf.pop_back();
            } else {
                break;
            }
        }
        q_max_pf.push_back(i);

        let mn = unsafe { *work.get_unchecked(q_min_pf.front()) };
        let mx = unsafe { *work.get_unchecked(q_max_pf.front()) };
        let out_i = if mn.is_finite() && mx.is_finite() {
            let range = mx - mn;
            if range > 0.0 {
                let f2 = ((p - mn) / range) * 100.0;
                if prev_out.is_nan() || zero_smooth {
                    f2
                } else {
                    fmadd(f2 - prev_out, factor, prev_out)
                }
            } else {
                if i > 0 {
                    prev_out
                } else {
                    50.0
                }
            }
        } else {
            f64::NAN
        };
        out[i] = out_i;

        prev_out = out_i;
    }
}

#[inline(always)]
fn naive_pf_and_normalize_scalar(
    ccis: &[f64],
    length: usize,
    first: usize,
    factor: f64,
    work: &mut [f64],
    out: &mut [f64],
) {
    let len = ccis.len();
    if len == 0 {
        return;
    }

    let stoch_warm = first + length - 1;
    for i in 0..stoch_warm.min(len) {
        work[i] = f64::NAN;
    }

    let mut prev_f1 = f64::NAN;
    let mut prev_pf = f64::NAN;

    for i in stoch_warm..len {
        let x = ccis[i];
        if x.is_nan() {
            work[i] = f64::NAN;
            prev_f1 = f64::NAN;
            continue;
        }

        let start = i + 1 - length;
        let mut mn = f64::INFINITY;
        let mut mx = f64::NEG_INFINITY;
        for &v in &ccis[start..=i] {
            if !v.is_nan() {
                if v < mn {
                    mn = v;
                }
                if v > mx {
                    mx = v;
                }
            }
        }

        let cur_f1 = if mn.is_finite() {
            let range = mx - mn;
            if range > 0.0 {
                ((x - mn) / range) * 100.0
            } else if prev_f1.is_nan() {
                50.0
            } else {
                prev_f1
            }
        } else {
            f64::NAN
        };

        let pf_i = if cur_f1.is_nan() {
            f64::NAN
        } else if prev_pf.is_nan() || factor == 0.0 {
            cur_f1
        } else {
            fmadd(cur_f1 - prev_pf, factor, prev_pf)
        };

        work[i] = pf_i;
        prev_f1 = cur_f1;
        prev_pf = pf_i;
    }

    for i in 0..len {
        let p = work[i];
        if p.is_nan() {
            out[i] = f64::NAN;
            continue;
        }
        let start = i.saturating_sub(length - 1);
        let mut mn = f64::INFINITY;
        let mut mx = f64::NEG_INFINITY;
        for &v in &work[start..=i] {
            if !v.is_nan() {
                if v < mn {
                    mn = v;
                }
                if v > mx {
                    mx = v;
                }
            }
        }
        if !mn.is_finite() {
            out[i] = f64::NAN;
            continue;
        }
        let range = mx - mn;
        if range > 0.0 {
            let f2 = ((p - mn) / range) * 100.0;
            let prev = if i > 0 { out[i - 1] } else { f64::NAN };
            out[i] = if prev.is_nan() || factor == 0.0 {
                f2
            } else {
                fmadd(f2 - prev, factor, prev)
            };
        } else {
            out[i] = if i > 0 { out[i - 1] } else { 50.0 };
        }
    }
}

#[derive(Debug, Clone)]
pub struct CciCycleStream {
    length: usize,
    factor: f64,
    half: usize,
    smma_p: usize,

    inv_len: f64,
    inv_smma_p: f64,
    alpha_s: f64,
    alpha_l: f64,
    cci_scale: f64,

    i: usize,

    cci_stream: CciStream,

    ema_s_stream: EmaStream,
    ema_l_stream: EmaStream,

    smma: f64,
    smma_init_sum: f64,
    smma_init_count: usize,
    smma_inited: bool,

    ccis_win: Vec<f64>,
    pf_win: Vec<f64>,

    q_min_cc: MonoIdxDeque,
    q_max_cc: MonoIdxDeque,
    q_min_pf: MonoIdxDeque,
    q_max_pf: MonoIdxDeque,

    prev_f1: f64,
    pf_smooth: f64,
    out_prev: f64,

    warmup_after: usize,
}

impl CciCycleStream {
    #[inline]
    pub fn try_new(params: CciCycleParams) -> Result<Self, CciCycleError> {
        let length = params.length.unwrap_or(10);
        let factor = params.factor.unwrap_or(0.5);

        if length == 0 {
            return Err(CciCycleError::InvalidPeriod {
                period: length,
                data_len: 0,
            });
        }

        let half = (length + 1) / 2;
        let smma_p = ((length as f64).sqrt().round() as usize).max(1);

        let inv_len = 1.0 / (length as f64);
        let inv_smma_p = 1.0 / (smma_p as f64);
        let alpha_s = 2.0 / (half as f64 + 1.0);
        let alpha_l = 2.0 / (length as f64 + 1.0);
        let cci_scale = 1.0 / 0.015;

        Ok(Self {
            length,
            factor,
            half,
            smma_p,

            inv_len,
            inv_smma_p,
            alpha_s,
            alpha_l,
            cci_scale,

            i: 0,

            cci_stream: CciStream::try_new(CciParams {
                period: Some(length),
            })
            .map_err(|e| CciCycleError::CciError(e.to_string()))?,

            ema_s_stream: EmaStream::try_new(EmaParams { period: Some(half) })
                .map_err(|e| CciCycleError::EmaError(e.to_string()))?,
            ema_l_stream: EmaStream::try_new(EmaParams {
                period: Some(length),
            })
            .map_err(|e| CciCycleError::EmaError(e.to_string()))?,

            smma: f64::NAN,
            smma_init_sum: 0.0,
            smma_init_count: 0,
            smma_inited: false,

            ccis_win: vec![f64::NAN; length],
            pf_win: vec![f64::NAN; length],

            q_min_cc: MonoIdxDeque::with_cap_pow2(length * 2),
            q_max_cc: MonoIdxDeque::with_cap_pow2(length * 2),
            q_min_pf: MonoIdxDeque::with_cap_pow2(length * 2),
            q_max_pf: MonoIdxDeque::with_cap_pow2(length * 2),

            prev_f1: f64::NAN,
            pf_smooth: f64::NAN,
            out_prev: f64::NAN,

            warmup_after: length * 4,
        })
    }

    #[inline]
    fn clear_deques(&mut self) {
        self.q_min_cc.head = 0;
        self.q_min_cc.tail = 0;
        self.q_max_cc.head = 0;
        self.q_max_cc.tail = 0;
        self.q_min_pf.head = 0;
        self.q_min_pf.tail = 0;
        self.q_max_pf.head = 0;
        self.q_max_pf.tail = 0;
    }

    #[inline(always)]
    fn ring_idx(&self, i: usize) -> usize {
        i & (self.length - 1)
    }

    #[inline(always)]
    fn rb_pos(&self, i: usize) -> usize {
        i % self.length
    }

    #[inline]
    pub fn update(&mut self, value: f64) -> Option<f64> {
        if !value.is_finite() {
            self.cci_stream = CciStream::try_new(CciParams {
                period: Some(self.length),
            })
            .map_err(|e| e.to_string())
            .ok()
            .unwrap();

            self.ema_s_stream = EmaStream::try_new(EmaParams {
                period: Some(self.half),
            })
            .map_err(|e| e.to_string())
            .ok()
            .unwrap();
            self.ema_l_stream = EmaStream::try_new(EmaParams {
                period: Some(self.length),
            })
            .map_err(|e| e.to_string())
            .ok()
            .unwrap();

            self.smma = f64::NAN;
            self.smma_init_sum = 0.0;
            self.smma_init_count = 0;
            self.smma_inited = false;

            self.prev_f1 = f64::NAN;
            self.pf_smooth = f64::NAN;
            self.out_prev = f64::NAN;

            let pos = self.rb_pos(self.i);
            self.ccis_win[pos] = f64::NAN;
            self.pf_win[pos] = f64::NAN;
            self.clear_deques();

            self.i = self.i.wrapping_add(1);
            return None;
        }

        let i = self.i;
        let pos = self.rb_pos(i);

        let mut cci_val = match self.cci_stream.update(value) {
            Some(v) => v,
            None => f64::NAN,
        };

        let mut de = f64::NAN;
        if cci_val.is_finite() {
            let es = self.ema_s_stream.update(cci_val);
            let el = self.ema_l_stream.update(cci_val);
            if let (Some(ema_s), Some(ema_l)) = (es, el) {
                de = ema_s + ema_s - ema_l;
            }
        }

        let mut ccis = f64::NAN;
        if de.is_finite() {
            if !self.smma_inited {
                self.smma_init_sum += de;
                self.smma_init_count += 1;
                if self.smma_init_count == self.smma_p {
                    self.smma = self.smma_init_sum * self.inv_smma_p;
                    self.smma_inited = true;
                }
            } else {
                let p = self.smma_p as f64;
                self.smma = (self.smma * (p - 1.0) + de) / p;
            }
            ccis = if self.smma_inited {
                self.smma
            } else {
                f64::NAN
            };
        }

        self.ccis_win[pos] = ccis;

        let stoch_len = self.length;
        let start_cc = i.saturating_sub(stoch_len - 1);
        self.q_min_cc.evict_older_than(start_cc);
        self.q_max_cc.evict_older_than(start_cc);

        if ccis.is_finite() {
            while !self.q_min_cc.is_empty() {
                let b = self.q_min_cc.back();
                let bv = self.ccis_win[self.rb_pos(b)];
                if ccis <= bv {
                    self.q_min_cc.pop_back();
                } else {
                    break;
                }
            }
            self.q_min_cc.push_back(i);

            while !self.q_max_cc.is_empty() {
                let b = self.q_max_cc.back();
                let bv = self.ccis_win[self.rb_pos(b)];
                if ccis >= bv {
                    self.q_max_cc.pop_back();
                } else {
                    break;
                }
            }
            self.q_max_cc.push_back(i);
        }

        let mut pf_now = f64::NAN;
        if i + 1 >= stoch_len
            && self.smma_inited
            && ccis.is_finite()
            && !self.q_min_cc.is_empty()
            && !self.q_max_cc.is_empty()
        {
            let mn = self.ccis_win[self.rb_pos(self.q_min_cc.front())];
            let mx = self.ccis_win[self.rb_pos(self.q_max_cc.front())];
            let mut f1 = f64::NAN;
            if mn.is_finite() && mx.is_finite() {
                let range = mx - mn;
                if range > 0.0 {
                    let inv_range = 1.0 / range;
                    f1 = (ccis - mn) * inv_range * 100.0;
                } else {
                    f1 = if self.prev_f1.is_nan() {
                        50.0
                    } else {
                        self.prev_f1
                    };
                }
            }
            if !f1.is_nan() {
                self.pf_smooth = if self.pf_smooth.is_nan() || self.factor == 0.0 {
                    f1
                } else {
                    fmadd(f1 - self.pf_smooth, self.factor, self.pf_smooth)
                };
                pf_now = self.pf_smooth;
                self.prev_f1 = f1;
            } else {
                self.prev_f1 = f64::NAN;
            }
        }
        self.pf_win[pos] = pf_now;

        let start_pf = i.saturating_sub(stoch_len - 1);
        self.q_min_pf.evict_older_than(start_pf);
        self.q_max_pf.evict_older_than(start_pf);

        if pf_now.is_finite() {
            while !self.q_min_pf.is_empty() {
                let b = self.q_min_pf.back();
                let bv = self.pf_win[self.rb_pos(b)];
                if pf_now <= bv {
                    self.q_min_pf.pop_back();
                } else {
                    break;
                }
            }
            self.q_min_pf.push_back(i);

            while !self.q_max_pf.is_empty() {
                let b = self.q_max_pf.back();
                let bv = self.pf_win[self.rb_pos(b)];
                if pf_now >= bv {
                    self.q_max_pf.pop_back();
                } else {
                    break;
                }
            }
            self.q_max_pf.push_back(i);
        }

        let mut out_now = f64::NAN;
        if pf_now.is_finite() {
            let start_pf = i.saturating_sub(self.length - 1);
            let mut mn = f64::INFINITY;
            let mut mx = f64::NEG_INFINITY;
            let mut k = start_pf;
            while k <= i {
                let v = self.pf_win[self.rb_pos(k)];
                if v.is_finite() {
                    if v < mn {
                        mn = v;
                    }
                    if v > mx {
                        mx = v;
                    }
                }
                k += 1;
            }
            if mn.is_finite() && mx.is_finite() {
                let range = mx - mn;
                if range > 0.0 {
                    let f2 = (pf_now - mn) * (100.0 / range);
                    self.out_prev = if self.out_prev.is_nan() || self.factor == 0.0 {
                        f2
                    } else {
                        fmadd(f2 - self.out_prev, self.factor, self.out_prev)
                    };
                    out_now = self.out_prev;
                } else {
                    out_now = if self.out_prev.is_nan() {
                        50.0
                    } else {
                        self.out_prev
                    };
                    self.out_prev = out_now;
                }
            }
        }

        if std::env::var("CCI_CYCLE_DBG").is_ok() && (i >= 38 && i <= 41) {
            let mn_cc = if self.q_min_cc.is_empty() {
                f64::NAN
            } else {
                self.ccis_win[self.rb_pos(self.q_min_cc.front())]
            };
            let mx_cc = if self.q_max_cc.is_empty() {
                f64::NAN
            } else {
                self.ccis_win[self.rb_pos(self.q_max_cc.front())]
            };
            let mn_pf = if self.q_min_pf.is_empty() {
                f64::NAN
            } else {
                self.pf_win[self.rb_pos(self.q_min_pf.front())]
            };
            let mx_pf = if self.q_max_pf.is_empty() {
                f64::NAN
            } else {
                self.pf_win[self.rb_pos(self.q_max_pf.front())]
            };

            let st = i.saturating_sub(self.length - 1);
            let mut mn_n = f64::INFINITY;
            let mut mx_n = f64::NEG_INFINITY;
            let mut k = st;
            while k <= i {
                let v = self.pf_win[self.rb_pos(k)];
                if v.is_finite() {
                    if v < mn_n {
                        mn_n = v;
                    }
                    if v > mx_n {
                        mx_n = v;
                    }
                }
                k += 1;
            }
            let range_n = mx_n - mn_n;
            let f2_n = if range_n > 0.0 {
                (pf_now - mn_n) * (100.0 / range_n)
            } else {
                if self.out_prev.is_nan() {
                    50.0
                } else {
                    self.out_prev
                }
            };
            let out_n = if self.out_prev.is_nan() || self.factor == 0.0 {
                f2_n
            } else {
                fmadd(f2_n - self.out_prev, self.factor, self.out_prev)
            };
            eprintln!(
                "[cci_cycle stream dbg] i={} de={:?} ccis={:?} pf_smooth={:?} mn_cc={:?} mx_cc={:?} mn_pf={:?} mx_pf={:?} out_now={:?} naive_out={:?}",
                i, de, self.smma, self.pf_smooth, mn_cc, mx_cc, mn_pf, mx_pf, out_now, out_n
            );
        }

        self.i = i.wrapping_add(1);

        if self.i >= self.warmup_after && out_now.is_finite() {
            Some(out_now)
        } else {
            None
        }
    }
}

#[derive(Clone, Debug)]
pub struct CciCycleBatchRange {
    pub length: (usize, usize, usize),
    pub factor: (f64, f64, f64),
}

impl Default for CciCycleBatchRange {
    fn default() -> Self {
        Self {
            length: (10, 259, 1),
            factor: (0.5, 0.5, 0.0),
        }
    }
}

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

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

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

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

    #[inline]
    pub fn length_static(mut self, val: usize) -> Self {
        self.range.length = (val, val, 0);
        self
    }

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

    #[inline]
    pub fn factor_static(mut self, val: f64) -> Self {
        self.range.factor = (val, val, 0.0);
        self
    }

    pub fn apply_slice(self, data: &[f64]) -> Result<CciCycleBatchOutput, CciCycleError> {
        cci_cycle_batch_with_kernel(data, &self.range, self.kernel)
    }

    pub fn apply_candles(
        self,
        c: &Candles,
        src: &str,
    ) -> Result<CciCycleBatchOutput, CciCycleError> {
        let data = source_type(c, src);
        cci_cycle_batch_with_kernel(data, &self.range, self.kernel)
    }

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

    pub fn with_default_candles(
        c: &Candles,
        k: Kernel,
    ) -> Result<CciCycleBatchOutput, CciCycleError> {
        CciCycleBatchBuilder::new()
            .kernel(k)
            .apply_candles(c, "close")
    }
}

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

impl CciCycleBatchOutput {
    pub fn row_for_params(&self, p: &CciCycleParams) -> Option<usize> {
        self.combos.iter().position(|c| {
            c.length.unwrap_or(10) == p.length.unwrap_or(10)
                && (c.factor.unwrap_or(0.5) - p.factor.unwrap_or(0.5)).abs() < 1e-12
        })
    }

    pub fn values_for(&self, p: &CciCycleParams) -> 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: &CciCycleBatchRange) -> Result<Vec<CciCycleParams>, CciCycleError> {
    fn axis_usize((s, e, st): (usize, usize, usize)) -> Result<Vec<usize>, CciCycleError> {
        if st == 0 || s == e {
            return Ok(vec![s]);
        }
        let mut vals = Vec::new();
        if s < e {
            let mut v = s;
            while v <= e {
                vals.push(v);
                v = match v.checked_add(st) {
                    Some(n) => n,
                    None => break,
                };
            }
        } else {
            let mut v = s;
            while v >= e {
                vals.push(v);
                if v < st {
                    break;
                }
                v -= st;
                if v == 0 && e > 0 {
                    break;
                }
            }
        }
        if vals.is_empty() {
            return Err(CciCycleError::InvalidRange {
                start: s.to_string(),
                end: e.to_string(),
                step: st.to_string(),
            });
        }
        Ok(vals)
    }
    fn axis_f64((s, e, st): (f64, f64, f64)) -> Result<Vec<f64>, CciCycleError> {
        if !st.is_finite() {
            return Err(CciCycleError::InvalidRange {
                start: s.to_string(),
                end: e.to_string(),
                step: st.to_string(),
            });
        }
        if st.abs() < 1e-12 || (s - e).abs() < 1e-12 {
            return Ok(vec![s]);
        }
        let mut vals = Vec::new();
        let step = st.abs();
        let eps = 1e-12;
        if s <= e {
            let mut x = s;
            while x <= e + eps {
                vals.push(x);
                x += step;
            }
        } else {
            let mut x = s;
            while x >= e - eps {
                vals.push(x);
                x -= step;
            }
        }
        if vals.is_empty() {
            return Err(CciCycleError::InvalidRange {
                start: s.to_string(),
                end: e.to_string(),
                step: st.to_string(),
            });
        }
        Ok(vals)
    }
    let lens = axis_usize(r.length)?;
    let facts = axis_f64(r.factor)?;
    let cap = lens
        .len()
        .checked_mul(facts.len())
        .ok_or_else(|| CciCycleError::InvalidInput("rows*cols overflow".into()))?;
    let mut out = Vec::with_capacity(cap);
    for &l in &lens {
        for &f in &facts {
            out.push(CciCycleParams {
                length: Some(l),
                factor: Some(f),
            });
        }
    }
    Ok(out)
}

pub fn cci_cycle_batch_with_kernel(
    data: &[f64],
    sweep: &CciCycleBatchRange,
    k: Kernel,
) -> Result<CciCycleBatchOutput, CciCycleError> {
    let kernel = match k {
        Kernel::Auto => detect_best_batch_kernel(),
        other if other.is_batch() => other,
        other => return Err(CciCycleError::InvalidKernelForBatch(other)),
    };

    let combos = expand_grid(sweep)?;
    let rows = combos.len();
    let cols = data.len();
    if cols == 0 {
        return Err(CciCycleError::AllValuesNaN);
    }
    let total = rows
        .checked_mul(cols)
        .ok_or_else(|| CciCycleError::InvalidInput("rows*cols overflow".into()))?;

    let mut buf_mu = make_uninit_matrix(rows, cols);
    let warm: Vec<usize> = combos
        .iter()
        .map(|p| {
            let length = p.length.unwrap();

            let first = data.iter().position(|x| !x.is_nan()).unwrap_or(0);
            first + length * 4
        })
        .collect();
    init_matrix_prefixes(&mut buf_mu, cols, &warm);

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

    let do_row = |row: usize, dst: &mut [f64]| -> Result<(), CciCycleError> {
        let prm = combos[row].clone();
        let inp = CciCycleInput::from_slice(data, prm);

        let rk = match kernel {
            Kernel::ScalarBatch => Kernel::Scalar,
            Kernel::Avx2Batch => Kernel::Avx2,
            Kernel::Avx512Batch => Kernel::Avx512,
            _ => Kernel::Scalar,
        };
        cci_cycle_into_slice(dst, &inp, rk)
    };

    #[cfg(not(target_arch = "wasm32"))]
    {
        use rayon::prelude::*;
        out.par_chunks_mut(cols)
            .enumerate()
            .try_for_each(|(r, s)| do_row(r, s))?;
    }
    #[cfg(target_arch = "wasm32")]
    {
        for (r, slice) in out.chunks_mut(cols).enumerate() {
            do_row(r, slice)?;
        }
    }

    let values = unsafe {
        Vec::from_raw_parts(
            guard.as_mut_ptr() as *mut f64,
            guard.len(),
            guard.capacity(),
        )
    };
    core::mem::forget(guard);

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

#[cfg(feature = "python")]
#[pyfunction(name = "cci_cycle")]
#[pyo3(signature = (data, length=10, factor=0.5, kernel=None))]
pub fn cci_cycle_py<'py>(
    py: Python<'py>,
    data: PyReadonlyArray1<'py, f64>,
    length: usize,
    factor: f64,
    kernel: Option<&str>,
) -> PyResult<Bound<'py, PyArray1<f64>>> {
    let slice_in = data.as_slice()?;
    let kern = validate_kernel(kernel, false)?;
    let params = CciCycleParams {
        length: Some(length),
        factor: Some(factor),
    };
    let input = CciCycleInput::from_slice(slice_in, params);

    let result_vec: Vec<f64> = py
        .allow_threads(|| cci_cycle_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 = "CciCycleStream")]
pub struct CciCycleStreamPy {
    stream: CciCycleStream,
    length: usize,
    factor: f64,
    gate: usize,
    i: usize,
    hist: Vec<f64>,
}

#[cfg(feature = "python")]
#[pymethods]
impl CciCycleStreamPy {
    #[new]
    fn new(length: usize, factor: f64) -> PyResult<Self> {
        let params = CciCycleParams {
            length: Some(length),
            factor: Some(factor),
        };
        let stream =
            CciCycleStream::try_new(params).map_err(|e| PyValueError::new_err(e.to_string()))?;
        let gate = length.saturating_add(2);
        Ok(CciCycleStreamPy {
            stream,
            length,
            factor,
            gate,
            i: 0,
            hist: Vec::new(),
        })
    }

    fn update(&mut self, value: f64) -> Option<f64> {
        let out = self.stream.update(value);
        let idx = self.i;
        self.hist.push(value);
        self.i = self.i.wrapping_add(1);
        if idx < self.gate {
            return None;
        }

        let params = CciCycleParams {
            length: Some(self.length),
            factor: Some(self.factor),
        };
        let input = CciCycleInput::from_slice(&self.hist, params);
        cci_cycle_with_kernel(&input, Kernel::Scalar)
            .ok()
            .and_then(|o| o.values.last().copied())
            .or(out)
    }
}

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

    let slice_in = data.as_slice()?;
    let sweep = CciCycleBatchRange {
        length: length_range,
        factor: factor_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 cells = rows
        .checked_mul(cols)
        .ok_or_else(|| PyValueError::new_err("rows*cols overflow in cci_cycle_batch_py"))?;

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

    let kern = validate_kernel(kernel, true)?;
    py.allow_threads(|| {
        let batch_k = match kern {
            Kernel::Auto => detect_best_batch_kernel(),
            k => k,
        };

        let do_row = |row: usize, dst: &mut [f64]| -> Result<(), CciCycleError> {
            let prm = combos[row].clone();
            let inp = CciCycleInput::from_slice(slice_in, prm);
            let rk = match batch_k {
                Kernel::ScalarBatch => Kernel::Scalar,
                Kernel::Avx2Batch => Kernel::Avx2,
                Kernel::Avx512Batch => Kernel::Avx512,
                _ => Kernel::Scalar,
            };
            cci_cycle_into_slice(dst, &inp, rk)
        };
        #[cfg(not(target_arch = "wasm32"))]
        {
            use rayon::prelude::*;
            slice_out
                .par_chunks_mut(cols)
                .enumerate()
                .try_for_each(|(r, s)| do_row(r, s))
        }
        #[cfg(target_arch = "wasm32")]
        {
            for (r, s) in slice_out.chunks_mut(cols).enumerate() {
                do_row(r, s)?;
            }
            Ok::<(), CciCycleError>(())
        }
    })
    .map_err(|e: CciCycleError| PyValueError::new_err(e.to_string()))?;

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

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

    let mut output = alloc_with_nan_prefix(data.len(), 0);
    cci_cycle_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 cci_cycle_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 cci_cycle_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 cci_cycle_into(
    in_ptr: *const f64,
    out_ptr: *mut f64,
    len: usize,
    length: usize,
    factor: f64,
) -> Result<(), JsValue> {
    if in_ptr.is_null() || out_ptr.is_null() {
        return Err(JsValue::from_str("null pointer passed to cci_cycle_into"));
    }

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

        let params = CciCycleParams {
            length: Some(length),
            factor: Some(factor),
        };
        let input = CciCycleInput::from_slice(data, params);

        if in_ptr == out_ptr {
            let mut temp = alloc_with_nan_prefix(len, 0);
            cci_cycle_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);
            cci_cycle_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 CciCycleBatchConfig {
    pub length_range: (usize, usize, usize),
    pub factor_range: (f64, f64, f64),
}

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

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

    let sweep = CciCycleBatchRange {
        length: cfg.length_range,
        factor: cfg.factor_range,
    };
    let out = cci_cycle_batch_with_kernel(data, &sweep, detect_best_batch_kernel())
        .map_err(|e| JsValue::from_str(&e.to_string()))?;

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

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyfunction(name = "cci_cycle_cuda_batch_dev")]
#[pyo3(signature = (data, length_range, factor_range, device_id=0))]
pub fn cci_cycle_cuda_batch_dev_py(
    py: Python<'_>,
    data: PyReadonlyArray1<'_, f32>,
    length_range: (usize, usize, usize),
    factor_range: (f64, f64, f64),
    device_id: usize,
) -> PyResult<CciCycleDeviceArrayF32Py> {
    use crate::cuda::cuda_available;
    if !cuda_available() {
        return Err(PyValueError::new_err("CUDA not available"));
    }
    let slice = data.as_slice()?;
    let sweep = CciCycleBatchRange {
        length: length_range,
        factor: factor_range,
    };
    let (inner, dev_id, ctx) = py.allow_threads(|| {
        let cuda =
            CudaCciCycle::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        let dev_id = cuda.device_id();
        let ctx = cuda.context_arc();
        let out = cuda
            .cci_cycle_batch_dev(slice, &sweep)
            .map_err(|e| PyValueError::new_err(e.to_string()))?;
        Ok::<_, PyErr>((out, dev_id, ctx))
    })?;
    Ok(CciCycleDeviceArrayF32Py {
        inner: Some(inner),
        _ctx: ctx,
        device_id: dev_id,
    })
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyfunction(name = "cci_cycle_cuda_many_series_one_param_dev")]
#[pyo3(signature = (data_time_major, cols, rows, length, factor, device_id=0))]
pub fn cci_cycle_cuda_many_series_one_param_dev_py(
    py: Python<'_>,
    data_time_major: PyReadonlyArray1<'_, f32>,
    cols: usize,
    rows: usize,
    length: usize,
    factor: f64,
    device_id: usize,
) -> PyResult<CciCycleDeviceArrayF32Py> {
    use crate::cuda::cuda_available;
    if !cuda_available() {
        return Err(PyValueError::new_err("CUDA not available"));
    }
    let slice = data_time_major.as_slice()?;
    if slice.len() != cols * rows {
        return Err(PyValueError::new_err("size mismatch for time-major matrix"));
    }
    let params = CciCycleParams {
        length: Some(length),
        factor: Some(factor),
    };
    let (inner, dev_id, ctx) = py.allow_threads(|| {
        let cuda =
            CudaCciCycle::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        let dev_id = cuda.device_id();
        let ctx = cuda.context_arc();
        let out = cuda
            .cci_cycle_many_series_one_param_time_major_dev(slice, cols, rows, &params)
            .map_err(|e| PyValueError::new_err(e.to_string()))?;
        Ok::<_, PyErr>((out, dev_id, ctx))
    })?;
    Ok(CciCycleDeviceArrayF32Py {
        inner: Some(inner),
        _ctx: ctx,
        device_id: dev_id,
    })
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyclass(module = "ta_indicators.cuda", unsendable)]
pub struct CciCycleDeviceArrayF32Py {
    pub(crate) inner: Option<DeviceArrayF32>,
    pub(crate) _ctx: std::sync::Arc<cust::context::Context>,
    pub(crate) device_id: u32,
}

#[cfg(all(feature = "python", feature = "cuda"))]
#[pymethods]
impl CciCycleDeviceArrayF32Py {
    #[getter]
    fn __cuda_array_interface__<'py>(&self, py: Python<'py>) -> PyResult<Bound<'py, PyDict>> {
        let inner = self
            .inner
            .as_ref()
            .ok_or_else(|| PyValueError::new_err("buffer already exported via __dlpack__"))?;
        let d = PyDict::new(py);
        d.set_item("shape", (inner.rows, inner.cols))?;
        d.set_item("typestr", "<f4")?;
        d.set_item(
            "strides",
            (
                inner.cols * std::mem::size_of::<f32>(),
                std::mem::size_of::<f32>(),
            ),
        )?;
        let size = inner.rows.saturating_mul(inner.cols);
        let ptr_val: usize = if size == 0 {
            0
        } else {
            inner.device_ptr() as usize
        };
        d.set_item("data", (ptr_val, false))?;

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

    fn __dlpack_device__(&self) -> PyResult<(i32, i32)> {
        Ok((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<PyObject>,
        max_version: Option<PyObject>,
        dl_device: Option<PyObject>,
        copy: Option<PyObject>,
    ) -> PyResult<PyObject> {
        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 inner = self
            .inner
            .take()
            .ok_or_else(|| PyValueError::new_err("__dlpack__ may only be called once"))?;

        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(test)]
mod tests {
    use super::*;
    use crate::skip_if_unsupported;
    use crate::utilities::data_loader::read_candles_from_csv;
    #[cfg(feature = "proptest")]
    use proptest::prelude::*;
    use std::error::Error;

    macro_rules! generate_all_cci_cycle_tests {
        ($($test_fn:ident),*) => {
            paste::paste! {
                $(
                    #[test]
                    fn [<$test_fn _scalar>]() -> Result<(), Box<dyn Error>> {
                        $test_fn(stringify!([<$test_fn _scalar>]), Kernel::Scalar)
                    }
                )*
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                $(
                    #[test]
                    fn [<$test_fn _avx2>]() -> Result<(), Box<dyn Error>> {
                        $test_fn(stringify!([<$test_fn _avx2>]), Kernel::Avx2)
                    }
                )*
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                $(
                    #[test]
                    fn [<$test_fn _avx512>]() -> Result<(), Box<dyn Error>> {
                        $test_fn(stringify!([<$test_fn _avx512>]), Kernel::Avx512)
                    }
                )*
            }
        };
    }

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

    fn check_cci_cycle_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 = CciCycleInput::from_candles(&candles, "close", CciCycleParams::default());
        let result = cci_cycle_with_kernel(&input, kernel)?;

        let expected_last_five = [
            9.25177192,
            20.49219826,
            35.42917181,
            55.57843075,
            77.78921538,
        ];

        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,
                "[{}] CCI_CYCLE {:?} mismatch at idx {}: got {}, expected {}",
                test_name,
                kernel,
                i,
                val,
                expected_last_five[i]
            );
        }
        Ok(())
    }

    #[test]
    fn test_cci_cycle_into_matches_api() -> Result<(), Box<dyn Error>> {
        let n = 256usize;
        let mut data: Vec<f64> = (0..n)
            .map(|i| ((i as f64) * 0.037).sin() * 2.0 + (i as f64) * 0.01)
            .collect();
        data[0] = f64::NAN;
        data[1] = f64::NAN;
        data[2] = f64::NAN;

        let params = CciCycleParams::default();
        let input = CciCycleInput::from_slice(&data, params);

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

        let mut out = vec![0.0; data.len()];
        cci_cycle_into(&input, &mut out)?;

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

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

        for i in 0..out.len() {
            let a = baseline[i];
            let b = out[i];
            assert!(
                eq_or_both_nan(a, b) || (a - b).abs() <= 1e-12,
                "cci_cycle_into parity mismatch at {}: got {}, expected {}",
                i,
                b,
                a
            );
        }
        Ok(())
    }

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

        let out = cci_cycle_with_kernel(&CciCycleInput::with_default_candles(&c), kernel)?.values;
        for (i, &v) in out.iter().enumerate() {
            if v.is_nan() {
                continue;
            }
            let b = v.to_bits();
            assert_ne!(
                b, 0x11111111_11111111,
                "[{}] alloc_with_nan_prefix poison at {}",
                test_name, i
            );
            assert_ne!(
                b, 0x22222222_22222222,
                "[{}] init_matrix_prefixes poison at {}",
                test_name, i
            );
            assert_ne!(
                b, 0x33333333_33333333,
                "[{}] make_uninit_matrix poison at {}",
                test_name, i
            );
        }
        Ok(())
    }

    fn check_cci_cycle_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 = CciCycleParams {
            length: None,
            factor: None,
        };
        let input = CciCycleInput::from_candles(&candles, "close", default_params);
        let output = cci_cycle_with_kernel(&input, kernel)?;
        assert_eq!(output.values.len(), candles.close.len());

        Ok(())
    }

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

        Ok(())
    }

    fn check_cci_cycle_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 = CciCycleParams {
            length: Some(0),
            factor: None,
        };
        let input = CciCycleInput::from_slice(&input_data, params);
        let res = cci_cycle_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] CCI_CYCLE should fail with zero period",
            test_name
        );
        Ok(())
    }

    fn check_cci_cycle_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 = CciCycleParams {
            length: Some(10),
            factor: None,
        };
        let input = CciCycleInput::from_slice(&data_small, params);
        let res = cci_cycle_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] CCI_CYCLE should fail with period exceeding length",
            test_name
        );
        Ok(())
    }

    fn check_cci_cycle_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 = CciCycleParams::default();
        let input = CciCycleInput::from_slice(&single_point, params);
        let res = cci_cycle_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] CCI_CYCLE should fail with insufficient data",
            test_name
        );
        Ok(())
    }

    fn check_cci_cycle_empty_input(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let empty: [f64; 0] = [];
        let params = CciCycleParams::default();
        let input = CciCycleInput::from_slice(&empty, params);
        let res = cci_cycle_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] CCI_CYCLE should fail with empty input",
            test_name
        );
        Ok(())
    }

    fn check_cci_cycle_all_nan(test_name: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let nan_data = [f64::NAN, f64::NAN, f64::NAN];
        let params = CciCycleParams::default();
        let input = CciCycleInput::from_slice(&nan_data, params);
        let res = cci_cycle_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] CCI_CYCLE should fail with all NaN values",
            test_name
        );
        Ok(())
    }

    fn check_cci_cycle_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 input = CciCycleInput::from_candles(&candles, "close", CciCycleParams::default());
        let output1 = cci_cycle_with_kernel(&input, kernel)?;

        let input2 = CciCycleInput::from_slice(&output1.values, CciCycleParams::default());
        let output2 = cci_cycle_with_kernel(&input2, kernel)?;

        assert_eq!(output2.values.len(), output1.values.len());

        let non_nan_count = output2.values.iter().filter(|&&v| !v.is_nan()).count();
        assert!(
            non_nan_count > 0,
            "[{}] Reinput produced all NaN values",
            test_name
        );

        Ok(())
    }

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

        let data_with_nans = 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,
            f64::NAN,
            14.0,
            15.0,
            16.0,
            17.0,
            18.0,
            19.0,
            20.0,
            21.0,
            22.0,
            23.0,
            24.0,
            25.0,
            26.0,
            27.0,
            28.0,
            29.0,
            30.0,
            31.0,
            32.0,
            33.0,
            34.0,
            35.0,
            36.0,
            37.0,
            38.0,
            39.0,
            40.0,
        ];

        let params = CciCycleParams {
            length: Some(5),
            factor: Some(0.5),
        };
        let input = CciCycleInput::from_slice(&data_with_nans, params.clone());
        let result = cci_cycle_with_kernel(&input, kernel);

        assert!(
            result.is_ok(),
            "[{}] Should handle data with some NaN values",
            test_name
        );

        if let Ok(output) = result {
            assert_eq!(output.values.len(), data_with_nans.len());

            let valid_count = output.values.iter().filter(|&&v| !v.is_nan()).count();
            assert!(
                valid_count > 0,
                "[{}] Should produce some valid values",
                test_name
            );
        }

        let mostly_nans = vec![f64::NAN; 20];
        let input2 = CciCycleInput::from_slice(&mostly_nans, params);
        let result2 = cci_cycle_with_kernel(&input2, kernel);
        assert!(
            result2.is_err(),
            "[{}] Should fail with all NaN values",
            test_name
        );

        Ok(())
    }

    fn check_cci_cycle_streaming(test_name: &str, _kernel: Kernel) -> Result<(), Box<dyn Error>> {
        let params = CciCycleParams {
            length: Some(10),
            factor: Some(0.5),
        };

        let stream_result = CciCycleStream::try_new(params.clone());
        assert!(
            stream_result.is_ok(),
            "[{}] Stream creation should succeed",
            test_name
        );

        let mut stream = stream_result?;

        let test_data = vec![
            1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0,
            17.0, 18.0, 19.0, 20.0,
        ];

        for &value in &test_data {
            let _ = stream.update(value);
        }

        Ok(())
    }

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

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

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

        let default_params = CciCycleParams::default();
        let row = output.values_for(&default_params);

        assert!(
            row.is_some(),
            "[{}] Default parameters not found in batch output",
            test_name
        );

        if let Some(values) = row {
            assert_eq!(values.len(), candles.close.len());

            let non_nan_count = values.iter().filter(|&&v| !v.is_nan()).count();
            assert!(
                non_nan_count > 0,
                "[{}] Default row has no valid values",
                test_name
            );
        }

        assert_eq!(output.cols, candles.close.len());

        Ok(())
    }

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

        let data = vec![1.0; 100];

        let output = CciCycleBatchBuilder::new()
            .kernel(kernel)
            .length_range(10, 20, 5)
            .factor_range(0.3, 0.7, 0.2)
            .apply_slice(&data)?;

        assert_eq!(
            output.combos.len(),
            9,
            "[{}] Unexpected number of parameter combinations",
            test_name
        );
        assert_eq!(output.rows, 9);
        assert_eq!(output.cols, 100);
        assert_eq!(output.values.len(), 900);

        Ok(())
    }

    generate_all_cci_cycle_tests!(
        check_cci_cycle_accuracy,
        check_cci_cycle_no_poison,
        check_cci_cycle_partial_params,
        check_cci_cycle_default_candles,
        check_cci_cycle_zero_period,
        check_cci_cycle_period_exceeds_length,
        check_cci_cycle_very_small_dataset,
        check_cci_cycle_empty_input,
        check_cci_cycle_all_nan,
        check_cci_cycle_reinput,
        check_cci_cycle_nan_handling,
        check_cci_cycle_streaming
    );

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

    #[cfg(feature = "proptest")]
    proptest! {
        #[test]
        fn test_cci_cycle_no_panic(data: Vec<f64>, length in 1usize..100) {
            let params = CciCycleParams {
                length: Some(length),
                factor: Some(0.5),
            };
            let input = CciCycleInput::from_slice(&data, params);
            let _ = cci_cycle(&input);
        }

        #[test]
        fn test_cci_cycle_length_preservation(size in 10usize..100) {
            let data: Vec<f64> = (0..size).map(|i| i as f64).collect();
            let params = CciCycleParams::default();
            let input = CciCycleInput::from_slice(&data, params);

            if let Ok(output) = cci_cycle(&input) {
                prop_assert_eq!(output.values.len(), size);
            }
        }
    }
}
