cmake_minimum_required(VERSION 3.20)
set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS
    "${CMAKE_CURRENT_SOURCE_DIR}/VERSION")
file(READ "${CMAKE_CURRENT_SOURCE_DIR}/VERSION" SVAPI_VERSION)
string(STRIP "${SVAPI_VERSION}" SVAPI_VERSION)
project(smartbotic-vectorapi VERSION ${SVAPI_VERSION} LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Build type" FORCE)
endif()

option(BUILD_TESTS "Build tests" ON)
option(BUILD_WEBUI "Build the React web UI via npm" ON)
# The function runner vendors QuickJS over the network at configure time, so it
# stays opt-in: an ordinary build and the whole existing test cycle must not
# start depending on a GitHub fetch. Build it on its own with
#   cmake -S runner -B build-runner
# which also works on the dev host, where the root project cannot configure.
option(BUILD_RUNNER "Build the QuickJS function runner" OFF)

include(GNUInstallDirs)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake")
include(Dependencies)

find_package(smartbotic-db-client REQUIRED)
find_package(nlohmann_json REQUIRED)
find_package(spdlog REQUIRED)
find_package(OpenSSL REQUIRED)

# Optional systemd (sd_notify)
find_package(PkgConfig)
if(PkgConfig_FOUND)
    pkg_check_modules(SYSTEMD IMPORTED_TARGET libsystemd)
endif()

# git commit for version banner.
#
# An EXPLICITLY PROVIDED value wins over the git probe, and that ordering is the
# whole point: the Docker release path has no `.git` (`.dockerignore` excludes
# it), so the probe fails there and every release deb reported
# `commit: unknown` while the local build reported the sha. build.sh had always
# computed the commit and passed it as `--build-arg BUILD_GIT_COMMIT`, and
# Dockerfile.build never declared the ARG - so Docker silently dropped it and
# nothing consumed it. Accepted and ignored, in the build system rather than on
# a route.
#
# It matters because this field is the cheapest answer to "is that fix in this
# release": without it, the only instruments are the deb's mtime, which is not
# evidence, and mapping a commit against a bump commit, which needs you to
# already know the bump.
if(NOT SVAPI_GIT_COMMIT)
    execute_process(COMMAND git rev-parse --short HEAD
        WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}
        OUTPUT_VARIABLE SVAPI_GIT_COMMIT OUTPUT_STRIP_TRAILING_WHITESPACE
        ERROR_QUIET)
endif()
if(NOT SVAPI_GIT_COMMIT)
    set(SVAPI_GIT_COMMIT "unknown")
endif()
configure_file(${CMAKE_SOURCE_DIR}/cmake/version.hpp.in
    ${CMAKE_BINARY_DIR}/generated/svapi/version.hpp @ONLY)

add_subdirectory(src)

if(BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()

# After the BUILD_TESTS block on purpose: runner/CMakeLists.txt registers tests,
# which needs enable_testing() to have run already.
if(BUILD_RUNNER)
    add_subdirectory(runner)
endif()

if(BUILD_WEBUI)
    include(WebUI OPTIONAL)   # provided by plan 2; absent is fine
endif()

# Install: config conffile, api docs
install(FILES ${CMAKE_SOURCE_DIR}/config/config.json
    DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/smartbotic-vectorapi)
install(FILES ${CMAKE_SOURCE_DIR}/runner/config/runner.json
    DESTINATION ${CMAKE_INSTALL_SYSCONFDIR}/smartbotic-vectorapi
    OPTIONAL)
install(FILES ${CMAKE_SOURCE_DIR}/api/openapi.json
              ${CMAKE_SOURCE_DIR}/api/llms.txt
    DESTINATION ${CMAKE_INSTALL_DATADIR}/smartbotic-vectorapi
    OPTIONAL)
