cmake_minimum_required(VERSION 3.16)
project(daq_viewer VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
set(CMAKE_AUTOUIC ON)

# ── Qt6 → Qt5 fallback ────────────────────────────────────────────────
find_package(Qt6 QUIET COMPONENTS Core Widgets PrintSupport)
if(Qt6_FOUND)
    message(STATUS "Using Qt6 ${Qt6_VERSION}")
    set(QT_LIBS Qt6::Core Qt6::Widgets Qt6::PrintSupport)
else()
    find_package(Qt5 REQUIRED COMPONENTS Core Widgets PrintSupport)
    message(STATUS "Using Qt5 ${Qt5_VERSION}")
    set(QT_LIBS Qt5::Core Qt5::Widgets Qt5::PrintSupport)
endif()

# ── Sources ───────────────────────────────────────────────────────────
set(NATIVEUI_SRC ../NativeUI/src)

set(APP_SOURCES
    src/main.cpp
    src/mainwindow.cpp
    src/plotwidget.cpp
    src/sidebar.cpp
    src/trigger_toolbar.cpp
    src/stats_panel.cpp
    src/layout_picker.cpp
    # Reused from NativeUI
    ${NATIVEUI_SRC}/config.cpp
    ${NATIVEUI_SRC}/protocol.cpp
    ${NATIVEUI_SRC}/udp_client.cpp
    ${NATIVEUI_SRC}/source.cpp
    ${NATIVEUI_SRC}/decimator.cpp
    ${NATIVEUI_SRC}/trigger_engine.cpp
    # QCustomPlot
    third_party/qcustomplot/qcustomplot.cpp
)

set(APP_HEADERS
    src/mainwindow.h
    src/plotwidget.h
    src/sidebar.h
    src/trigger_toolbar.h
    src/stats_panel.h
    src/lttb.h
    third_party/qcustomplot/qcustomplot.h
)

add_executable(daq_viewer ${APP_SOURCES} ${APP_HEADERS})

target_include_directories(daq_viewer PRIVATE
    src/
    ${NATIVEUI_SRC}
    third_party/qcustomplot/
)

target_link_libraries(daq_viewer PRIVATE ${QT_LIBS})

# Threads (for UDPClient)
find_package(Threads REQUIRED)
target_link_libraries(daq_viewer PRIVATE Threads::Threads)

# ── Install ───────────────────────────────────────────────────────────
install(TARGETS daq_viewer RUNTIME DESTINATION bin)
