import numpy as np
from vitalDSP.preprocess.preprocess_operations import preprocess_signal


def fft_based_rr(signal, sampling_rate, preprocess=None, **preprocess_kwargs):
    """
    Estimate respiratory rate using the FFT (Fast Fourier Transform) method.

    Parameters
    ----------
    signal : numpy.ndarray
        The input respiratory signal.
    sampling_rate : float
        The sampling rate of the signal in Hz.
    preprocess : str, optional
        The preprocessing method to apply before estimation (e.g., "bandpass", "wavelet").
    preprocess_kwargs : dict, optional
        Additional arguments for the preprocessing function.

    Returns
    -------
    rr : float
        Estimated respiratory rate in breaths per minute.

    Examples
    --------
    >>> signal = np.sin(2 * np.pi * 0.2 * np.arange(0, 10, 0.01))
    >>> rr = fft_based_rr(signal, sampling_rate=100, preprocess='bandpass', lowcut=0.1, highcut=0.5)
    >>> print(rr)
    """
    if preprocess:
        signal = preprocess_signal(
            signal, sampling_rate, filter_type=preprocess, **preprocess_kwargs
        )

    # n = len(signal)
    # freq = np.fft.fftfreq(n, d=1 / sampling_rate)
    # fft_spectrum = np.fft.fft(signal)

    # peak_freq = freq[np.argmax(np.abs(fft_spectrum))]
    # Perform FFT on the signal
    fft_result = np.fft.fft(signal)
    freqs = np.fft.fftfreq(len(signal), d=1 / sampling_rate)

    # Consider only positive frequencies
    positive_freqs = freqs[freqs > 0]
    positive_fft = np.abs(fft_result[freqs > 0])

    # Find the peak frequency in the FFT spectrum
    peak_freq = positive_freqs[np.argmax(positive_fft)]

    # # Convert peak frequency from Hz to BPM
    # rr_bpm = peak_freq * 60  # Conversion from Hz to BPM

    return np.abs(peak_freq) * 60
