# QML SceneGraph MCP Bridge
# SPDX-License-Identifier: GPL-2.0-or-later
#
# Links GammaRay client libs (GPL-2.0-or-later) => this bridge must be
# GPL-compatible. qtmcp is consumed under its GPL-2.0-only option (compatible).

cmake_minimum_required(VERSION 3.16)
project(QmlSceneGraphMcpBridge LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)              # qtmcp requires C++20
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)

# --- qtmcp via FetchContent FIRST ---
# qtmcp's find_package(Qt6) (inside the subdirectory) creates 3rd-party targets
# like Threads::Threads and promotes them to global THERE. If a prior top-level
# find_package(Qt6)/find_package(GammaRay) already created Threads::Threads at top
# scope, the subdirectory promotion fails ("not built in this directory"). So
# FetchContent runs before any Qt6/GammaRay find at this level.
include(FetchContent)
FetchContent_Declare(
  qtmcp
  GIT_REPOSITORY https://github.com/signal-slot/qtmcp.git
  GIT_TAG main        # TODO: pin to a released tag/commit for reproducibility
  GIT_SHALLOW TRUE
)
# Qt-prefixed vars (NOT BUILD_EXAMPLES) — verified to suppress example/test build.
set(QT_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
set(QT_BUILD_TESTS OFF CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(qtmcp)

# --- GammaRay (built & installed in ../install-prefix by Step 1) ---
# Pass -DCMAKE_PREFIX_PATH=$(pwd)/../install-prefix at configure.
find_package(GammaRay REQUIRED)

# --- Qt6 components for the bridge target (Qt6 already found globally by qtmcp) ---
find_package(Qt6 COMPONENTS Core Gui Widgets Network REQUIRED)

add_executable(qml-sg-mcp-bridge
    src/main.cpp
    src/gammaray_session.cpp
    src/gammaray_session.h
    src/scenegraph_tools.cpp
    src/scenegraph_tools.h
    src/material_interface.cpp
    src/material_interface.h
    src/quickinspector_types.h
)

target_link_libraries(qml-sg-mcp-bridge PRIVATE
    gammaray_client                    # VERIFIED: no GammaRay:: namespace
    gammaray_common
    Qt6::Core
    Qt6::Gui
    Qt6::Widgets                       # REQUIRED: QApplication for RemoteModel::style()
    Qt6::Network
    Qt6::McpServer                     # qtmcp, provides Qt::McpServer alias
    Qt6::McpCommon
)

# Add GammaRay source common/ for message.h (not in installed headers)
target_include_directories(qml-sg-mcp-bridge PRIVATE
    /home/blumia/Sources/GammaRay/common
)

# Runtime: qtmcp builds SHARED libs + the stdio backend plugin into the build
# tree. Set RPATH so the bridge finds libQt6Mcp*.so, and set QT_PLUGIN_PATH at
# run time (see run.sh) so Qt loads mcpserverbackend/libqmcpserverstdio.so.
# Also add GammaRay's install lib dir for libgammaray_*.so.
set(_GR_LIBDIR "$<TARGET_PROPERTY:gammaray_client,IMPORTED_LOCATION>")
set_target_properties(qml-sg-mcp-bridge PROPERTIES
  BUILD_RPATH "$<TARGET_FILE_DIR:Qt6::McpServer>;${CMAKE_SOURCE_DIR}/../install-prefix/lib"
  INSTALL_RPATH "$ORIGIN/../lib"
)
