/*
 * 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 DENSITY_FITTED_MO_INTEGRALS_COMPUTE_API_H
#define DENSITY_FITTED_MO_INTEGRALS_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 Transforms the 3-index density fitting tensor from the AO basis to the MO basis.
 * @ingroup DFMOIntegrals
 * @since v0.2.0
 *
 * This routine transforms three-index AO integrals into the MO basis within the density 
 * fitting representation. Pairs of coefficient matrices are taken as input.
 * 
 * Starting from a density-fitted representation of the AO integrals:
 * 
 * \f[ 
 * \left[ f_{\rm HF} (\mu\nu|\frac{1}{r}|\lambda\sigma) + f_{\rm LRC} (\mu\nu|\frac{{\rm erf}(\omega r)}{r}|\lambda\sigma) \right]
 *     = \sum_{A} B^{A}_{\mu\nu} B^{A}_{\lambda\sigma}
 * \f]
 *
 * This function performs the transformation:
 * 
 * \f[ B^{A}_{ia} = \sum_{\mu\nu} B^{A}_{\mu\nu} C(L)_{i\mu} C(R)_{a\nu} \f]
 * 
 * The resulting tensors can be used to directly construct 4-index MO integrals by contracting over the auxiliary index.
 * 
 * \f[ 
 * \left[ f_{\rm HF} (ia|\frac{1}{r}|jb) + f_{\rm LRC} (ia|\frac{{\rm erf}(\omega r)}{r}|jb) \right]
 *     = \sum_{A} B^{A}_{ia} B^{A}_{jb}
 * \f]
 * 
 * The transformation is most efficient when the MO dimension of C(L) is
 * smaller than the MO dimension of C(R), as C(L) is applied first. The
 * resulting tensor is transformed by the fitting metric.
 *
 * Multiple MO integral tensors can be evaluated in a single function call.
 * The resulting MO integral tensors respect the values of 
 * \c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION,
 * \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_FRACTION, and
 * \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_OMEGA set in the \c cuestDFIntPlan_t.
 *
 * This function supports the use of a \c cuestDFIntPlan_t constructed with the
 * CUEST_DFINTPLAN_PARAMETERS_THREE_INDEX_INTEGRAL_DIRECT parameter set to either
 * 0 or 1.
 *
 * All required temporary workspace must be sized using the corresponding workspace
 * query function. A \c variableBufferSize descriptor must also be provided to
 * constrain the memory usage.
 *
 * @param[in]  handle                       cuEST handle. Must not be NULL.
 * @param[in]  plan                         DF integral computation plan (opaque handle). Must not be NULL.
 * @param[in]  parameters                   Compute parameters (\c cuestDFMOIntegralsComputeParameters_t). Must not be NULL
 * @param[in]  variableBufferSize           The variableBufferSize determines the size of an internal scratch buffer
 *                                              used for certain transformations. A buffer of 2GB is a reasonable default value.
 *                                              Host memory is currently unused. Note that the variableBufferSize does not limit total
 *                                              memory usage. The value of the variableBufferSize must not change between WorkspaceQuery
 *                                              and Compute calls.
 * @param[in]  temporaryWorkspace           Temporary workspace buffers (preallocated for
 *                                              this operation using the query function).
 *                                              Must not be NULL.
 * @param[in]  numCoefficientMatrices       Number of pairs of coefficient matrices. Must be > 0.
 * @param[in]  numLeftOrbitals              Array of molecular orbital counts for each left
 *                                              coefficient matrix (size: \c numCoefficientMatrices) on CPU.
 *                                              Each entry must be > 0. Must not be NULL.
 * @param[in]  numRightOrbitals             Array of molecular orbital counts for each right
 *                                              coefficient matrix (size: \c numCoefficientMatrices) on CPU.
 *                                              Each entry must be > 0. Must not be NULL.
 * @param[in]  leftCoefficientMatrices      Concatenated left coefficient matrices. Each matrix
 *                                              has shape [\c numLeftOrbitals[i], nao] on GPU. Total size:
 *                                              sum(\c numLeftOrbitals[i]) × nao. Must not be NULL.
 * @param[in]  rightCoefficientMatrices     Concatenated right coefficient matrices. Each matrix
 *                                              has shape [\c numRightOrbitals[i], nao] on GPU. Total size:
 *                                              sum(\c numRightOrbitals[i]) × nao. Must not be NULL.
 * @param[out] outTensors                   Output concatenated MO integral tensors. Each tensor has shape
 *                                              [naux, \c numLeftOrbitals[i], \c numRightOrbitals[i]] on GPU.
 *                                              Total size: naux × sum(\c numRightOrbitals[i] × \c numLeftOrbitals[i]).
 *                                              Must not 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_PLAN if the \c cuestDFIntPlan_t is not correctly configured or in a dirty state;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT
 *             if \c numCoefficientMatrices == 0,
 *             or any \c numLeftOrbitals[i] == 0,
 *             or any \c numRightOrbitals[i] == 0;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 *
 */
CUEST_API
cuestStatus_t cuestDFMOIntegralsCompute(
    cuestHandle_t handle,
    cuestDFIntPlan_t plan,
    const cuestDFMOIntegralsComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspace_t* temporaryWorkspace,
    uint64_t numCoefficientMatrices,
    const uint64_t* numLeftOrbitals,
    const uint64_t* numRightOrbitals,
    const double* leftCoefficientMatrices,
    const double* rightCoefficientMatrices,
    double* outTensors);

/**
 * @brief Query the temporary workspace required to transform the 3-index density 
 *     fitting tensor from the AO basis to the MO basis.
 * @ingroup DFMOIntegrals
 * @since v0.2.0
 *
 * This function determines the workspace required to perform an AO to MO transform
 * of the DF 3-index tensor with the given plan, coefficient matrices, and maximum
 * workspace constraint. The output descriptor is filled on success and can be used
 * to allocate host and device workspace buffers prior to calling
 * \c cuestDFMOIntegralsCompute.
 *
 * Some input pointers (\c coefficientMatrices and \c outTensors) are optional
 * and may be NULL.
 *
 * @param[in]  handle                       cuEST handle. Must not be NULL.
 * @param[in]  plan                         DF integral computation plan (opaque handle). Must not be NULL.
 * @param[in]  parameters                   Compute parameters (\c cuestDFMOIntegralsComputeParameters_t). Must not be NULL
 * @param[in]  variableBufferSize           The variableBufferSize determines the size of an internal scratch buffer
 *                                              used for certain transformations. A buffer of 2GB is a reasonable default value.
 *                                              Host memory is currently unused. Note that the variableBufferSize does not limit total
 *                                              memory usage. The value of the variableBufferSize must not change between WorkspaceQuery
 *                                              and Compute calls.
 * @param[out] temporaryWorkspaceDescriptor Output descriptor for required temporary workspace
 *                                              sizes (host and device). Must not be NULL.
 * @param[in]  numCoefficientMatrices       Number of pairs of coefficient matrices. Must be > 0.
 * @param[in]  numLeftOrbitals              Array of molecular orbital counts for each left
 *                                              coefficient matrix (size: \c numCoefficientMatrices) on CPU.
 *                                              Each entry must be > 0. Must not be NULL.
 * @param[in]  numRightOrbitals             Array of molecular orbital counts for each right
 *                                              coefficient matrix (size: \c numCoefficientMatrices) on CPU.
 *                                              Each entry must be > 0. Must not be NULL.
 * @param      leftCoefficientMatrices      Concatenated left coefficient matrices. Each matrix
 *                                              has shape [\c numLeftOrbitals[i], nao] on GPU. Total size:
 *                                              sum(\c numLeftOrbitals[i]) × nao. May be NULL.
 * @param      rightCoefficientMatrices     Concatenated right coefficient matrices. Each matrix
 *                                              has shape [\c numRightOrbitals[i], nao] on GPU. Total size:
 *                                              sum(\c numRightOrbitals[i]) × nao. May be NULL.
 * @param      outTensors                   Output concatenated MO integral tensors. Each tensor has shape
 *                                              [naux, \c numLeftOrbitals[i], \c numRightOrbitals[i]] on GPU.
 *                                              Total size: naux × sum(\c numRightOrbitals[i] × \c numLeftOrbitals[i]).
 *                                              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_PLAN if the \c cuestDFIntPlan_t is not correctly configured or in a dirty state;
 *   - \c CUEST_STATUS_INVALID_ARGUMENT
 *             if \c numCoefficientMatrices == 0,
 *             or any \c numLeftOrbitals[i] == 0,
 *             or any \c numRightOrbitals[i] == 0;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestDFMOIntegralsComputeWorkspaceQuery(
    cuestHandle_t handle,
    cuestDFIntPlan_t plan,
    const cuestDFMOIntegralsComputeParameters_t parameters,
    const cuestWorkspaceDescriptor_t* variableBufferSize,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    uint64_t numCoefficientMatrices,
    const uint64_t* numLeftOrbitals,
    const uint64_t* numRightOrbitals,
    const double* leftCoefficientMatrices,
    const double* rightCoefficientMatrices,
    double* outTensors);

#ifdef __cplusplus
}
#endif

#endif /* DENSITY_FITTED_MO_INTEGRALS_COMPUTE_API_H */
