cmake_minimum_required(VERSION 3.20)
project(weaseldb VERSION 0.0.1)

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

if(NOT CMAKE_BUILD_TYPE)
  set(CMAKE_BUILD_TYPE Release)
endif()

set(CMAKE_CXX_FLAGS_DEBUG "-g -O0 -Wall -Wextra")
set(CMAKE_CXX_FLAGS_RELEASE
    "-g -O3 -Wall -Wextra -DNDEBUG -fno-omit-frame-pointer")

add_subdirectory(third_party)

find_package(Threads REQUIRED)

include(FetchContent)
FetchContent_Declare(
  toml11
  GIT_REPOSITORY https://github.com/ToruNiina/toml11.git
  GIT_TAG be08ba2be2a964edcdb3d3e3ea8d100abc26f286 # v4.4.0
)
FetchContent_MakeAvailable(toml11)

FetchContent_Declare(
  doctest
  GIT_REPOSITORY https://github.com/doctest/doctest.git
  GIT_TAG 1da23a3e8119ec5cce4f9388e91b065e20bf06f5 # v2.4.12
)
FetchContent_MakeAvailable(doctest)

FetchContent_Declare(
  nanobench
  GIT_REPOSITORY https://github.com/martinus/nanobench.git
  GIT_TAG a5a50c2b33eea2ff1fcb355cacdface43eb42b25 # v4.3.11
)
FetchContent_MakeAvailable(nanobench)

FetchContent_Declare(
  nlohmann_json
  GIT_REPOSITORY https://github.com/nlohmann/json.git
  GIT_TAG bc889afb4c5bf1c0d8ee29ef35eaaf4c8bef8a5d # v3.11.2
)
FetchContent_MakeAvailable(nlohmann_json)

set(RAPIDJSON_BUILD_TESTS
    OFF
    CACHE BOOL "Disable RapidJSON tests" FORCE)
set(RAPIDJSON_BUILD_DOC
    OFF
    CACHE BOOL "Disable RapidJSON documentation" FORCE)
set(RAPIDJSON_BUILD_EXAMPLES
    OFF
    CACHE BOOL "Disable RapidJSON examples" FORCE)
FetchContent_Declare(
  RapidJSON
  GIT_REPOSITORY https://github.com/Tencent/rapidjson.git
  GIT_TAG ab1842a2dae061284c0a62dca1cc6d5e7e37e346 # v1.1.0
)
FetchContent_MakeAvailable(RapidJSON)

set(SIMDUTF_TESTS
    OFF
    CACHE BOOL "Disable simdutf tests" FORCE)
set(SIMDUTF_BENCHMARKS
    OFF
    CACHE BOOL "Disable simdutf benchmarks" FORCE)
set(SIMDUTF_TOOLS
    OFF
    CACHE BOOL "Disable simdutf tools" FORCE)
FetchContent_Declare(
  simdutf
  GIT_REPOSITORY https://github.com/simdutf/simdutf.git
  GIT_TAG 6aacd743d20528a2082189504ac96caf749e6c2e # v7.3.6
)
FetchContent_MakeAvailable(simdutf)

FetchContent_Declare(
  llhttp
  URL "https://github.com/nodejs/llhttp/archive/refs/tags/release/v9.2.1.tar.gz"
  URL_HASH
    SHA256=3c163891446e529604b590f9ad097b2e98b5ef7e4d3ddcf1cf98b62ca668f23e
  DOWNLOAD_EXTRACT_TIMESTAMP ON)
set(BUILD_SHARED_LIBS
    OFF
    CACHE INTERNAL "")
set(BUILD_STATIC_LIBS
    ON
    CACHE INTERNAL "")
FetchContent_MakeAvailable(llhttp)

include_directories(src)

find_package(weaseljson REQUIRED)

# Suppress deprecated literal operator warnings globally (from nlohmann_json and
# toml11)
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
  add_compile_options(-Wno-unknown-warning-option
                      -Wno-deprecated-literal-operator)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  # GCC doesn't have deprecated-literal-operator warning, so no need to suppress
endif()

# Generate JSON token hash table using gperf
find_program(GPERF_EXECUTABLE gperf REQUIRED)
add_custom_command(
  OUTPUT ${CMAKE_BINARY_DIR}/json_tokens.cpp
  COMMAND
    ${GPERF_EXECUTABLE} --class-name=PerfectHash
    ${CMAKE_SOURCE_DIR}/src/json_tokens.gperf >
    ${CMAKE_BINARY_DIR}/json_tokens.cpp
  DEPENDS ${CMAKE_SOURCE_DIR}/src/json_tokens.gperf
  COMMENT "Generating JSON token hash table with gperf")
add_custom_target(generate_json_tokens
                  DEPENDS ${CMAKE_BINARY_DIR}/json_tokens.cpp)

add_executable(weaseldb src/main.cpp)
target_link_libraries(weaseldb weaseldb_sources)

enable_testing()

# Create shared test data library
add_library(test_data STATIC benchmarks/test_data.cpp)
target_include_directories(test_data PUBLIC benchmarks)
target_link_libraries(test_data simdutf::simdutf)

# Create doctest implementation library
add_library(doctest_impl STATIC doctest_impl.cpp)
target_link_libraries(doctest_impl PUBLIC doctest::doctest)

# Create nanobench implementation library
add_library(nanobench_impl STATIC nanobench_impl.cpp)
target_link_libraries(nanobench_impl PUBLIC nanobench)

# Define all source files in one place
set(WEASELDB_SOURCES
    src/arena.cpp
    src/cpu_work.cpp
    src/format.cpp
    src/metric.cpp
    src/json_commit_request_parser.cpp
    src/api_url_parser.cpp
    src/server.cpp
    src/connection.cpp
    src/connection_registry.cpp
    src/http_handler.cpp
    src/config.cpp
    src/process_collector.cpp
    ${CMAKE_BINARY_DIR}/json_tokens.cpp)

# Create library based on build type
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  # In debug builds, use single library with assertions enabled
  add_library(weaseldb_sources STATIC ${WEASELDB_SOURCES})
  add_dependencies(weaseldb_sources generate_json_tokens)
  target_include_directories(weaseldb_sources PUBLIC src)
  target_link_libraries(
    weaseldb_sources PUBLIC simdutf::simdutf weaseljson Threads::Threads
                            llhttp_static toml11::toml11 perfetto)
  target_compile_options(weaseldb_sources PRIVATE -UNDEBUG)

  # Alias for tests to use same target name
  add_library(weaseldb_sources_debug ALIAS weaseldb_sources)
else()
  # In release builds, create both variants
  add_library(weaseldb_sources STATIC ${WEASELDB_SOURCES})
  add_dependencies(weaseldb_sources generate_json_tokens)
  target_include_directories(weaseldb_sources PUBLIC src)
  target_link_libraries(
    weaseldb_sources PUBLIC simdutf::simdutf weaseljson Threads::Threads
                            llhttp_static toml11::toml11 perfetto)

  # Debug version with assertions enabled for tests
  add_library(weaseldb_sources_debug STATIC ${WEASELDB_SOURCES})
  add_dependencies(weaseldb_sources_debug generate_json_tokens)
  target_include_directories(weaseldb_sources_debug PUBLIC src)
  target_link_libraries(
    weaseldb_sources_debug PUBLIC simdutf::simdutf weaseljson Threads::Threads
                                  llhttp_static toml11::toml11 perfetto)
  target_compile_options(weaseldb_sources_debug PRIVATE -UNDEBUG)
endif()

add_executable(test_arena tests/test_arena.cpp)
target_link_libraries(test_arena doctest_impl weaseldb_sources_debug)
target_compile_options(test_arena PRIVATE -UNDEBUG)

add_executable(test_server tests/test_server.cpp)
target_link_libraries(test_server doctest_impl weaseldb_sources_debug)
target_compile_options(test_server PRIVATE -UNDEBUG)
add_test(NAME test_server COMMAND test_server)

add_executable(
  test_commit_request
  tests/test_commit_request.cpp tests/nlohmann_reference_parser.cpp
  tests/parser_comparison.cpp)
target_link_libraries(test_commit_request doctest_impl weaseldb_sources_debug
                      test_data nlohmann_json::nlohmann_json)
target_include_directories(test_commit_request PRIVATE tests)
target_compile_options(test_commit_request PRIVATE -UNDEBUG)

# Metrics system test
add_executable(test_metric tests/test_metric.cpp)
target_link_libraries(test_metric doctest_impl weaseldb_sources_debug)
target_compile_options(test_metric PRIVATE -UNDEBUG)

# Register with CTest
add_test(NAME metric_tests COMMAND test_metric)

add_executable(bench_arena benchmarks/bench_arena.cpp)
target_link_libraries(bench_arena nanobench_impl weaseldb_sources)

add_executable(bench_cpu_work benchmarks/bench_cpu_work.cpp src/cpu_work.cpp)
target_link_libraries(bench_cpu_work nanobench_impl)

add_executable(bench_commit_request benchmarks/bench_commit_request.cpp)
target_link_libraries(bench_commit_request nanobench_impl weaseldb_sources
                      test_data)

add_executable(bench_parser_comparison benchmarks/bench_parser_comparison.cpp)
target_link_libraries(bench_parser_comparison nanobench_impl weaseldb_sources
                      test_data nlohmann_json::nlohmann_json)
target_include_directories(bench_parser_comparison
                           PRIVATE ${rapidjson_SOURCE_DIR}/include)

add_executable(bench_thread_pipeline benchmarks/bench_thread_pipeline.cpp)
target_link_libraries(bench_thread_pipeline nanobench_impl Threads::Threads)
target_include_directories(bench_thread_pipeline PRIVATE src)

add_executable(bench_format_comparison benchmarks/bench_format_comparison.cpp)
target_link_libraries(bench_format_comparison nanobench_impl weaseldb_sources)

# Metrics system benchmark
add_executable(bench_metric benchmarks/bench_metric.cpp)
target_link_libraries(bench_metric nanobench_impl weaseldb_sources)

# Register benchmark with CTest
add_test(NAME metric_benchmarks COMMAND bench_metric)

# Debug tools
add_executable(debug_arena tools/debug_arena.cpp)
target_link_libraries(debug_arena weaseldb_sources)

# Load tester
add_executable(load_tester tools/load_tester.cpp)
target_link_libraries(load_tester Threads::Threads llhttp_static perfetto)

add_test(NAME arena_tests COMMAND test_arena)
add_test(NAME commit_request_tests COMMAND test_commit_request)
add_test(NAME arena_benchmarks COMMAND bench_arena)
add_test(NAME commit_request_benchmarks COMMAND bench_commit_request)
add_test(NAME parser_comparison_benchmarks COMMAND bench_parser_comparison)
add_test(NAME thread_pipeline_benchmarks COMMAND bench_thread_pipeline)
add_test(NAME format_comparison_benchmarks COMMAND bench_format_comparison)

add_executable(test_api_url_parser tests/test_api_url_parser.cpp)
target_link_libraries(test_api_url_parser doctest_impl weaseldb_sources_debug)
target_compile_options(test_api_url_parser PRIVATE -UNDEBUG)
add_test(NAME api_url_parser_tests COMMAND test_api_url_parser)

# Reference counting tests and benchmarks
add_executable(test_reference tests/test_reference.cpp)
target_link_libraries(test_reference doctest_impl)
target_include_directories(test_reference PRIVATE src)
target_compile_options(test_reference PRIVATE -UNDEBUG)
add_test(NAME reference_tests COMMAND test_reference)

add_executable(bench_reference benchmarks/bench_reference.cpp)
target_link_libraries(bench_reference doctest_impl nanobench_impl
                      Threads::Threads)
target_include_directories(bench_reference PRIVATE src)
add_test(NAME reference_benchmarks COMMAND bench_reference)
