cmake_minimum_required(VERSION 3.14...3.27)

# 设置编译器
if(NOT APPLE)
    set(CMAKE_C_COMPILER "/usr/local/bin/gcc")
    set(CMAKE_CXX_COMPILER "/usr/local/bin/g++")
endif()

# 设置 CMake 策略（移除非必要的新策略）
cmake_policy(SET CMP0074 NEW)  # 保留必要的包根目录策略

# 包含 FetchContent 模块
include(FetchContent)

# 生成 compile_commands.json
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

project(collision_avoidance)

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

# 检查编译器版本
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)
        message(FATAL_ERROR "GCC 版本需要 >= 7.0 以支持 C++17")
    endif()
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
    if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
        message(FATAL_ERROR "Clang 版本需要 >= 5.0 以支持 C++17")
    endif()
endif()

# 设置输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# 设置 Boost
set(Boost_NO_WARN_NEW_VERSIONS 1)
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
if(NOT APPLE)
    # 在 CentOS 上使用 Boost 1.69
    set(BOOST_ROOT "/usr/include/boost169")
    set(BOOST_LIBRARYDIR "/usr/lib64/boost169")
else()
    # 在 macOS 上使用 Homebrew 安装的 Boost
    set(BOOST_ROOT "/opt/homebrew/Cellar/boost/1.87.0")
    set(BOOST_LIBRARYDIR "/opt/homebrew/Cellar/boost/1.87.0/lib")
endif()

# 设置 CMake 策略（在 find_package(Boost) 前添加）
if(POLICY CMP0167)
    cmake_policy(SET CMP0167 OLD)  # 强制使用传统 FindBoost 模块
endif()

find_package(Boost 1.69.0 REQUIRED COMPONENTS 
    system 
    filesystem 
    thread 
    chrono 
    date_time 
    atomic
    regex  # 添加缺失的 regex 组件
)

# 添加 libcurl
find_package(CURL REQUIRED)

# 修改 JSON 库配置为双模式
if(APPLE)
    # macOS 使用 FetchContent
    FetchContent_Declare(
        nlohmann_json
        GIT_REPOSITORY https://github.com/nlohmann/json.git
        GIT_TAG v3.11.3
    )
    FetchContent_MakeAvailable(nlohmann_json)
else()
    # CentOS 使用系统安装的包
    find_package(nlohmann_json REQUIRED)
    
    # 验证系统包路径
    if(NOT TARGET nlohmann_json::nlohmann_json)
        message(FATAL_ERROR "系统未安装正确版本的 nlohmann_json")
    endif()
    
    # 添加手动包含路径（适用于旧版CMake）
    include_directories(${nlohmann_json_INCLUDE_DIRS})
endif()

# 源文件列表
set(LIB_SOURCES
    src/collector/DataCollector.cpp
    src/collector/DataSourceConfig.cpp
    src/core/System.cpp
    src/detector/CollisionDetector.cpp
    src/detector/SafetyZone.cpp
    src/network/HTTPDataSource.cpp
    src/network/WebSocketServer.cpp
    src/network/HTTPClient.cpp
    src/network/TrafficLightHttpServer.cpp
    src/spatial/CoordinateConverter.cpp
    src/types/BasicTypes.cpp
    src/types/VehicleData.cpp
    src/vehicle/ControllableVehicles.cpp
    src/config/AirportBounds.cpp
    src/config/SystemConfig.cpp
    src/config/IntersectionConfig.cpp
    src/detector/TrafficLightDetector.cpp
)

# 创建主库
add_library(${PROJECT_NAME}_lib ${LIB_SOURCES})

target_include_directories(${PROJECT_NAME}_lib
    PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${Boost_INCLUDE_DIRS}
    ${CURL_INCLUDE_DIRS}
)

target_link_libraries(${PROJECT_NAME}_lib
    PUBLIC
    Boost::system
    Boost::filesystem
    Boost::thread
    Boost::chrono
    Boost::date_time
    Boost::atomic
    Boost::regex
    nlohmann_json::nlohmann_json
    CURL::libcurl
    Threads::Threads
    -pthread
)

# 创建主可执行文件
add_executable(${PROJECT_NAME} src/main.cpp)
target_link_libraries(${PROJECT_NAME} PRIVATE ${PROJECT_NAME}_lib)

# 添加一个选项来控制是否编译测试
if(APPLE)
    option(BUILD_TESTS "Build test cases" ON)
else()
    option(BUILD_TESTS "Build test cases" OFF)
endif()

# 添加测试
if(BUILD_TESTS AND EXISTS "${CMAKE_SOURCE_DIR}/tests")
    if(APPLE)
        # macOS 使用 FetchContent 下载 googletest
        FetchContent_Declare(
            googletest
            GIT_REPOSITORY https://github.com/google/googletest.git
            GIT_TAG v1.15.2
        )
        FetchContent_MakeAvailable(googletest)
    else()
        # CentOS 也使用 FetchContent 下载 googletest
        FetchContent_Declare(
            googletest
            GIT_REPOSITORY https://github.com/google/googletest.git
            GIT_TAG v1.15.2
        )
        FetchContent_MakeAvailable(googletest)
    endif()

    # 添加 tests 子目录
    add_subdirectory(tests)
endif()

# 打印配置信息
message(STATUS "CMAKE_SOURCE_DIR: ${CMAKE_SOURCE_DIR}")
message(STATUS "PROJECT_SOURCE_DIR: ${PROJECT_SOURCE_DIR}")
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "CMAKE_CXX_FLAGS: ${CMAKE_CXX_FLAGS}")
message(STATUS "CMAKE_EXE_LINKER_FLAGS: ${CMAKE_EXE_LINKER_FLAGS}")

# 复制配置文件到构建目录
configure_file(${CMAKE_SOURCE_DIR}/config/system_config.json ${CMAKE_BINARY_DIR}/config/system_config.json COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/config/intersections.json ${CMAKE_BINARY_DIR}/config/intersections.json COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/config/airport_bounds.json ${CMAKE_BINARY_DIR}/config/airport_bounds.json COPYONLY)
configure_file(${CMAKE_SOURCE_DIR}/config/unmanned_vehicles.json ${CMAKE_BINARY_DIR}/config/unmanned_vehicles.json COPYONLY)

# 查找 Threads 包
find_package(Threads REQUIRED)

if(NOT APPLE)
    target_link_libraries(collision_avoidance
        PRIVATE
        ${PROJECT_NAME}_lib
    )
endif()