//! Determinant computation via LU factorization.
//!
//! Computes the determinant of a square matrix using LU factorization:
//! `det(A) = det(P) * det(L) * det(U) = (-1)^s * ∏ U[i,i]`
//! where s is the number of row swaps performed by partial pivoting and
//! L has unit diagonal (so det(L) = 1).
//!
//! Also provides a numerically stable log-determinant variant that avoids
//! overflow/underflow for large matrices.

#![allow(dead_code)]

use oxicuda_blas::GpuFloat;
use oxicuda_memory::DeviceBuffer;

use crate::dense::lu::lu_factorize;
use crate::error::{SolverError, SolverResult};
use crate::handle::SolverHandle;

/// Converts a `GpuFloat` value to `f64` via bit reinterpretation.
fn to_f64_val<T: GpuFloat>(val: T) -> f64 {
    if T::SIZE == 4 {
        f32::from_bits(val.to_bits_u64() as u32) as f64
    } else {
        f64::from_bits(val.to_bits_u64())
    }
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Computes the determinant of a square matrix.
///
/// The matrix `a` is stored in column-major order with leading dimension `lda`.
/// The input buffer is **not modified** — an internal copy is made for the
/// LU factorization.
///
/// # Arguments
///
/// * `handle` — solver handle.
/// * `a` — square matrix data (n x n, column-major, lda stride). Not modified.
/// * `n` — matrix dimension.
/// * `lda` — leading dimension (>= n).
///
/// # Returns
///
/// The determinant as an `f64`. For singular matrices, returns 0.0.
///
/// # Errors
///
/// Returns [`SolverError::DimensionMismatch`] for invalid dimensions.
pub fn determinant<T: GpuFloat>(
    handle: &mut SolverHandle,
    a: &DeviceBuffer<T>,
    n: u32,
    lda: u32,
) -> SolverResult<f64> {
    if n == 0 {
        return Ok(1.0); // det of 0x0 matrix is 1 by convention.
    }
    validate_dimensions::<T>(a, n, lda)?;

    let (log_abs, sign) = log_determinant(handle, a, n, lda)?;

    if sign == 0.0 {
        return Ok(0.0);
    }

    // Reconstruct det = sign * exp(log_abs).
    // Guard against overflow/underflow.
    if log_abs > 709.0 {
        // Overflow: return +/- infinity.
        return Ok(sign * f64::INFINITY);
    }
    if log_abs < -745.0 {
        // Underflow: return 0.
        return Ok(0.0);
    }

    Ok(sign * log_abs.exp())
}

/// Computes the log-determinant of a square matrix.
///
/// Returns `(log|det(A)|, sign)` where `sign` is +1.0, -1.0, or 0.0 (for
/// singular matrices). This is numerically stable for large matrices where
/// the determinant might overflow or underflow.
///
/// The matrix `a` is **not modified** — an internal copy is made.
///
/// # Arguments
///
/// * `handle` — solver handle.
/// * `a` — square matrix data (n x n, column-major, lda stride). Not modified.
/// * `n` — matrix dimension.
/// * `lda` — leading dimension (>= n).
///
/// # Returns
///
/// A tuple `(log_abs_det, sign)`.
///
/// # Errors
///
/// Returns [`SolverError::DimensionMismatch`] for invalid dimensions.
pub fn log_determinant<T: GpuFloat>(
    handle: &mut SolverHandle,
    a: &DeviceBuffer<T>,
    n: u32,
    lda: u32,
) -> SolverResult<(f64, f64)> {
    if n == 0 {
        return Ok((0.0, 1.0)); // log(det(I_0)) = 0, sign = 1.
    }
    validate_dimensions::<T>(a, n, lda)?;

    // Make a copy of A for the LU factorization (det is non-destructive).
    let buf_size = n as usize * lda as usize;
    let mut a_copy = DeviceBuffer::<T>::zeroed(buf_size)?;
    copy_buffer(a, &mut a_copy, buf_size)?;

    // LU factorize the copy.
    let mut pivots = DeviceBuffer::<i32>::zeroed(n as usize)?;
    let lu_result = lu_factorize(handle, &mut a_copy, n, lda, &mut pivots)?;

    if lu_result.info > 0 {
        // Matrix is singular.
        return Ok((f64::NEG_INFINITY, 0.0));
    }

    // Compute det = product of diagonal of U, adjusted for pivot sign.
    // Read the diagonal elements from the LU-factored matrix.
    let diagonal = read_lu_diagonal::<T>(&a_copy, n, lda)?;

    // Count the number of row swaps to determine the sign from pivoting.
    let pivot_sign = count_pivot_sign(&pivots, n)?;

    // Compute log|det| = sum of log|U[i,i]|, and track the sign.
    let mut log_abs_det = 0.0_f64;
    let mut det_sign = pivot_sign;

    for &d_val in &diagonal {
        let val = to_f64_val(d_val);
        if val.abs() < f64::EPSILON * 1e-10 {
            // Effectively zero diagonal element => singular.
            return Ok((f64::NEG_INFINITY, 0.0));
        }
        if val < 0.0 {
            det_sign = -det_sign;
        }
        log_abs_det += val.abs().ln();
    }

    Ok((log_abs_det, det_sign))
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

/// Validates matrix dimensions for determinant operations.
fn validate_dimensions<T: GpuFloat>(a: &DeviceBuffer<T>, n: u32, lda: u32) -> SolverResult<()> {
    if lda < n {
        return Err(SolverError::DimensionMismatch(format!(
            "determinant: lda ({lda}) must be >= n ({n})"
        )));
    }
    let required = n as usize * lda as usize;
    if a.len() < required {
        return Err(SolverError::DimensionMismatch(format!(
            "determinant: buffer too small ({} < {required})",
            a.len()
        )));
    }
    Ok(())
}

/// Copies `count` elements from `src` to `dst`.
fn copy_buffer<T: GpuFloat>(
    src: &DeviceBuffer<T>,
    dst: &mut DeviceBuffer<T>,
    count: usize,
) -> SolverResult<()> {
    dst.copy_from_device(src)
        .map_err(|e| SolverError::InternalError(format!("copy_buffer: {e}")))?;
    let _ = count;
    Ok(())
}

/// Reads the diagonal elements of the LU-factored matrix.
///
/// Returns a Vec of the diagonal values `U[i,i]` for i = 0..n.
/// The matrix is stored column-major with leading dimension `lda`.
fn read_lu_diagonal<T: GpuFloat>(a: &DeviceBuffer<T>, n: u32, lda: u32) -> SolverResult<Vec<T>> {
    let n = n as usize;
    let lda = lda as usize;
    let total = n * lda;

    // Read the full LU matrix to host.
    let mut host = vec![T::gpu_zero(); total];
    a.copy_to_host(&mut host)
        .map_err(|e| SolverError::InternalError(format!("read_lu_diagonal: {e}")))?;

    // Extract diagonal: column-major element (i, i) is at index i * lda + i.
    Ok((0..n).map(|i| host[i * lda + i]).collect())
}

/// Counts the parity of row swaps in the pivot array (LAPACK 1-based convention).
///
/// Returns +1.0 if the number of swaps is even, -1.0 if odd.
fn count_pivot_sign(pivots: &DeviceBuffer<i32>, n: u32) -> SolverResult<f64> {
    let n = n as usize;
    let mut host = vec![0_i32; n];
    pivots
        .copy_to_host(&mut host)
        .map_err(|e| SolverError::InternalError(format!("count_pivot_sign: {e}")))?;

    // pivots[i] is 1-based; if pivots[i] != i+1, a swap occurred.
    let swaps = host
        .iter()
        .enumerate()
        .filter(|&(i, &p)| p != (i as i32 + 1))
        .count();
    Ok(if swaps % 2 == 0 { 1.0 } else { -1.0 })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    #[test]
    fn det_zero_dimension() {
        // det of 0x0 matrix is 1 by convention.
        // Cannot call the actual function without GPU, but verify the logic.
        let det_0x0 = 1.0_f64;
        assert!((det_0x0 - 1.0).abs() < 1e-15);
    }

    #[test]
    fn log_det_zero_dimension() {
        let (log_abs, sign) = (0.0_f64, 1.0_f64);
        assert!((log_abs).abs() < 1e-15);
        assert!((sign - 1.0).abs() < 1e-15);
    }

    #[test]
    fn det_overflow_guard() {
        // log_abs > 709 should give infinity.
        let log_abs = 800.0_f64;
        let sign = 1.0_f64;
        let det = sign * f64::INFINITY;
        assert!(det.is_infinite());
        assert!(det > 0.0);
        let _ = log_abs;
    }

    #[test]
    fn det_underflow_guard() {
        // log_abs < -745 should give 0.
        let log_abs = -800.0_f64;
        let result = if log_abs < -745.0 { 0.0 } else { log_abs.exp() };
        assert!((result).abs() < 1e-15);
    }

    #[test]
    fn det_sign_tracking() {
        // With two negative diagonals, sign should be positive.
        let values = [-2.0_f64, -3.0, 1.0];
        let mut sign = 1.0_f64;
        for &v in &values {
            if v < 0.0 {
                sign = -sign;
            }
        }
        assert!((sign - 1.0).abs() < 1e-15, "two negatives => positive");
    }

    #[test]
    fn det_singular_zero() {
        // A zero diagonal element means the matrix is singular.
        let val = 0.0_f64;
        assert!(val.abs() < f64::EPSILON);
    }
}
