/*
 * 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_INTEGRAL_PLAN_API_H
#define DENSITY_FITTED_INTEGRAL_PLAN_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 Build a density-fitted integral plan (DF) for atomic orbitals.
 * @ingroup DensityFittingIntegralPlan
 *
 * Constructs all needed metric arrays and three-center integrals. All handles and buffers must be valid.
 * Workspace buffers must be preallocated using sizes from workspace query.
 *
 * The \c cuestDFIntPlan_t may be configured to treat range-separated Coulomb operators.
 *
 * \f[
 *   \underbrace{\frac{1}{r}}_{\text{full-range}}
 *   =
 *   \underbrace{\frac{\mathrm{erfc}(\omega r)}{r}}_{\text{short-range}}
 *   +
 *   \underbrace{\frac{\mathrm{erf}(\omega r)}{r}}_{\text{long-range}}
 * \f]
 *
 * In cuEST, range-separation is parameterized in terms of the full-range and long-range components. 
 *
 * - \c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION: Controls the fraction of full-range exchange included.
 * - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_FRACTION: Controls the fraction of long-range exchange included.
 * - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_OMEGA: Specifies the range separation parameter.
 *
 * It is required that the operator specification must be positive and respect the following conditions:
 *
 * - \c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION \f$\ge 0\f$.
 * - \c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION + \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_FRACTION \f$\ge 0\f$.
 * - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_OMEGA \f$\ge 0\f$.
 *
 * \c CUEST_STATUS_INVALID_ARGUMENT will be returned if these conditions are not respected during handle creation. 
 *
 * The operator specification is respected by all subsequent calls involving the \c cuestDFIntPlan_t. The one
 * exception is \c cuestDFCoulombCompute, which is always evaluated using the full-range Coulomb operator.
 *
 * Example configurations:
 *
 * - PBE0 (full-range, only):
 *   - \c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION \f$= 0.25\f$.
 *   - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_FRACTION \f$= 0\f$.
 *   - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_OMEGA unused.
 *
 * - \f$\omega\f$B97 (long-range, only):
 *   - \c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION \f$= 0.0\f$.
 *   - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_FRACTION \f$= 1.0\f$.
 *   - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_OMEGA \f$= 0.4\f$.
 *
 * - \f$\omega\f$B97M-V (full- and long-range):
 *   - \c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION \f$= 0.15\f$.
 *   - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_FRACTION \f$= 0.85\f$.
 *   - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_OMEGA \f$= 0.3\f$.
 *
 * - HSE06 (short-range, only):
 *   - \c CUEST_DFINTPLAN_PARAMETERS_EXCHANGE_FRACTION \f$= 0.25\f$.
 *   - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_FRACTION \f$= -0.25\f$.
 *   - \c CUEST_DFINTPLAN_PARAMETERS_LRC_EXCHANGE_OMEGA \f$= 0.11\f$.
 *
 * It is possible to put or create the \c cuestDFIntPlan_t in a state where it is no longer possible to use 
 * the \c cuestDFIntPlan_t for certain calculations.
 *
 * - Creating the \c cuestDFIntPlan_t with the \c CUEST_DFINTPLAN_PARAMETERS_THREE_INDEX_INTEGRAL_DIRECT parameter
 *   set to a non-zero value allows the plan to be used in calls to \c cuestDFMOIntegralsCompute. At present, no
 *   other functions support this configuration.
 * - Calls to \c cuestDFSymmetricDerivativeCompute using the \c CUEST_DFSYMMETRICDERIVATIVECOMPUTE_MEMORY_POLICY_OVERWRITE
 *   memory policy place the \c cuestDFIntPlan_t in a dirty state. No further compute calls involving this plan will
 *   succeed. 
 *
 * \c CUEST_STATUS_INVALID_PLAN will be returned if a compute call is made with a \c cuestDFIntPlan_t in an
 * unusable state.
 *
 * \verbatim embed:rst
 .. Note::
    - The number of atoms in the primary basis handle must match the number of atoms in the
      auxiliary basis handle.
    - The `cuestAOBasis_t` primary basis handle provided here must correspond to the basis handle provided
      when the `cuestAOPairList_t` pair list handle was created.
    - It is the user's responsibility not to destroy the `cuestAOBasis_t` primary basis handle,
      auxiliary basis handle or `cuestAOPairList_t` pair list handle until operations involving the 
      `cuestDFIntPlan_t` plan handle are complete.
    - After the `cuestAOBasis_t` basis handle, auxiliary basis handle or `cuestAOPairList_t` pair list handle are destroyed, the `cuestDFIntPlan_t` plan handle is no longer valid.
    - Screening of negligible integrals computed while using this `cuestDFIntPlan_t` plan handle 
      is controlled by `thresholdPQ` set during construction of the `cuestAOPairList_t` pair list handle.
 \endverbatim
 *
 * @param[in]  handle                cuEST handle. Must not be NULL.
 * @param[in]  primaryBasis          AO basis set for primary orbitals. Must not be NULL.
 * @param[in]  auxiliaryBasis        AO basis set for auxiliary/fitting orbitals. 
 *                                       The auxiliary basis set must use a pure (spherical) 
 *                                       angular momentum representation. Must not be NULL.
 * @param[in]  pairList              AO pair list describing sparsity. Must not be NULL.
 * @param[in]  parameters            Density-fitting parameters structure. Must not be NULL.
 * @param[in]  persistentWorkspace   Workspace for persistent buffers. Must not be NULL.
 * @param[in]  temporaryWorkspace    Workspace for temporary buffers. Must not be NULL.
 * @param[out] outPlan               Output pointer for DF plan handle. 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_ARGUMENT if any of the \c cuestDFIntPlanParameters_t are invalid;
 *   - \c CUEST_STATUS_EXCEPTION if \c auxiliaryBasis is not pure;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestDFIntPlanCreate(
    cuestHandle_t handle,
    const cuestAOBasis_t primaryBasis,
    const cuestAOBasis_t auxiliaryBasis,
    const cuestAOPairList_t pairList,
    const cuestDFIntPlanParameters_t parameters, 
    cuestWorkspace_t* persistentWorkspace,
    cuestWorkspace_t* temporaryWorkspace,
    cuestDFIntPlan_t* outPlan);

/**
 * @brief Query workspace sizes for creating a density-fitted integral plan.
 * @ingroup DensityFittingIntegralPlan
 *
 * Simulates all allocations for DF plan, returning size requirements.
 * All handles and pointers must be valid (not NULL). Output descriptors are filled on success.
 *
 * @param[in]  handle                             cuEST handle. Must not be NULL.
 * @param[in]  primaryBasis                       AO basis set for primary orbitals. Must not be NULL.
 * @param[in]  auxiliaryBasis                     AO basis set for auxiliary/fitting orbitals. 
 *                                                    The auxiliary basis set must use a pure (spherical) 
 *                                                    angular momentum representation. Must not be NULL.
 * @param[in]  pairList                           AO pair list handle. Must not be NULL.
 * @param[in]  parameters                         Density-fitting parameters structure. Must not be NULL.
 * @param[out] persistentWorkspaceDescriptor      Output descriptor for persistent buffers. Must not be NULL.
 * @param[out] temporaryWorkspaceDescriptor       Output descriptor for temporary buffers. Must not be NULL.
 * @param      outPlan                            Output pointer for DF plan handle. 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 any of the \c cuestDFIntPlanParameters_t are invalid;
 *   - \c CUEST_STATUS_EXCEPTION if \c auxiliaryBasis is not pure;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestDFIntPlanCreateWorkspaceQuery(
    cuestHandle_t handle,
    const cuestAOBasis_t primaryBasis,
    const cuestAOBasis_t auxiliaryBasis,
    const cuestAOPairList_t pairList,
    const cuestDFIntPlanParameters_t parameters, 
    cuestWorkspaceDescriptor_t* persistentWorkspaceDescriptor,
    cuestWorkspaceDescriptor_t* temporaryWorkspaceDescriptor,
    cuestDFIntPlan_t* outPlan);

/**
 * @brief Destroy/free a density-fitted integral plan and all resources.
 * @ingroup DensityFittingIntegralPlan
 *
 * The plan handle and its memory must not be used after destruction.
 *
 * @param[in] plan      DF plan handle to destroy. Must not be NULL.
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success;
 *   - \c CUEST_STATUS_NULL_POINTER if \c plan is NULL;
 *   - \c CUEST_STATUS_INVALID_TYPE if opaque handles are not the correct type;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestDFIntPlanDestroy(
    cuestDFIntPlan_t plan);

#ifdef __cplusplus
}
#endif

#endif /* DENSITY_FITTED_INTEGRAL_PLAN_API_H */
