#[cfg(feature = "python")]
use numpy::{IntoPyArray, PyArray1};
#[cfg(feature = "python")]
use pyo3::exceptions::PyValueError;
#[cfg(feature = "python")]
use pyo3::prelude::*;
#[cfg(feature = "python")]
use pyo3::types::PyDict;
#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
use serde::{Deserialize, Serialize};
#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
use wasm_bindgen::prelude::*;

use crate::utilities::data_loader::{source_type, Candles};
use crate::utilities::enums::Kernel;
use crate::utilities::helpers::{
    alloc_with_nan_prefix, detect_best_batch_kernel, detect_best_kernel, init_matrix_prefixes,
    make_uninit_matrix,
};
#[cfg(feature = "python")]
use crate::utilities::kernel_validation::validate_kernel;
use aligned_vec::{AVec, CACHELINE_ALIGN};
#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
use core::arch::x86_64::*;
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use std::convert::AsRef;
use std::error::Error;
use std::mem::MaybeUninit;
use thiserror::Error;

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

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

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

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

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

#[derive(Debug, Clone)]
pub struct LinearRegSlopeInput<'a> {
    pub data: LinearRegSlopeData<'a>,
    pub params: LinearRegSlopeParams,
}

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

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

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

impl LinearRegSlopeBuilder {
    #[inline(always)]
    pub fn new() -> Self {
        Self::default()
    }
    #[inline(always)]
    pub fn period(mut self, n: usize) -> Self {
        self.period = Some(n);
        self
    }
    #[inline(always)]
    pub fn kernel(mut self, k: Kernel) -> Self {
        self.kernel = k;
        self
    }
    #[inline(always)]
    pub fn apply(self, c: &Candles) -> Result<LinearRegSlopeOutput, LinearRegSlopeError> {
        let p = LinearRegSlopeParams {
            period: self.period,
        };
        let i = LinearRegSlopeInput::from_candles(c, "close", p);
        linearreg_slope_with_kernel(&i, self.kernel)
    }
    #[inline(always)]
    pub fn apply_slice(self, d: &[f64]) -> Result<LinearRegSlopeOutput, LinearRegSlopeError> {
        let p = LinearRegSlopeParams {
            period: self.period,
        };
        let i = LinearRegSlopeInput::from_slice(d, p);
        linearreg_slope_with_kernel(&i, self.kernel)
    }
    #[inline(always)]
    pub fn into_stream(self) -> Result<LinearRegSlopeStream, LinearRegSlopeError> {
        let p = LinearRegSlopeParams {
            period: self.period,
        };
        LinearRegSlopeStream::try_new(p)
    }
}

#[derive(Debug, Error)]
pub enum LinearRegSlopeError {
    #[error("linearreg_slope: Empty data provided.")]
    EmptyInputData,
    #[error("linearreg_slope: All values are NaN.")]
    AllValuesNaN,
    #[error("linearreg_slope: Invalid period: period = {period}, data length = {data_len}")]
    InvalidPeriod { period: usize, data_len: usize },
    #[error("linearreg_slope: Not enough valid data: needed = {needed}, valid = {valid}")]
    NotEnoughValidData { needed: usize, valid: usize },
    #[error("linearreg_slope: Output length mismatch: expected = {expected}, got = {got}")]
    OutputLengthMismatch { expected: usize, got: usize },
    #[error("linearreg_slope: invalid range: start={start}, end={end}, step={step}")]
    InvalidRange {
        start: usize,
        end: usize,
        step: usize,
    },
    #[error("linearreg_slope: invalid kernel for batch: {0:?}")]
    InvalidKernelForBatch(Kernel),
}

#[inline]
pub fn linearreg_slope(
    input: &LinearRegSlopeInput,
) -> Result<LinearRegSlopeOutput, LinearRegSlopeError> {
    linearreg_slope_with_kernel(input, Kernel::Auto)
}

pub fn linearreg_slope_with_kernel(
    input: &LinearRegSlopeInput,
    kernel: Kernel,
) -> Result<LinearRegSlopeOutput, LinearRegSlopeError> {
    let data: &[f64] = input.as_ref();
    if data.is_empty() {
        return Err(LinearRegSlopeError::EmptyInputData);
    }
    let period = input.get_period();

    if period < 2 || period > data.len() {
        return Err(LinearRegSlopeError::InvalidPeriod {
            period,
            data_len: data.len(),
        });
    }
    let first_valid_idx = match data.iter().position(|&x| !x.is_nan()) {
        Some(idx) => idx,
        None => return Err(LinearRegSlopeError::AllValuesNaN),
    };
    if (data.len() - first_valid_idx) < period {
        return Err(LinearRegSlopeError::NotEnoughValidData {
            needed: period,
            valid: data.len() - first_valid_idx,
        });
    }
    let mut out = alloc_with_nan_prefix(data.len(), first_valid_idx + period - 1);
    let chosen = match kernel {
        Kernel::Auto => Kernel::Scalar,
        other => other,
    };
    unsafe {
        match chosen {
            Kernel::Scalar | Kernel::ScalarBatch => {
                linearreg_slope_scalar(data, period, first_valid_idx, &mut out)
            }
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx2 | Kernel::Avx2Batch => {
                linearreg_slope_avx2(data, period, first_valid_idx, &mut out)
            }
            #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
            Kernel::Avx512 | Kernel::Avx512Batch => {
                linearreg_slope_avx512(data, period, first_valid_idx, &mut out)
            }
            _ => unreachable!(),
        }
    }
    Ok(LinearRegSlopeOutput { values: out })
}

#[inline]
pub fn linearreg_slope_scalar(data: &[f64], period: usize, first: usize, out: &mut [f64]) {
    let len = data.len();
    if len == 0 {
        return;
    }

    let p = period as f64;
    let base = first;
    let mut i = base + period - 1;
    if i >= len {
        return;
    }

    let x = 0.5 * p * (p + 1.0);
    let x2 = (p * (p + 1.0) * (2.0 * p + 1.0)) / 6.0;
    let denom = p * x2 - x * x;
    if denom.abs() < f64::EPSILON {
        for out_i in i..len {
            out[out_i] = f64::NAN;
        }
        return;
    }
    let bd = 1.0 / denom;
    let p_bd = p * bd;
    let x_bd = x * bd;

    #[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;
    }

    unsafe {
        let dp = data.as_ptr();

        let mut y = 0.0f64;
        let mut y_c = 0.0f64;
        let mut xy = 0.0f64;
        let mut xy_c = 0.0f64;
        for j in 0..(period - 1) {
            let v = *dp.add(base + j);
            kahan_add(&mut y, &mut y_c, v);
            kahan_add(&mut xy, &mut xy_c, v * (j + 1) as f64);
        }

        let mut in_new = dp.add(base + period - 1);
        let mut in_old = dp.add(base);
        let end = dp.add(len);
        let mut out_ptr = out.as_mut_ptr().add(base + period - 1);

        while in_new.add(1) < end {
            let v0 = *in_new;
            kahan_add(&mut y, &mut y_c, v0);
            kahan_add(&mut xy, &mut xy_c, v0 * p);
            let b0 = xy * p_bd - y * x_bd;
            *out_ptr = if b0.abs() <= 1.1e-8 { 0.0 } else { b0 };
            kahan_add(&mut xy, &mut xy_c, -y);
            kahan_add(&mut y, &mut y_c, -*in_old);

            let v1 = *in_new.add(1);
            kahan_add(&mut y, &mut y_c, v1);
            kahan_add(&mut xy, &mut xy_c, v1 * p);
            let b1 = xy * p_bd - y * x_bd;
            *out_ptr.add(1) = if b1.abs() <= 1.1e-8 { 0.0 } else { b1 };
            kahan_add(&mut xy, &mut xy_c, -y);
            kahan_add(&mut y, &mut y_c, -*in_old.add(1));

            in_new = in_new.add(2);
            in_old = in_old.add(2);
            out_ptr = out_ptr.add(2);
        }

        if in_new < end {
            let v = *in_new;
            kahan_add(&mut y, &mut y_c, v);
            kahan_add(&mut xy, &mut xy_c, v * p);
            let b = xy * p_bd - y * x_bd;
            *out_ptr = if b.abs() <= 1.1e-8 { 0.0 } else { b };
        }
    }
}

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

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

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

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

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

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

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

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

impl LinearRegSlopeBatchBuilder {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn kernel(mut self, k: Kernel) -> Self {
        self.kernel = k;
        self
    }
    #[inline]
    pub fn period_range(mut self, start: usize, end: usize, step: usize) -> Self {
        self.range.period = (start, end, step);
        self
    }
    #[inline]
    pub fn period_static(mut self, p: usize) -> Self {
        self.range.period = (p, p, 0);
        self
    }
    pub fn apply_slice(
        self,
        data: &[f64],
    ) -> Result<LinearRegSlopeBatchOutput, LinearRegSlopeError> {
        linearreg_slope_batch_with_kernel(data, &self.range, self.kernel)
    }
    pub fn with_default_slice(
        data: &[f64],
        k: Kernel,
    ) -> Result<LinearRegSlopeBatchOutput, LinearRegSlopeError> {
        LinearRegSlopeBatchBuilder::new()
            .kernel(k)
            .apply_slice(data)
    }
    pub fn apply_candles(
        self,
        c: &Candles,
        src: &str,
    ) -> Result<LinearRegSlopeBatchOutput, LinearRegSlopeError> {
        let slice = source_type(c, src);
        self.apply_slice(slice)
    }
    pub fn with_default_candles(
        c: &Candles,
    ) -> Result<LinearRegSlopeBatchOutput, LinearRegSlopeError> {
        LinearRegSlopeBatchBuilder::new()
            .kernel(Kernel::Auto)
            .apply_candles(c, "close")
    }
}

#[derive(Clone, Debug)]
pub struct LinearRegSlopeBatchOutput {
    pub values: Vec<f64>,
    pub combos: Vec<LinearRegSlopeParams>,
    pub rows: usize,
    pub cols: usize,
}
impl LinearRegSlopeBatchOutput {
    pub fn row_for_params(&self, p: &LinearRegSlopeParams) -> Option<usize> {
        self.combos
            .iter()
            .position(|c| c.period.unwrap_or(14) == p.period.unwrap_or(14))
    }
    pub fn values_for(&self, p: &LinearRegSlopeParams) -> 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: &LinearRegSlopeBatchRange) -> Vec<LinearRegSlopeParams> {
    fn axis_usize(
        (start, end, step): (usize, usize, usize),
    ) -> Result<Vec<usize>, LinearRegSlopeError> {
        if step == 0 || start == end {
            return Ok(vec![start]);
        }
        if start < end {
            let mut v = Vec::new();
            let st = step.max(1);
            let mut x = start;
            while x <= end {
                v.push(x);
                match x.checked_add(st) {
                    Some(next) => x = next,
                    None => break,
                }
            }
            if v.is_empty() {
                return Err(LinearRegSlopeError::InvalidRange { start, end, step });
            }
            return Ok(v);
        }

        let mut v = Vec::new();
        let st = step.max(1) as isize;
        let mut x = start as isize;
        let end_i = end as isize;
        while x >= end_i {
            v.push(x as usize);
            x -= st;
        }
        if v.is_empty() {
            return Err(LinearRegSlopeError::InvalidRange { start, end, step });
        }
        Ok(v)
    }
    let periods = axis_usize(r.period).unwrap_or_else(|_| Vec::new());
    let mut out = Vec::with_capacity(periods.len());
    for p in periods {
        out.push(LinearRegSlopeParams { period: Some(p) });
    }
    out
}

#[inline(always)]
pub fn linearreg_slope_batch_slice(
    data: &[f64],
    sweep: &LinearRegSlopeBatchRange,
    kern: Kernel,
) -> Result<LinearRegSlopeBatchOutput, LinearRegSlopeError> {
    linearreg_slope_batch_inner(data, sweep, kern, false)
}

#[inline(always)]
pub fn linearreg_slope_batch_par_slice(
    data: &[f64],
    sweep: &LinearRegSlopeBatchRange,
    kern: Kernel,
) -> Result<LinearRegSlopeBatchOutput, LinearRegSlopeError> {
    linearreg_slope_batch_inner(data, sweep, kern, true)
}

#[inline(always)]
fn linearreg_slope_batch_inner(
    data: &[f64],
    sweep: &LinearRegSlopeBatchRange,
    kern: Kernel,
    parallel: bool,
) -> Result<LinearRegSlopeBatchOutput, LinearRegSlopeError> {
    let combos = expand_grid(sweep);
    if combos.is_empty() {
        return Err(LinearRegSlopeError::InvalidRange {
            start: sweep.period.0,
            end: sweep.period.1,
            step: sweep.period.2,
        });
    }

    for combo in &combos {
        let period = combo.period.unwrap();
        if period < 2 {
            return Err(LinearRegSlopeError::InvalidPeriod {
                period,
                data_len: data.len(),
            });
        }
    }

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

    let _total = rows
        .checked_mul(cols)
        .ok_or(LinearRegSlopeError::InvalidRange {
            start: sweep.period.0,
            end: sweep.period.1,
            step: sweep.period.2,
        })?;

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

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

    linearreg_slope_batch_inner_into(data, sweep, kern, parallel, out)?;

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

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

#[inline(always)]
fn linearreg_slope_batch_inner_into(
    data: &[f64],
    sweep: &LinearRegSlopeBatchRange,
    kern: Kernel,
    parallel: bool,
    out: &mut [f64],
) -> Result<Vec<LinearRegSlopeParams>, LinearRegSlopeError> {
    let combos = expand_grid(sweep);
    if combos.is_empty() {
        return Err(LinearRegSlopeError::InvalidRange {
            start: sweep.period.0,
            end: sweep.period.1,
            step: sweep.period.2,
        });
    }

    for combo in &combos {
        let period = combo.period.unwrap();
        if period < 2 {
            return Err(LinearRegSlopeError::InvalidPeriod {
                period,
                data_len: data.len(),
            });
        }
    }

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

    let rows = combos.len();
    let cols = data.len();

    let _total = rows
        .checked_mul(cols)
        .ok_or(LinearRegSlopeError::InvalidRange {
            start: sweep.period.0,
            end: sweep.period.1,
            step: sweep.period.2,
        })?;

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

    if rows <= 1 {
        let out_uninit = unsafe {
            std::slice::from_raw_parts_mut(out.as_mut_ptr() as *mut MaybeUninit<f64>, out.len())
        };
        let do_row_scalar = |row: usize, dst_mu: &mut [MaybeUninit<f64>]| unsafe {
            let period = combos[row].period.unwrap();
            let dst =
                core::slice::from_raw_parts_mut(dst_mu.as_mut_ptr() as *mut f64, dst_mu.len());
            match kern {
                Kernel::Scalar => linearreg_slope_row_scalar(data, first, period, dst),
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                Kernel::Avx2 => linearreg_slope_row_avx2(data, first, period, dst),
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                Kernel::Avx512 => linearreg_slope_row_avx512(data, first, period, dst),
                _ => unreachable!(),
            }
        };
        if parallel {
            #[cfg(not(target_arch = "wasm32"))]
            {
                out_uninit
                    .par_chunks_mut(cols)
                    .enumerate()
                    .for_each(|(row, slice)| do_row_scalar(row, slice));
            }
            #[cfg(target_arch = "wasm32")]
            {
                for (row, slice) in out_uninit.chunks_mut(cols).enumerate() {
                    do_row_scalar(row, slice);
                }
            }
        } else {
            for (row, slice) in out_uninit.chunks_mut(cols).enumerate() {
                do_row_scalar(row, slice);
            }
        }
    } else {
        let mut py = Vec::with_capacity(data.len() + 1);
        let mut pky = Vec::with_capacity(data.len() + 1);
        py.push(0.0);
        pky.push(0.0);
        if first > 0 {
            py.resize(first + 1, 0.0);
            pky.resize(first + 1, 0.0);
        }
        for i in first..data.len() {
            let y = unsafe { *data.get_unchecked(i) };
            let prev_y = unsafe { *py.get_unchecked(i) };
            let prev_ky = unsafe { *pky.get_unchecked(i) };
            py.push(prev_y + y);
            pky.push(prev_ky + (i as f64) * y);
        }

        let out_uninit = unsafe {
            std::slice::from_raw_parts_mut(out.as_mut_ptr() as *mut MaybeUninit<f64>, out.len())
        };
        let do_row_prefix = |row: usize, dst_mu: &mut [MaybeUninit<f64>]| {
            let period = combos[row].period.unwrap();
            let n = period as f64;
            let m = (period - 1) as f64;
            let sum_x = 0.5 * m * n;
            let sum_x2 = (m * n) * (2.0 * m + 1.0) / 6.0;
            let denom = n * sum_x2 - sum_x * sum_x;
            if denom.abs() < f64::EPSILON {
                return;
            }
            let dst = unsafe {
                core::slice::from_raw_parts_mut(dst_mu.as_mut_ptr() as *mut f64, dst_mu.len())
            };
            let start_i = first + period - 1;
            for i in start_i..cols {
                let s = i + 1 - period;
                let sy = unsafe { *py.get_unchecked(i + 1) - *py.get_unchecked(s) };
                let sxy = unsafe {
                    (*pky.get_unchecked(i + 1) - *pky.get_unchecked(s)) - (s as f64) * sy
                };
                let num = n.mul_add(sxy, -sum_x * sy);
                dst[i] = num / denom;
            }
        };

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

    Ok(combos)
}

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

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

#[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
#[inline(always)]
unsafe fn linearreg_slope_row_avx512(data: &[f64], first: usize, period: usize, out: &mut [f64]) {
    if period <= 32 {
        linearreg_slope_row_avx512_short(data, first, period, out);
    } else {
        linearreg_slope_row_avx512_long(data, first, period, out);
    }
}

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

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

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

    n: f64,
    m: f64,
    sum_x: f64,
    sum_x2: f64,
    denom: f64,
    inv_denom: f64,

    sum_y: f64,
    sum_y_c: f64,
    sum_xy: f64,
    sum_xy_c: f64,

    step: usize,
    recalc_mask: usize,
}

impl LinearRegSlopeStream {
    #[inline]
    pub fn try_new(params: LinearRegSlopeParams) -> Result<Self, LinearRegSlopeError> {
        let period = params.period.unwrap_or(14);
        if period < 2 {
            return Err(LinearRegSlopeError::InvalidPeriod {
                period,
                data_len: 0,
            });
        }

        let n = period as f64;
        let m = (period - 1) as f64;

        let sum_x = 0.5 * m * n;

        let sum_x2 = (m * n) * (2.0 * m + 1.0) / 6.0;

        let denom = n * sum_x2 - sum_x * sum_x;

        let inv_denom = if denom.abs() > f64::EPSILON {
            1.0 / denom
        } else {
            f64::NAN
        };

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

            n,
            m,
            sum_x,
            sum_x2,
            denom,
            inv_denom,

            sum_y: 0.0,
            sum_y_c: 0.0,
            sum_xy: 0.0,
            sum_xy_c: 0.0,

            step: 0,
            recalc_mask: 255,
        })
    }

    #[inline(always)]
    pub fn update(&mut self, value: f64) -> Option<f64> {
        if !value.is_finite() {
            self.reset_state();
            return None;
        }

        if !self.filled {
            let j = self.warm_count as f64;

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

            let y0 = value - self.sum_y_c;
            let t0 = self.sum_y + y0;
            self.sum_y_c = (t0 - self.sum_y) - y0;
            self.sum_y = t0;

            let jy = j * value;
            let y1 = jy - self.sum_xy_c;
            let t1 = self.sum_xy + y1;
            self.sum_xy_c = (t1 - self.sum_xy) - y1;
            self.sum_xy = t1;

            self.warm_count += 1;
            if self.warm_count < self.period {
                return None;
            }

            self.filled = true;

            return self.emit_slope();
        }

        let y_old = self.buffer[self.head];
        self.buffer[self.head] = value;
        self.head = (self.head + 1) % self.period;

        let delta0 = value - y_old;
        let yk0 = delta0 - self.sum_y_c;
        let t0 = self.sum_y + yk0;
        self.sum_y_c = (t0 - self.sum_y) - yk0;
        self.sum_y = t0;

        let delta1 = -self.sum_y + self.n * value;
        let yk1 = delta1 - self.sum_xy_c;
        let t1 = self.sum_xy + yk1;
        self.sum_xy_c = (t1 - self.sum_xy) - yk1;
        self.sum_xy = t1;

        self.step = self.step.wrapping_add(1);
        if (self.step & self.recalc_mask) == 0 {
            self.recompute_exact();
        }

        self.emit_slope()
    }

    #[inline(always)]
    fn emit_slope(&self) -> Option<f64> {
        if !self.filled || !(self.denom.is_finite()) {
            return None;
        }

        if self.m > 0.0 {
            let first = self.buffer[self.head];
            let last = self.buffer[(self.head + self.period - 1) % self.period];
            let a2 = (last - first) / self.m;

            let s0_model = a2.mul_add(self.sum_x, first * self.n);
            let s1_model = a2.mul_add(self.sum_x2, first * self.sum_x);

            let tol0 = 1e-12_f64 * 1.0_f64.max(self.sum_y.abs()).max(s0_model.abs());
            let tol1 = 1e-12_f64 * 1.0_f64.max(self.sum_xy.abs()).max(s1_model.abs());
            if (self.sum_y - s0_model).abs() <= tol0 && (self.sum_xy - s1_model).abs() <= tol1 {
                return Some(a2);
            }
        }

        let num = self.n.mul_add(self.sum_xy, -self.sum_x * self.sum_y);
        Some(num * self.inv_denom)
    }

    #[inline(always)]
    fn recompute_exact(&mut self) {
        let mut sy = 0.0;
        let mut sxy = 0.0;
        let mut idx = self.head;
        for j in 0..self.period {
            let y = self.buffer[idx];
            sy += y;
            sxy = (j as f64).mul_add(y, sxy);
            idx += 1;
            if idx == self.period {
                idx = 0;
            }
        }
        self.sum_y = sy;
        self.sum_y_c = 0.0;
        self.sum_xy = sxy;
        self.sum_xy_c = 0.0;
    }

    #[inline(always)]
    fn reset_state(&mut self) {
        self.head = 0;
        self.filled = false;
        self.warm_count = 0;
        self.sum_y = 0.0;
        self.sum_y_c = 0.0;
        self.sum_xy = 0.0;
        self.sum_xy_c = 0.0;
    }
}

#[inline(always)]
fn expand_grid_stream(_r: &LinearRegSlopeBatchRange) -> Vec<LinearRegSlopeParams> {
    vec![LinearRegSlopeParams::default()]
}

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

    let slice_in = data.as_slice()?;
    let kern = validate_kernel(kernel, false)?;
    let params = LinearRegSlopeParams {
        period: Some(period),
    };
    let linearreg_slope_in = LinearRegSlopeInput::from_slice(slice_in, params);

    let result_vec: Vec<f64> = py
        .allow_threads(|| linearreg_slope_with_kernel(&linearreg_slope_in, kern).map(|o| o.values))
        .map_err(|e| PyValueError::new_err(e.to_string()))?;

    Ok(result_vec.into_pyarray(py))
}

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

    let slice_in = data.as_slice()?;
    let sweep = LinearRegSlopeBatchRange {
        period: period_range,
    };

    let combos = expand_grid(&sweep);
    let rows = combos.len();
    if rows == 0 {
        return Err(PyValueError::new_err(
            "linearreg_slope: invalid period range (empty expansion)",
        ));
    }
    let cols = slice_in.len();
    let total = rows
        .checked_mul(cols)
        .ok_or_else(|| PyValueError::new_err("linearreg_slope: rows*cols overflow"))?;

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

    let first = slice_in
        .iter()
        .position(|x| !x.is_nan())
        .ok_or_else(|| PyValueError::new_err("All values are NaN"))?;
    let warm: Vec<usize> = combos
        .iter()
        .map(|c| first + c.period.unwrap() - 1)
        .collect();
    unsafe {
        let out_mu: &mut [MaybeUninit<f64>] = core::slice::from_raw_parts_mut(
            slice_out.as_mut_ptr() as *mut MaybeUninit<f64>,
            slice_out.len(),
        );
        init_matrix_prefixes(out_mu, cols, &warm);
    }

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

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

#[cfg(all(feature = "python", feature = "cuda"))]
use crate::cuda::moving_averages::CudaLinearregSlope;
#[cfg(all(feature = "python", feature = "cuda"))]
use crate::indicators::moving_averages::alma::DeviceArrayF32Py;

#[cfg(all(feature = "python", feature = "cuda"))]
#[pyfunction(name = "linearreg_slope_cuda_batch_dev")]
#[pyo3(signature = (data_f32, period_range, device_id=0))]
pub fn linearreg_slope_cuda_batch_dev_py<'py>(
    py: Python<'py>,
    data_f32: numpy::PyReadonlyArray1<'py, f32>,
    period_range: (usize, usize, usize),
    device_id: usize,
) -> PyResult<(DeviceArrayF32Py, Bound<'py, PyDict>)> {
    use crate::cuda::cuda_available;
    use numpy::IntoPyArray;
    use pyo3::types::PyDict;

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

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

    let (inner, combos, ctx, dev_id) = py.allow_threads(|| {
        let cuda =
            CudaLinearregSlope::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        let ctx = cuda.context_arc();
        let dev = cuda.device_id();
        cuda.linearreg_slope_batch_dev(slice_in, &sweep)
            .map(|(inner, combos)| (inner, combos, ctx, dev))
            .map_err(|e| PyValueError::new_err(e.to_string()))
    })?;

    let dict = PyDict::new(py);
    let periods: Vec<u64> = combos.iter().map(|c| c.period.unwrap() as u64).collect();
    dict.set_item("periods", periods.into_pyarray(py))?;

    Ok((
        DeviceArrayF32Py {
            inner,
            _ctx: Some(ctx),
            device_id: Some(dev_id),
        },
        dict,
    ))
}

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

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

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

    let (inner, ctx, dev_id) = py.allow_threads(|| {
        let cuda =
            CudaLinearregSlope::new(device_id).map_err(|e| PyValueError::new_err(e.to_string()))?;
        let ctx = cuda.context_arc();
        let dev = cuda.device_id();
        cuda.linearreg_slope_many_series_one_param_time_major_dev(flat_in, cols, rows, &params)
            .map(|inner| (inner, ctx, dev))
            .map_err(|e| PyValueError::new_err(e.to_string()))
    })?;

    Ok(DeviceArrayF32Py {
        inner,
        _ctx: Some(ctx),
        device_id: Some(dev_id),
    })
}

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

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

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

pub fn linearreg_slope_into_slice(
    dst: &mut [f64],
    input: &LinearRegSlopeInput,
    kern: Kernel,
) -> Result<(), LinearRegSlopeError> {
    let data: &[f64] = input.as_ref();
    if data.is_empty() {
        return Err(LinearRegSlopeError::EmptyInputData);
    }
    let period = input.get_period();

    if period < 2 || period > data.len() {
        return Err(LinearRegSlopeError::InvalidPeriod {
            period,
            data_len: data.len(),
        });
    }
    if dst.len() != data.len() {
        return Err(LinearRegSlopeError::OutputLengthMismatch {
            expected: data.len(),
            got: dst.len(),
        });
    }

    let first_valid_idx = match data.iter().position(|&x| !x.is_nan()) {
        Some(idx) => idx,
        None => return Err(LinearRegSlopeError::AllValuesNaN),
    };
    if (data.len() - first_valid_idx) < period {
        return Err(LinearRegSlopeError::NotEnoughValidData {
            needed: period,
            valid: data.len() - first_valid_idx,
        });
    }

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

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

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

    Ok(())
}

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

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

    Ok(output)
}

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

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

    unsafe {
        let data = std::slice::from_raw_parts(in_ptr, len);
        let params = LinearRegSlopeParams {
            period: Some(period),
        };
        let input = LinearRegSlopeInput::from_slice(data, params);

        if in_ptr == out_ptr {
            let mut temp = vec![0.0; len];
            linearreg_slope_into_slice(&mut temp, &input, detect_best_kernel())
                .map_err(|e| JsValue::from_str(&e.to_string()))?;
            let out = std::slice::from_raw_parts_mut(out_ptr, len);
            out.copy_from_slice(&temp);
        } else {
            let out = std::slice::from_raw_parts_mut(out_ptr, len);
            linearreg_slope_into_slice(out, &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 linearreg_slope_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 linearreg_slope_free(ptr: *mut f64, len: usize) {
    if !ptr.is_null() {
        unsafe {
            let _ = Vec::from_raw_parts(ptr, len, len);
        }
    }
}

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

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

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

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

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

    let js_output = LinearRegSlopeBatchJsOutput {
        values: output.values,
        combos: output.combos,
        rows: output.rows,
        cols: output.cols,
    };

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

#[cfg(all(target_arch = "wasm32", feature = "wasm"))]
#[wasm_bindgen]
pub fn linearreg_slope_batch_into(
    in_ptr: *const f64,
    out_ptr: *mut f64,
    len: usize,
    period_start: usize,
    period_end: usize,
    period_step: usize,
) -> Result<usize, JsValue> {
    if in_ptr.is_null() || out_ptr.is_null() {
        return Err(JsValue::from_str(
            "null pointer passed to linearreg_slope_batch_into",
        ));
    }
    unsafe {
        let data = core::slice::from_raw_parts(in_ptr, len);
        let sweep = LinearRegSlopeBatchRange {
            period: (period_start, period_end, period_step),
        };
        let combos = expand_grid(&sweep);
        let rows = combos.len();
        if rows == 0 {
            return Err(JsValue::from_str(
                "linearreg_slope: invalid period range (empty expansion)",
            ));
        }
        let cols = len;
        let total = rows
            .checked_mul(cols)
            .ok_or_else(|| JsValue::from_str("linearreg_slope: rows*cols overflow"))?;

        let out = core::slice::from_raw_parts_mut(out_ptr, total);

        let first = data
            .iter()
            .position(|x| !x.is_nan())
            .ok_or_else(|| JsValue::from_str("All values are NaN"))?;
        let warm: Vec<usize> = combos
            .iter()
            .map(|c| first + c.period.unwrap() - 1)
            .collect();
        let out_mu =
            core::slice::from_raw_parts_mut(out.as_mut_ptr() as *mut MaybeUninit<f64>, out.len());
        init_matrix_prefixes(out_mu, cols, &warm);

        linearreg_slope_batch_inner_into(data, &sweep, detect_best_kernel(), false, out)
            .map_err(|e| JsValue::from_str(&e.to_string()))?;

        Ok(rows)
    }
}

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

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

        Ok(())
    }

    fn check_linearreg_slope_accuracy(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let input_data = [100.0, 98.0, 95.0, 90.0, 85.0, 80.0, 78.0, 77.0, 79.0, 81.0];
        let params = LinearRegSlopeParams { period: Some(5) };
        let input = LinearRegSlopeInput::from_slice(&input_data, params);
        let result = linearreg_slope_with_kernel(&input, kernel)?;
        assert_eq!(result.values.len(), input_data.len());
        for val in &result.values[4..] {
            assert!(
                !val.is_nan(),
                "Expected valid slope values after period-1 index"
            );
        }
        Ok(())
    }

    fn check_linearreg_slope_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 = LinearRegSlopeParams { period: Some(0) };
        let input = LinearRegSlopeInput::from_slice(&input_data, params);
        let res = linearreg_slope_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] linearreg_slope should fail with zero period",
            test_name
        );
        Ok(())
    }

    fn check_linearreg_slope_period_one(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let input_data = [10.0, 20.0, 30.0, 40.0, 50.0];
        let params = LinearRegSlopeParams { period: Some(1) };
        let input = LinearRegSlopeInput::from_slice(&input_data, params);
        let res = linearreg_slope_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] linearreg_slope should fail with period=1 (needs at least 2 points for slope)",
            test_name
        );

        if let Err(e) = res {
            let msg = e.to_string();
            assert!(
                msg.contains("Invalid period"),
                "[{}] Expected 'Invalid period' error, got: {}",
                test_name,
                msg
            );
        }
        Ok(())
    }

    fn check_linearreg_slope_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 = LinearRegSlopeParams { period: Some(10) };
        let input = LinearRegSlopeInput::from_slice(&data_small, params);
        let res = linearreg_slope_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] linearreg_slope should fail with period exceeding length",
            test_name
        );
        Ok(())
    }

    fn check_linearreg_slope_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 = LinearRegSlopeParams { period: Some(14) };
        let input = LinearRegSlopeInput::from_slice(&single_point, params);
        let res = linearreg_slope_with_kernel(&input, kernel);
        assert!(
            res.is_err(),
            "[{}] linearreg_slope should fail with insufficient data",
            test_name
        );
        Ok(())
    }

    fn check_linearreg_slope_reinput(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let input_data = [10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0];
        let first_params = LinearRegSlopeParams { period: Some(3) };
        let first_input = LinearRegSlopeInput::from_slice(&input_data, first_params);
        let first_result = linearreg_slope_with_kernel(&first_input, kernel)?;
        let second_params = LinearRegSlopeParams { period: Some(3) };
        let second_input = LinearRegSlopeInput::from_slice(&first_result.values, second_params);
        let second_result = linearreg_slope_with_kernel(&second_input, kernel)?;
        assert_eq!(second_result.values.len(), first_result.values.len());
        Ok(())
    }

    fn check_linearreg_slope_nan_handling(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test_name);
        let file_path = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let candles = read_candles_from_csv(file_path)?;
        let input = LinearRegSlopeInput::from_candles(
            &candles,
            "close",
            LinearRegSlopeParams { period: Some(14) },
        );
        let res = linearreg_slope_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(())
    }

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

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

        let test_params = vec![
            LinearRegSlopeParams::default(),
            LinearRegSlopeParams { period: Some(2) },
            LinearRegSlopeParams { period: Some(3) },
            LinearRegSlopeParams { period: Some(5) },
            LinearRegSlopeParams { period: Some(7) },
            LinearRegSlopeParams { period: Some(10) },
            LinearRegSlopeParams { period: Some(14) },
            LinearRegSlopeParams { period: Some(20) },
            LinearRegSlopeParams { period: Some(21) },
            LinearRegSlopeParams { period: Some(30) },
            LinearRegSlopeParams { period: Some(50) },
            LinearRegSlopeParams { period: Some(100) },
            LinearRegSlopeParams { period: Some(200) },
        ];

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

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

                let bits = val.to_bits();

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

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

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

        Ok(())
    }

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

    macro_rules! generate_all_linearreg_slope_tests {
        ($($test_fn:ident),*) => {
            paste::paste! {
                $(
                    #[test]
                    fn [<$test_fn _scalar_f64>]() {
                        let _ = $test_fn(stringify!([<$test_fn _scalar_f64>]), Kernel::Scalar);
                    }
                )*
                #[cfg(all(feature = "nightly-avx", target_arch = "x86_64"))]
                $(
                    #[test]
                    fn [<$test_fn _avx2_f64>]() {
                        let _ = $test_fn(stringify!([<$test_fn _avx2_f64>]), Kernel::Avx2);
                    }
                    #[test]
                    fn [<$test_fn _avx512_f64>]() {
                        let _ = $test_fn(stringify!([<$test_fn _avx512_f64>]), Kernel::Avx512);
                    }
                )*
            }
        }
    }
    generate_all_linearreg_slope_tests!(
        check_linearreg_slope_partial_params,
        check_linearreg_slope_accuracy,
        check_linearreg_slope_zero_period,
        check_linearreg_slope_period_one,
        check_linearreg_slope_period_exceeds_length,
        check_linearreg_slope_very_small_dataset,
        check_linearreg_slope_reinput,
        check_linearreg_slope_nan_handling,
        check_linearreg_slope_no_poison
    );

    #[cfg(not(all(target_arch = "wasm32", feature = "wasm")))]
    #[test]
    fn test_linearreg_slope_into_matches_api() -> Result<(), Box<dyn Error>> {
        let n = 512usize;
        let mut data = vec![0.0f64; n];
        for i in 0..n {
            let t = i as f64;
            data[i] = 1.0 + 0.01 * t + (t * 0.2).sin() * 0.5;
        }

        let input = LinearRegSlopeInput::from_slice(&data, LinearRegSlopeParams::default());

        let base = linearreg_slope(&input)?.values;

        let mut into_out = vec![0.0f64; n];
        linearreg_slope_into(&input, &mut into_out)?;

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

        assert_eq!(base.len(), into_out.len());
        for i in 0..n {
            assert!(
                eq_or_both_nan(base[i], into_out[i]),
                "linearreg_slope_into mismatch at {}: base={}, into={}",
                i,
                base[i],
                into_out[i]
            );
        }

        Ok(())
    }

    fn check_batch_default_row(test: &str, kernel: Kernel) -> Result<(), Box<dyn Error>> {
        skip_if_unsupported!(kernel, test);
        let file = "src/data/2018-09-01-2024-Bitfinex_Spot-4h.csv";
        let c = read_candles_from_csv(file)?;
        let output = LinearRegSlopeBatchBuilder::new()
            .kernel(kernel)
            .apply_candles(&c, "close")?;
        let def = LinearRegSlopeParams::default();
        let row = output.values_for(&def).expect("default row missing");
        assert_eq!(row.len(), c.close.len());
        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, 10, 2),
            (5, 25, 5),
            (30, 60, 15),
            (2, 5, 1),
            (10, 30, 10),
            (14, 21, 7),
            (14, 14, 0),
            (50, 150, 25),
            (3, 15, 3),
        ];

        for (cfg_idx, &(p_start, p_end, p_step)) in test_configs.iter().enumerate() {
            let output = LinearRegSlopeBatchBuilder::new()
                .kernel(kernel)
                .period_range(p_start, p_end, p_step)
                .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 combo = &output.combos[row];

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

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

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

        Ok(())
    }

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

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

    #[cfg(test)]
    fn check_linearreg_slope_property(
        test_name: &str,
        kernel: Kernel,
    ) -> Result<(), Box<dyn std::error::Error>> {
        use proptest::prelude::*;
        skip_if_unsupported!(kernel, test_name);

        let strat = (2usize..=100)
            .prop_flat_map(|period| {
                (
                    prop::collection::vec(
                        (-1e6f64..1e6f64).prop_filter("finite", |x| x.is_finite()),
                        period..=500,
                    ),
                    Just(period),
                    0usize..=5,
                )
            })
            .prop_map(|(mut data, period, scenario)| {
                match scenario {
                    0 => {}
                    1 => {
                        let constant = data.get(0).copied().unwrap_or(100.0);
                        data.iter_mut().for_each(|x| *x = constant);
                    }
                    2 => {
                        for (i, val) in data.iter_mut().enumerate() {
                            *val = 2.0 * i as f64 + 10.0;
                        }
                    }
                    3 => {
                        let mut base = 100.0;
                        for val in data.iter_mut() {
                            *val = base;
                            base += (0.1 + (*val).abs() * 1e-6);
                        }
                    }
                    4 => {
                        let mut base = 1000.0;
                        for val in data.iter_mut() {
                            *val = base;
                            base -= (0.1 + (*val).abs() * 1e-6);
                        }
                    }
                    5 => {
                        for (i, val) in data.iter_mut().enumerate() {
                            *val = if i % 20 == 0 {
                                1000.0 * (if i % 40 == 0 { 1.0 } else { -1.0 })
                            } else {
                                10.0 + i as f64 * 0.5
                            };
                        }
                    }
                    _ => unreachable!(),
                }
                (data, period)
            });

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

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

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

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

                for i in (period - 1)..data.len() {
                    let window = &data[i + 1 - period..=i];
                    let y = out[i];
                    let r = ref_out[i];

                    if y.is_finite() && r.is_finite() {
                        let y_bits = y.to_bits();
                        let r_bits = r.to_bits();
                        let ulp_diff: u64 = y_bits.abs_diff(r_bits);

                        prop_assert!(
                            (y - r).abs() <= 1e-9 || ulp_diff <= 8,
                            "Kernel mismatch at idx {}: {} vs {} (ULP={})",
                            i,
                            y,
                            r,
                            ulp_diff
                        );
                    } else {
                        prop_assert_eq!(
                            y.is_nan(),
                            r.is_nan(),
                            "NaN mismatch at idx {}: {} vs {}",
                            i,
                            y,
                            r
                        );
                    }

                    if window
                        .windows(2)
                        .all(|w| (w[0] - w[1]).abs() < f64::EPSILON)
                    {
                        prop_assert!(
                            y.abs() <= 1e-8,
                            "Expected slope ~0 for constant data at idx {}, got {}",
                            i,
                            y
                        );
                    }

                    let is_linear = {
                        if period >= 3 {
                            let x1 = 0.0;
                            let y1 = window[0];
                            let x2 = (period - 1) as f64;
                            let y2 = window[period - 1];
                            let expected_slope = (y2 - y1) / (x2 - x1);

                            let mut is_linear = true;
                            for (j, &val) in window.iter().enumerate() {
                                let expected = y1 + expected_slope * j as f64;
                                if (val - expected).abs() > 1e-9 {
                                    is_linear = false;
                                    break;
                                }
                            }

                            if is_linear {
                                prop_assert!(
                                    (y - expected_slope).abs() <= 1e-9,
                                    "Linear data slope mismatch at idx {}: {} vs expected {}",
                                    i,
                                    y,
                                    expected_slope
                                );
                            }
                            is_linear
                        } else {
                            false
                        }
                    };

                    let is_increasing = window.windows(2).all(|w| w[1] > w[0]);
                    if is_increasing && !is_linear {
                        prop_assert!(
                            y > 1e-8,
                            "Expected positive slope for increasing data at idx {}, got {}",
                            i,
                            y
                        );
                    }

                    let is_decreasing = window.windows(2).all(|w| w[1] < w[0]);
                    if is_decreasing && !is_linear {
                        prop_assert!(
                            y < -1e-8,
                            "Expected negative slope for decreasing data at idx {}, got {}",
                            i,
                            y
                        );
                    }

                    if y.is_finite() {
                        let data_range = window.iter().cloned().fold(f64::NEG_INFINITY, f64::max)
                            - window.iter().cloned().fold(f64::INFINITY, f64::min);

                        if data_range < 1e-9 {
                            prop_assert!(
                                y.abs() <= 1e-6,
                                "Expected near-zero slope for constant data at idx {}, got {}",
                                i,
                                y
                            );
                        } else {
                            let max_slope = data_range / (period as f64 * 0.5);

                            prop_assert!(
                                y.abs() <= max_slope * 5.0,
                                "Slope magnitude too large at idx {}: {} (max expected ~{})",
                                i,
                                y.abs(),
                                max_slope
                            );
                        }
                    }

                    prop_assert!(!y.is_infinite(), "Found infinite value at idx {}: {}", i, y);
                }

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

        Ok(())
    }

    #[cfg(test)]
    generate_all_linearreg_slope_tests!(check_linearreg_slope_property);
}
