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

#include <stdint.h>

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

#ifdef __cplusplus
extern "C" {
#endif

/**
 * @brief Creates a new cuEST handle and its associated resources from a parameter structure.
 * @ingroup Context
 * 
 * Ownership of handles or pointers passed into the structure is NOT transferred.
 * Default parameters will construct a generic GPU context.
 * 
 * Requirements:
 *   - A cudaStream alone may be provided, all others may be left NULL.
 *   - If cuBLAS or cuSolver handles are specified, a cudaStream must also be specified.
 *   - When cuBLAS or cuSolver handles are specified, these handles must be set
 *     to operate on the provided cudaStream. (i.e. by calling \c cublasSetStream
 *     or \c cusolverDnSetStream with the provided cudaStream);
 *
 * @param[in]  parameters         Parameter structure handle (must not be NULL).
 * @param[out] handle             Pointer to opaque cuEST handle for output.
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success,
 *   - \c CUEST_STATUS_NULL_POINTER if \c parameters or \c handle is NULL,
 *   - \c CUEST_STATUS_INVALID_TYPE if \c parameters is not of the \c cuestHandleParameters_t type,
 *   - \c CUEST_STATUS_UNSUPPORTED_ARCHITECTURE if the GPU architecture is not supported by cuEST,
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if a handle parameter value is invalid (e.g. an invalid JIT compile-thread count or an unsafe JIT cache directory),
 *   - \c CUEST_STATUS_HOME_NOT_FOUND if no JIT cache directory was specified and the HOME environment variable is unset or empty,
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestCreate(
    cuestHandleParameters_t parameters,
    cuestHandle_t* handle);

/**
 * @brief Destroys a cuEST handle and all associated resources.
 * @ingroup Context
 *
 * Frees memory for GPU resources, quadrature data, solid harmonics coefficients, and rys table.
 *
 * @param[in] handle  Opaque cuEST handle to destroy (must not be NULL).
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success,
 *   - \c CUEST_STATUS_NULL_POINTER if handle is NULL,
 *   - \c CUEST_STATUS_INVALID_TYPE if handle is not of the \c cuestHandle_t type,
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestDestroy(
    cuestHandle_t handle);

/**
 * @brief Sets the math mode used when cuEST functions are called with this handle.
 * @ingroup Context
 *
 * @param[in,out] handle  Opaque cuEST handle (must not be NULL).
 * @param[in] mode    Math mode enum.
 *
 * - \c CUEST_DEFAULT_MATH_MODE cuEST will use algorithms that are determined heuristically
 *      to maximize performance. Some results may use mixed precision arithmetic.
 * - \c CUEST_NATIVE_FP64_MATH_MODE cuEST will use FP64 arithmetic everywhere.
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success,
 *   - \c CUEST_STATUS_INVALID_HANDLE if handle is NULL,
 *   - \c CUEST_STATUS_INVALID_TYPE if handle is not of the \c cuestHandle_t type,
 *   - \c CUEST_STATUS_INVALID_ARGUMENT if the \c cuestMathMode_t is invalid;
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestSetMathMode(
    cuestHandle_t handle, 
    cuestMathMode_t mode);

/**
 * @brief Gets the math mode associated with this handle.
 * @ingroup Context
 *
 * @param[in]  handle       Opaque cuEST handle (must not be NULL).
 * @param[out] mode         Math mode enum (must not be NULL).
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success,
 *   - \c CUEST_STATUS_INVALID_HANDLE if handle is NULL,
 *   - \c CUEST_STATUS_NULL_POINTER if mode is NULL,
 *   - \c CUEST_STATUS_INVALID_TYPE if handle is not of the \c cuestHandle_t type,
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestGetMathMode(
    cuestHandle_t handle, 
    cuestMathMode_t* mode);

/**
 * @brief Sets the compute capability used in heuristics to determine the most performant code path.
 * @ingroup Context
 *
 * @param[in,out] handle               Opaque cuEST handle (must not be NULL).
 * @param[in] targetMajorVersion   Compute capability target major version.
 * @param[in] targetMinorVersion   Compute capability target minor version.
 *
 * Notes: 
 *   - Manually setting the compute capability target is not recommended for production calculations.
 *     This option is intended only to compare numerical performance of the same algorithm on different
 *     architectures.
 *   - Not all algorithms may be available on all architectures. Changing the compute capability
 *     target may result in exceptions during subsequent calls to cuEST.
 *   - The compute capability target only affects heuristics. It does not affect kernel execution
 *     or JIT compilation.
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success,
 *   - \c CUEST_STATUS_INVALID_HANDLE if handle is NULL,
 *   - \c CUEST_STATUS_INVALID_TYPE if handle is not of the \c cuestHandle_t type,
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestSetComputeCapabilityTarget(
    cuestHandle_t handle, 
    uint32_t targetMajorVersion,
    uint32_t targetMinorVersion);

/**
 * @brief Gets the compute capability used in heuristics to determine the most performant code path.
 * @ingroup Context
 *
 * @param[in]  handle              Opaque cuEST handle (must not be NULL).
 * @param[out] targetMajorVersion  Compute capability target major version (must not be NULL). 
 * @param[out] targetMinorVersion  Compute capability target minor version (must not be NULL). 
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success,
 *   - \c CUEST_STATUS_INVALID_HANDLE if handle is NULL,
 *   - \c CUEST_STATUS_INVALID_TYPE if handle is not of the \c cuestHandle_t type,
 *   - \c CUEST_STATUS_NULL_POINTER if targetMajorVersion or targetMinorVersion is NULL,
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestGetComputeCapabilityTarget(
    cuestHandle_t handle, 
    uint32_t* targetMajorVersion,
    uint32_t* targetMinorVersion);

/**
 * @brief Gets the major version of the cuEST library.
 * @ingroup Context
 *
 * @param[in]  handle              Opaque cuEST handle (must not be NULL).
 * @param[out] majorVersion        Major version of the cuEST library (must not be NULL). 
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success,
 *   - \c CUEST_STATUS_INVALID_HANDLE if handle is NULL,
 *   - \c CUEST_STATUS_INVALID_TYPE if handle is not of the \c cuestHandle_t type,
 *   - \c CUEST_STATUS_NULL_POINTER if majorVersion is NULL,
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestGetMajorVersion(
    cuestHandle_t handle, 
    uint32_t* majorVersion);

/**
 * @brief Gets the minor version of the cuEST library.
 * @ingroup Context
 *
 * @param[in]  handle              Opaque cuEST handle (must not be NULL).
 * @param[out] minorVersion        Minor version of the cuEST library (must not be NULL). 
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success,
 *   - \c CUEST_STATUS_INVALID_HANDLE if handle is NULL,
 *   - \c CUEST_STATUS_INVALID_TYPE if handle is not of the \c cuestHandle_t type,
 *   - \c CUEST_STATUS_NULL_POINTER if minorVersion is NULL,
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestGetMinorVersion(
    cuestHandle_t handle, 
    uint32_t* minorVersion);

/**
 * @brief Gets the patch version of the cuEST library.
 * @ingroup Context
 *
 * @param[in]  handle              Opaque cuEST handle (must not be NULL).
 * @param[out] patchVersion        Patch version of the cuEST library (must not be NULL). 
 *
 * @return
 *   - \c CUEST_STATUS_SUCCESS on success,
 *   - \c CUEST_STATUS_INVALID_HANDLE if handle is NULL,
 *   - \c CUEST_STATUS_INVALID_TYPE if handle is not of the \c cuestHandle_t type,
 *   - \c CUEST_STATUS_NULL_POINTER if patchVersion is NULL,
 *   - \c CUEST_STATUS_EXCEPTION or \c CUEST_STATUS_UNKNOWN_ERROR otherwise.
 */
CUEST_API
cuestStatus_t cuestGetPatchVersion(
    cuestHandle_t handle, 
    uint32_t* patchVersion);

#ifdef __cplusplus
} // extern "C"
#endif

#endif /* CONTEXT_API_H */
