cmake_minimum_required(VERSION 3.16)
project(lidar_manager_web LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

include(FetchContent)
find_package(Threads REQUIRED)

FetchContent_Declare(
  cpp_httplib
  GIT_REPOSITORY https://github.com/yhirose/cpp-httplib.git
  GIT_TAG v0.44.0
)
FetchContent_GetProperties(cpp_httplib)
if(NOT cpp_httplib_POPULATED)
  FetchContent_Populate(cpp_httplib)
endif()

FetchContent_Declare(
  nlohmann_json
  URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
)
FetchContent_GetProperties(nlohmann_json)
if(NOT nlohmann_json_POPULATED)
  FetchContent_Populate(nlohmann_json)
endif()

find_package(SQLite3 REQUIRED)

add_executable(lidar_manager_web
  src/main.cpp
  src/app/lidar_manager_app.cpp
  src/util/file_util.cpp
  src/util/string_util.cpp
  src/util/id_util.cpp
  src/util/http_util.cpp
  src/util/crypto_util.cpp
  src/auth/auth_service.cpp
  src/domain/layout_schema.cpp
  src/domain/layout_profile.cpp
  src/storage/database.cpp
  src/storage/map_store.cpp
  src/storage/site_store.cpp
  src/storage/sound_store.cpp
  src/storage/dashboard_store.cpp
  src/storage/state_repository.cpp
  src/validation/sensor_validator.cpp
  src/server/static_file_server.cpp
  src/server/api_server.cpp
  src/mission/mission_queue.cpp
  src/mission/mission_store.cpp
  src/mission/mission_enqueue.cpp
  src/mission/modbus_trigger_service.cpp
  src/mission/mission_scheduler.cpp
  src/robot/robot_runtime.cpp
  src/server/api_mission_routes.cpp
  src/server/api_robot_routes.cpp
  src/server/api_media_routes.cpp
  src/server/api_dashboard_routes.cpp
)

target_link_libraries(lidar_manager_web PRIVATE Threads::Threads SQLite::SQLite3)

target_include_directories(lidar_manager_web PRIVATE
  "${CMAKE_CURRENT_SOURCE_DIR}/src"
)

target_include_directories(lidar_manager_web SYSTEM PRIVATE
  "${cpp_httplib_SOURCE_DIR}"
  "${nlohmann_json_SOURCE_DIR}/single_include"
)

target_compile_definitions(lidar_manager_web PRIVATE
  _DEFAULT_SOURCE
)

option(BUILD_TESTING "Build unit tests (requires tests/ directory)" OFF)

if(BUILD_TESTING)
  enable_testing()
  include(FetchContent)
  FetchContent_Declare(
    googletest
    URL https://github.com/google/googletest/archive/refs/tags/v1.14.0.tar.gz
  )
  set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
  FetchContent_MakeAvailable(googletest)

  set(LM_TEST_LIB_SOURCES
    src/util/file_util.cpp
    src/util/string_util.cpp
    src/util/id_util.cpp
    src/storage/database.cpp
    src/mission/mission_store.cpp
    src/mission/mission_enqueue.cpp
    src/validation/sensor_validator.cpp
  )

  add_executable(lidar_manager_tests
    tests/test_mission_enqueue.cpp
    tests/test_mission_store.cpp
    tests/test_sensor_validator.cpp
    ${LM_TEST_LIB_SOURCES}
  )

  target_include_directories(lidar_manager_tests PRIVATE
    "${CMAKE_CURRENT_SOURCE_DIR}/src"
  )
  target_include_directories(lidar_manager_tests SYSTEM PRIVATE
    "${nlohmann_json_SOURCE_DIR}/single_include"
  )
  target_compile_definitions(lidar_manager_tests PRIVATE
    TEST_FIXTURE_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests/fixtures/data"
  )
  target_link_libraries(lidar_manager_tests PRIVATE GTest::gtest_main SQLite::SQLite3)
  include(GoogleTest)
  gtest_discover_tests(lidar_manager_tests WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
  add_test(NAME unit COMMAND lidar_manager_tests)
endif()
