/*
 * SPDX-FileCopyrightText: 2026 Oak Ridge National Laboratory and Contributors
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#ifndef ADIOS2_BINDINGS_PYTHON_ENGINE_H_
#define ADIOS2_BINDINGS_PYTHON_ENGINE_H_

#include <nanobind/nanobind.h>
#include <nanobind/ndarray.h>

#include <string>

#include "adios2/core/Engine.h"

#include "nbVariable.h"

namespace nb = nanobind;

namespace adios2
{
namespace py11
{

// forward declare
class IO; // friend

class Engine
{
    friend class IO;
    friend class Query;

public:
    struct Info
    {
        Dims Start;
        Dims Count;
    };

    Engine() = default;

    ~Engine() = default;

    explicit operator bool() const noexcept;

    nb::bytes GetMetadata();

    StepStatus BeginStep(const StepMode mode, const float timeoutSeconds = -1.f);
    StepStatus BeginStep();

    void Put(Variable variable, const nb::ndarray<nb::numpy> &array,
             const Mode launch = Mode::Deferred);
    void Put(Variable variable, const std::vector<int64_t> &ints,
             const Mode launch = Mode::Deferred);
    void Put(Variable variable, const std::vector<double> &doubles,
             const Mode launch = Mode::Deferred);
    // function for cupy, numpy and Tensor pointers
    void Put(Variable variable, std::uintptr_t array, const Mode launch);
    void Put(Variable variable, const std::vector<std::complex<double>> &complexes,
             const Mode launch = Mode::Deferred);
    void Put(Variable variable, const std::string &string);
    void PerformPuts();
    void PerformDataWrite();

    void Get(Variable variable, nb::ndarray<nb::numpy> &array, const Mode launch = Mode::Deferred);
    // function for cupy, numpy and Tensor pointers
    void Get(Variable variable, std::uintptr_t array, const Mode launch);
    std::string Get(Variable variable, const Mode launch = Mode::Deferred);

    void PerformGets();

    void EndStep();

    /**
     * Returns current status information for each engine.
     * @return if between BeginStep/EndStep() pair
     */
    bool BetweenStepPairs() const;

    void Flush(const int transportIndex = -1);

    void Close(const int transportIndex = -1);

    size_t CurrentStep() const;

    std::string Name() const;
    std::string Type() const;
    size_t Steps() const;
    void LockWriterDefinitions() const;
    void LockReaderSelections() const;

    std::vector<std::map<std::string, std::string>> BlocksInfo(std::string &string,
                                                               const size_t step) const;

private:
    Engine(core::Engine *engine);
    core::Engine *m_Engine = nullptr;
};

} // end namespace py11
} // end namespace adios2

#endif
