/*
 * Copyright 2025-2026 NVIDIA CORPORATION & AFFILIATES.  All rights reserved.
 *
 * NOTICE TO LICENSEE:
 *
 * This source code and/or documentation ("Licensed Deliverables") are
 * subject to NVIDIA intellectual property rights under U.S. and
 * international Copyright laws.
 *
 * These Licensed Deliverables contained herein is PROPRIETARY and
 * CONFIDENTIAL to NVIDIA and is being provided under the terms and
 * conditions of a form of NVIDIA software license agreement by and
 * between NVIDIA and Licensee ("License Agreement") or electronically
 * accepted by Licensee.  Notwithstanding any terms or conditions to
 * the contrary in the License Agreement, reproduction or disclosure
 * of the Licensed Deliverables to any third party without the express
 * written consent of NVIDIA is prohibited.
 *
 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
 * LICENSE AGREEMENT, NVIDIA MAKES NO REPRESENTATION ABOUT THE
 * SUITABILITY OF THESE LICENSED DELIVERABLES FOR ANY PURPOSE.  IT IS
 * PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY OF ANY KIND.
 * NVIDIA DISCLAIMS ALL WARRANTIES WITH REGARD TO THESE LICENSED
 * DELIVERABLES, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY,
 * NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE.
 * NOTWITHSTANDING ANY TERMS OR CONDITIONS TO THE CONTRARY IN THE
 * LICENSE AGREEMENT, IN NO EVENT SHALL NVIDIA BE LIABLE FOR ANY
 * SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, OR ANY
 * DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
 * OF THESE LICENSED DELIVERABLES.
 *
 * U.S. Government End Users.  These Licensed Deliverables are a
 * "commercial item" as that term is defined at 48 C.F.R. 2.101 (OCT
 * 1995), consisting of "commercial computer software" and "commercial
 * computer software documentation" as such terms are used in 48
 * C.F.R. 12.212 (SEPT 1995) and is provided to the U.S. Government
 * only as a commercial end item.  Consistent with 48 C.F.R.12.212 and
 * 48 C.F.R. 227.7202-1 through 227.7202-4 (JUNE 1995), all
 * U.S. Government End Users acquire the Licensed Deliverables with
 * only those rights set forth herein.
 *
 * Any use of the Licensed Deliverables in individual and commercial
 * software must include, in the user documentation and internal
 * comments to the code, the above Disclaimer and U.S. Government End
 * Users Notice.
 */

#ifndef POTENTIAL_INTEGRAL_DERIVATIVE_COMPUTE_API_H
#define POTENTIAL_INTEGRAL_DERIVATIVE_COMPUTE_API_H

#include <stddef.h>

#include "../macros.h"
#include "../types/cuest_types.h"
#include "../types/cuest_parameter_types.h"
#include "../types/workspace_api.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Compute the potential integral derivatives with respect to atomic and charge coordinates,
 *        contracted with a density matrix.
 * @ingroup PotentialGrad
 *
 * This routine evaluates the derivative of the potential integrals and contracts the result with
 * a density matrix in the AO basis. The resulting gradient is returned both with
 * respect to the AO basis functions and with respect to the point-charge
 * coordinates. All required temporary workspace must be sized and allocated
 * using the corresponding workspace query function.
 *
 * The user must provide:
 * - A preallocated/populated density matrix of size (nao × nao).
 * - A list of point charges and their Cartesian coordinates.
 * - Preallocated output buffers for the AO basis gradients and charge gradients (to be overwritten).
 *
 * \b Kernel and precision selection. This routine can use JIT-compiled kernels and/or reduced-precision
 * "ffloat" compute, selected through the compute parameters:
 *   - \c CUEST_POTENTIALDERIVATIVECOMPUTE_PARAMETERS_JIT_USAGE_MODE (\c cuestJITUsageMode_t): \c CUEST_JIT_USAGE_MODE_ON
 *     uses JIT-compiled kernels, \c CUEST_JIT_USAGE_MODE_OFF uses AOT-compiled kernels. JIT-compiled kernels
 *     are only available in the cuEST CUDA 13 package; the default is \c CUEST_JIT_USAGE_MODE_ON in the
 *     cuEST CUDA 13 package and \c CUEST_JIT_USAGE_MODE_OFF in the cuEST CUDA 12 package. Requesting
 *     \c CUEST_JIT_USAGE_MODE_ON with the cuEST CUDA 12 package returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *   - \c CUEST_POTENTIALDERIVATIVECOMPUTE_PARAMETERS_FFLOAT_USAGE_MODE (\c cuestFfloatUsageMode_t) selects the compute precision:
 *     \c CUEST_FFLOAT_USAGE_MODE_ON requests ffloat compute, \c CUEST_FFLOAT_USAGE_MODE_OFF requests fp64
 *     compute, and \c CUEST_FFLOAT_USAGE_MODE_DEFAULT requests ffloat compute when it is expected to provide
 *     a performance advantage on the present GPU architecture.
 *   - ffloat compute is only compatible with JIT-compiled kernels. Requesting ffloat compute while JIT is
 *     disabled (\c CUEST_FFLOAT_USAGE_MODE_ON together with \c CUEST_JIT_USAGE_MODE_OFF) is not supported and
 *     returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *   - It is recommended to leave \c CUEST_FFLOAT_USAGE_MODE_OFF for general use of the potential derivative.
 *
 * @param[in]  handle             cuEST handle. Must not be NULL.
 * @param[in]  plan               One-electron integral computation plan (opaque handle). Must not be NULL.
 * @param[in]  parameters         Compute parameters (\c cuestPotentialDerivativeComputeParameters_t). Must not be NULL
 * @param[in]  temporaryWorkspace Temporary workspace buffers (preallocated for this operation). Must not be NULL.
 * @param[in]  numCharges         Number of point charges. Must be greater than zero.
 * @param[in]  xyz                Cartesian coordinates of the point charges
 *                                    (size: \c numCharges × 3, in row-major order) on GPU. Must not be NULL.
 * @param[in]  q                  Point charges (size: \c numCharges) on GPU. Must not be NULL.
 * @param[in]  densityMatrix      Input density matrix (size: nao × nao) on GPU. Must not be NULL.
 * @param[out] outBasisGradient   Output gradient with respect to AO basis centers
 *                                    (size: natom × 3) on GPU. Must not be NULL.
 *                                    Elements of this array are overwritten by the calculation results.
 * @param[out] outChargeGradient  Output gradient with respect to the point-charge coordinates
 *                                    (size: \c numCharges × 3) on GPU. Must not be NULL.
 *                                    Elements of this array are overwritten by the calculation results.
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success;
 *   - \c CUEST_STATUS_INVALID_HANDLE if the cuEST handle is NULL;
 *   - \c CUEST_STATUS_NULL_POINTER if any required pointer is NULL;
 *   - \c CUEST_STATUS_INVALID_TYPE if opaque handles are not the correct type;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numCharges is zero;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if a parameter value is invalid, if JIT-compiled kernels are requested
 *     with the cuEST CUDA 12 package, or if ffloat compute is requested while JIT is disabled (incompatible
 *     JIT/ffloat combination);
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestPotentialDerivativeCompute(
    cuestHandle_t handle,
    const cuestOEIntPlan_t plan,
    const cuestPotentialDerivativeComputeParameters_t parameters,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numCharges,
    const double* xyz,
    const double* q,
    const double* densityMatrix,
    double* outBasisGradient,
    double* outChargeGradient);

/**
 * @brief Query the temporary workspace required to compute the potential integral
 *        derivatives with respect to atomic and charge coordinates,
 *        contracted with a density matrix.
 * @ingroup PotentialGrad
 *
 * This function determines the workspace required for a potential derivative
 * evaluation with a given plan and point-charge specification. The output
 * descriptor is filled on success and can be used to allocate host and device
 * workspace buffers prior to calling \c cuestPotentialDerivativeCompute.
 *
 * The density matrix and output gradient pointers are optional and may be NULL.
 *
 * \b Kernel and precision selection. This routine can use JIT-compiled kernels and/or reduced-precision
 * "ffloat" compute, selected through the compute parameters:
 *   - \c CUEST_POTENTIALDERIVATIVECOMPUTE_PARAMETERS_JIT_USAGE_MODE (\c cuestJITUsageMode_t): \c CUEST_JIT_USAGE_MODE_ON
 *     uses JIT-compiled kernels, \c CUEST_JIT_USAGE_MODE_OFF uses AOT-compiled kernels. JIT-compiled kernels
 *     are only available in the cuEST CUDA 13 package; the default is \c CUEST_JIT_USAGE_MODE_ON in the
 *     cuEST CUDA 13 package and \c CUEST_JIT_USAGE_MODE_OFF in the cuEST CUDA 12 package. Requesting
 *     \c CUEST_JIT_USAGE_MODE_ON with the cuEST CUDA 12 package returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *   - \c CUEST_POTENTIALDERIVATIVECOMPUTE_PARAMETERS_FFLOAT_USAGE_MODE (\c cuestFfloatUsageMode_t) selects the compute precision:
 *     \c CUEST_FFLOAT_USAGE_MODE_ON requests ffloat compute, \c CUEST_FFLOAT_USAGE_MODE_OFF requests fp64
 *     compute, and \c CUEST_FFLOAT_USAGE_MODE_DEFAULT requests ffloat compute when it is expected to provide
 *     a performance advantage on the present GPU architecture.
 *   - ffloat compute is only compatible with JIT-compiled kernels. Requesting ffloat compute while JIT is
 *     disabled (\c CUEST_FFLOAT_USAGE_MODE_ON together with \c CUEST_JIT_USAGE_MODE_OFF) is not supported and
 *     returns \c CUEST_STATUS_INVALID_ARGUMENT.
 *   - It is recommended to leave \c CUEST_FFLOAT_USAGE_MODE_OFF for general use of the potential derivative.
 *
 * @param[in]  handle                            cuEST handle. Must not be NULL.
 * @param[in]  plan                              One-electron integral computation plan (opaque handle). Must not be NULL.
 * @param[in]  parameters                        Compute parameters (\c cuestPotentialDerivativeComputeParameters_t). Must not be NULL
 * @param[out] temporaryWorkspaceDescriptor      Output descriptor for temporary workspace sizes (host and device). Must not be NULL.
 * @param[in]  numCharges                        Number of point charges. Must be greater than zero.
 * @param      xyz                               Optional Cartesian coordinates of the point charges. May be NULL.
 * @param      q                                 Optional point charges. May be NULL.
 * @param      densityMatrix                     Optional density matrix. May be NULL.
 * @param      outBasisGradient                  Optional output gradient buffer with respect to AO basis centers. May be NULL.
 * @param      outChargeGradient                 Optional output gradient buffer with respect to the point-charge coordinates. May be NULL.
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success;
 *   - \c CUEST_STATUS_INVALID_HANDLE if the cuEST handle is NULL;
 *   - \c CUEST_STATUS_NULL_POINTER if any required pointer is NULL;
 *   - \c CUEST_STATUS_INVALID_TYPE if opaque handles are not the correct type;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if \c numCharges is zero;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if a parameter value is invalid, if JIT-compiled kernels are requested
 *     with the cuEST CUDA 12 package, or if ffloat compute is requested while JIT is disabled (incompatible
 *     JIT/ffloat combination);
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestPotentialDerivativeComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestOEIntPlan_t plan,
    const cuestPotentialDerivativeComputeParameters_t parameters,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numCharges,
    const double* xyz,
    const double* q,
    const double* densityMatrix,
    double* outBasisGradient,
    double* outChargeGradient);

#ifdef __cplusplus
}
#endif

#endif /* POTENTIAL_INTEGRAL_DERIVATIVE_COMPUTE_API_H */
