cmake_minimum_required(VERSION 3.16)
project(robot_app 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(robot_app
  src/main.cpp
  src/app/robot_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/transition_store.cpp
  src/storage/io_module_store.cpp
  src/io/io_module_service.cpp
  src/io/io_zone_runtime.cpp
  src/io/io_module_usage.cpp
  src/storage/path_store.cpp
  src/storage/path_guide_store.cpp
  src/path/path_planner.cpp
  src/path/path_service.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/position_resolver.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_transition_routes.cpp
  src/server/api_io_module_routes.cpp
  src/server/api_path_routes.cpp
  src/server/api_path_guide_routes.cpp
  src/server/api_dashboard_routes.cpp
)

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

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

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

target_compile_definitions(robot_app 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(robot_app_tests
    tests/test_mission_enqueue.cpp
    tests/test_mission_store.cpp
    tests/test_sensor_validator.cpp
    ${LM_TEST_LIB_SOURCES}
  )

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