/*
 * 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_COMPUTE_API_H
#define POTENTIAL_INTEGRAL_COMPUTE_API_H

#include <stddef.h>
#include <stdint.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 atomic orbital nuclear attraction (potential energy) matrix (V) for given point charges.
 * @ingroup Potential
 *
 * This function computes the AO potential energy matrix for a set of point charges (typically nuclei) as:
 *    \f[ V_{pq} = \sum_{A} q_A * \left< p \left| \frac{1}{|r - R_A|} \right| q \right> \f]
 * All input arrays must be preallocated and valid.
 *   - The \c xyz array has size \c numCharges × 3 (row major order: \f$ [x_0, y_0, z_0, x_1, y_1, z_1, ...] \f$ ).
 *   - The \c q array has length \c numCharges.
 *   - The output buffer \c outVMatrix must be preallocated for (nao × nao) doubles.
 * Temporary workspace must be preallocated using the requirements from \c cuestPotentialComputeWorkspaceQuery.
 *
 * Note:
 *   - The potential is evaluated with a positive unit test charge. To obtain
 *     nuclear attraction integrals, the \c q array could have negative entries. Alternatively, the \c q array
 *     could have positive entries and the result scaled by the charge of an electron (i.e. -1 in atomic units).
 *
 * \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_POTENTIALCOMPUTE_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_POTENTIALCOMPUTE_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.
 *
 * @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 cuestPotentialComputeParameters_t). Must not be NULL
 * @param[in]  temporaryWorkspace  Temporary workspace buffers, preallocated as required. Must not be NULL.
 * @param[in]  numCharges          Number of point charges (must be greater than zero).
 * @param[in]  xyz                 Array of charge coordinates (size: \c numCharges × 3) in atomic units on GPU. Must not be NULL.
 * @param[in]  q                   Array of charge magnitudes (size: \c numCharges) on GPU. Must not be NULL.
 * @param[out] outVMatrix          Output AO potential matrix buffer (size: nao × nao) 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 cuestPotentialCompute(
    cuestHandle_t handle,
    const cuestOEIntPlan_t plan,
    const cuestPotentialComputeParameters_t parameters,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numCharges,
    const double* xyz,
    const double* q,
    double* outVMatrix);

/**
 * @brief Query the temporary workspace required (in bytes) for a potential energy matrix calculation.
 * @ingroup Potential
 *
 * This function calculates the workspace required for a potential energy matrix evaluation,
 * returning host/device workspace buffer sizes in the output descriptor. The user arrays \c q, \c xyz, and \c outVMatrix may be NULL;
 * only sizes are computed. \c numCharges must be greater than zero.
 *
 * \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_POTENTIALCOMPUTE_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_POTENTIALCOMPUTE_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.
 *
 * @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 cuestPotentialComputeParameters_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 array of charge coordinates (may be NULL).
 * @param      q                                   Optional array of charge magnitudes (may be NULL).
 * @param      outVMatrix                          Optional output AO potential matrix buffer (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 cuestPotentialComputeWorkspaceQuery(
    cuestHandle_t handle,
    const cuestOEIntPlan_t plan,
    const cuestPotentialComputeParameters_t parameters,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numCharges,
    const double* xyz,
    const double* q,
    double* outVMatrix);


#ifdef __cplusplus
}
#endif

#endif /* POTENTIAL_INTEGRAL_COMPUTE_API_H */
