cmake_minimum_required(VERSION 3.16)
project(unitree_go2_custom VERSION 1.0.0)

# Set C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find required packages
find_package(PkgConfig REQUIRED)
find_package(Threads REQUIRED)

# Find unitree_sdk2
set(UNITREE_SDK2_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../unitree_sdk2")
if(NOT EXISTS ${UNITREE_SDK2_PATH})
    message(FATAL_ERROR "unitree_sdk2 not found at ${UNITREE_SDK2_PATH}")
endif()

# Add unitree_sdk2 as subdirectory
add_subdirectory(${UNITREE_SDK2_PATH} unitree_sdk2)

# Find MQTT library (Eclipse Paho)
pkg_check_modules(PAHO_MQTT REQUIRED libpaho-mqtt3c)
pkg_check_modules(PAHO_MQTTPP REQUIRED libpaho-mqttpp3)

# Find JSON library
find_package(nlohmann_json REQUIRED)

# Include directories
include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${UNITREE_SDK2_PATH}/include
)

# Source files
set(SOURCES
    src/main.cpp
    src/mqtt.cpp
    src/controller.cpp
    src/custom_robot.cpp
    src/config.cpp
    src/logger.cpp
)

# Create executable with simple name
add_executable(main ${SOURCES})

# Link libraries
target_link_libraries(main
    unitree_sdk2
    ${PAHO_MQTT_LIBRARIES}
    ${PAHO_MQTTPP_LIBRARIES}
    nlohmann_json::nlohmann_json
    Threads::Threads
)

# Compiler options
target_compile_options(main PRIVATE
    ${PAHO_MQTT_CFLAGS_OTHER}
    ${PAHO_MQTTPP_CFLAGS_OTHER}
)

# Set output directory
set_target_properties(main PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}
)

# Install target
install(TARGETS main
    RUNTIME DESTINATION bin
)

# Copy config files
install(FILES config/robot_config.json
    DESTINATION etc/${PROJECT_NAME}
)
