#  Copyright (C) GridGain Systems. All Rights Reserved.
#  _________        _____ __________________        _____
#  __  ____/___________(_)______  /__  ____/______ ____(_)_______
#  _  / __  __  ___/__  / _  __  / _  / __  _  __ `/__  / __  __ \
#  / /_/ /  _  /    _  /  / /_/ /  / /_/ /  / /_/ /  / /_/ /  / /
#  \____/   /_/     /_/   \_,__/   \____/   \__,_/  /_/   /_/ /_/

cmake_minimum_required(VERSION 3.18)
project(examples_test)

include(ExternalProject)
enable_testing()

# ---------------------------------------------------------------------------
# IGNITE_CMAKE_PREFIX_PATH  (required)
# Path to the cmake package directory produced by the ignite build,
# e.g. modules/platforms/cpp/cmake-build-debug/cmake.
# This is forwarded to every example so they each exercise find_package()
# exactly as a real user project would.
# ---------------------------------------------------------------------------
set(IGNITE_CMAKE_PREFIX_PATH "" CACHE PATH
    "Path to the ignite cmake package directory")

if(NOT IGNITE_CMAKE_PREFIX_PATH)
    message(FATAL_ERROR
        "IGNITE_CMAKE_PREFIX_PATH is required.\n"
        "Point it at the ignite cmake package directory, e.g.:\n"
        "  -DIGNITE_CMAKE_PREFIX_PATH="
        "/path/to/ignite-3/modules/platforms/cpp/cmake-build-debug/cmake")
endif()

# ---------------------------------------------------------------------------
# IGNITE_EXAMPLES_DIR  (optional)
# Path to the examples source directory.  Defaults to the standard relative
# location inside the source tree; override when pointing at examples from an
# installed package (e.g. /usr/share/gridgain/examples).
# ---------------------------------------------------------------------------
set(IGNITE_EXAMPLES_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../../examples" CACHE PATH
    "Path to the examples source directory")

# ---------------------------------------------------------------------------
# GRIDGAIN_CLUSTER_ADDRESS  (optional, default: localhost)
# Passed as an environment variable when running the examples.
# ---------------------------------------------------------------------------
set(GRIDGAIN_CLUSTER_ADDRESS "localhost" CACHE STRING
    "Cluster address used when running examples (host or host:port)")

# ---------------------------------------------------------------------------
# add_example(NAME)
#
# Registers one example as two CMake artefacts:
#
#   build_<NAME>   — ExternalProject target that configures and builds the
#                    example from its own CMakeLists.txt via find_package().
#                    Failing here means the package config or compilation is
#                    broken.
#
#   run_<NAME>     — CTest test that executes the compiled binary against a
#                    running cluster.  The example exits 0 on success and 1
#                    on error, so a non-zero exit code marks the test failed.
# ---------------------------------------------------------------------------
function(add_example NAME)
    set(_src "${IGNITE_EXAMPLES_DIR}/${NAME}")
    set(_bin "${CMAKE_CURRENT_BINARY_DIR}/${NAME}")

    # ExternalProject_Add automatically forwards the parent's generator, platform,
    # and toolset to the sub-project — do NOT pass -A/-T via CMAKE_ARGS or cmake
    # rejects the duplicate ("Multiple -A options not allowed").
    ExternalProject_Add(build_${NAME}
        SOURCE_DIR  "${_src}"
        BINARY_DIR  "${_bin}"
        CMAKE_ARGS  "-DCMAKE_PREFIX_PATH=${IGNITE_CMAKE_PREFIX_PATH}"
                    "-DCMAKE_BUILD_TYPE=${CMAKE_BUILD_TYPE}"
        # Always re-run the build step so source changes are picked up.
        BUILD_ALWAYS    ON
        INSTALL_COMMAND ""
    )

    # Multi-config generators (Visual Studio, Xcode) place binaries in a
    # per-config subdirectory; single-config generators (Ninja, Makefile) do not.
    if(CMAKE_CONFIGURATION_TYPES)
        set(_exe "${_bin}/$<CONFIG>/${NAME}${CMAKE_EXECUTABLE_SUFFIX}")
    else()
        set(_exe "${_bin}/${NAME}${CMAKE_EXECUTABLE_SUFFIX}")
    endif()

    add_test(NAME run_${NAME}
        COMMAND "${_exe}"
    )
    set_tests_properties(run_${NAME} PROPERTIES
        ENVIRONMENT "GRIDGAIN_CLUSTER_ADDRESS=${GRIDGAIN_CLUSTER_ADDRESS}"
        TIMEOUT     60
    )
endfunction()

add_example(binary_views)
add_example(views)
add_example(sql_api)
add_example(transactions)
add_example(continuous_query)

# binary_views and views share the PUBLIC.PERSON table; serialize them so
# parallel ctest runs (-j N) don't cause conflicts.
set_tests_properties(run_binary_views run_views PROPERTIES
    RESOURCE_LOCK person_table)
