cmake_minimum_required(VERSION 3.24 FATAL_ERROR)
project(dmf-studio VERSION 0.1.0 LANGUAGES CXX C)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)  # required: static deps link into libmxl.so

# All executables land in build/bin so studio-manager finds node binaries next to itself
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

include(FetchContent)

# ── fmt ──────────────────────────────────────────────────────────────────────
FetchContent_Declare(fmt
    GIT_REPOSITORY https://github.com/fmtlib/fmt.git
    GIT_TAG        12.1.0)
FetchContent_MakeAvailable(fmt)

# ── spdlog (uses the fmt target fetched above) ────────────────────────────────
set(SPDLOG_FMT_EXTERNAL ON CACHE BOOL "" FORCE)
FetchContent_Declare(spdlog
    GIT_REPOSITORY https://github.com/gabime/spdlog.git
    GIT_TAG        v1.17.0)
FetchContent_MakeAvailable(spdlog)

# ── stduuid (header-only; no brew formula) ───────────────────────────────────
set(UUID_SYSTEM_GENERATOR OFF CACHE BOOL "" FORCE)  # avoid libuuid/CoreFoundation dep
set(UUID_USING_CXX20_SPAN ON  CACHE BOOL "" FORCE)  # std::span, no GSL dependency
set(UUID_BUILD_TESTS      OFF CACHE BOOL "" FORCE)
FetchContent_Declare(stduuid
    GIT_REPOSITORY https://github.com/mariusbancila/stduuid.git
    GIT_TAG        v1.2.3
)
FetchContent_MakeAvailable(stduuid)

# ── picojson (single-header; no brew formula) ────────────────────────────────
FetchContent_Declare(picojson_fc
    GIT_REPOSITORY https://github.com/kazuho/picojson.git
    GIT_TAG        v1.3.0
)
FetchContent_MakeAvailable(picojson_fc)

# MXL includes <picojson/picojson.h> and <picojson/wrapper.h>.
# We replicate what Findpicojson.cmake would do — correctly including both dirs.
set(_PICO_INC "${CMAKE_BINARY_DIR}/picojson-include")
set(_PICO_WRAP "${CMAKE_BINARY_DIR}/picojson-wrapper")
file(MAKE_DIRECTORY "${_PICO_INC}/picojson" "${_PICO_WRAP}/picojson")
file(COPY "${picojson_fc_SOURCE_DIR}/picojson.h"
     DESTINATION "${_PICO_INC}/picojson/")
file(WRITE "${_PICO_WRAP}/picojson/wrapper.h"
    "#pragma once\n"
    "#pragma GCC diagnostic push\n"
    "#pragma GCC diagnostic ignored \"-Wmaybe-uninitialized\"\n"
    "#include <picojson/picojson.h>\n"
    "#pragma GCC diagnostic pop\n")

add_library(picojson_impl INTERFACE)
target_include_directories(picojson_impl INTERFACE "${_PICO_INC}" "${_PICO_WRAP}")
add_library(picojson::picojson ALIAS picojson_impl)

# ── MXL SDK ──────────────────────────────────────────────────────────────────
# All four deps (fmt, spdlog, stduuid, picojson::picojson) are already targets,
# so MXL's "if (NOT TARGET ...)" guards skip its find_package() calls.
set(BUILD_TOOLS OFF CACHE BOOL "" FORCE)
set(BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(BUILD_DOCS  OFF CACHE BOOL "" FORCE)
set(BUILD_UTILS OFF CACHE BOOL "" FORCE)
add_subdirectory(mxl)

# ── nlohmann/json (for our code) ─────────────────────────────────────────────
FetchContent_Declare(json
    URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz)
FetchContent_MakeAvailable(json)

# ── FFmpeg ──────────────────────────────────────────────────────────────────
find_package(PkgConfig REQUIRED)

# Check for FFmpeg components
pkg_check_modules(FFMPEG REQUIRED
    libavformat
    libavcodec
    libswscale
    libavutil
    libswresample
)

# Create an interface library for FFmpeg
add_library(ffmpeg INTERFACE)
target_include_directories(ffmpeg INTERFACE ${FFMPEG_INCLUDE_DIRS})
target_link_libraries(ffmpeg INTERFACE ${FFMPEG_LIBRARIES})

# ── Shared utilities (Signal.hpp, NodeBase.hpp, FlowDef.hpp, V210.hpp) ───────
add_library(dmf-shared INTERFACE)
target_include_directories(dmf-shared INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/shared)
target_link_libraries(dmf-shared INTERFACE mxl nlohmann_json::nlohmann_json)

# ── Basic nodes ──────────────────────────────────────────────────────────────
add_subdirectory(nodes/testpattern)
add_subdirectory(nodes/fakesink)

# ── NDI nodes ────────────────────────────────────────────────────────────────
set(NDI_SDK_DIR "" CACHE PATH "Path to NDI SDK root")
if(NDI_SDK_DIR)
    add_subdirectory(nodes/ndiin)
    add_subdirectory(nodes/ndiout)
endif()

# ── DeckLink nodes ────────────────────────────────────────────────────────────────
set(DECKLINK_SDK_DIR "" CACHE PATH "Path to DeckLink SDK root")
if(DECKLINK_SDK_DIR)
    add_subdirectory(nodes/decklinkin)
    add_subdirectory(nodes/decklinkout)
endif()

add_subdirectory(nodes/videoin)

# ── Core server ──────────────────────────────────────────────────────────────
add_subdirectory(studio-manager)
