cmake_minimum_required(VERSION 3.16)
project(oplabs-mixer-daemon
    VERSION 1.0.0
    DESCRIPTION "OPLabs Standalone Mixer Engine Daemon"
    LANGUAGES CXX
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# ---------------------------------------------------------------------------
# Dependencies
# ---------------------------------------------------------------------------

# JACK Audio Connection Kit
find_package(PkgConfig QUIET)
if(PkgConfig_FOUND)
    pkg_check_modules(JACK jack)
endif()
if(NOT JACK_FOUND)
    find_package(Jack QUIET)
endif()

# nlohmann/json (single-header, fetched automatically)
include(FetchContent)
FetchContent_Declare(
    nlohmann_json
    GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG v3.11.3
)
FetchContent_MakeAvailable(nlohmann_json)

# ---------------------------------------------------------------------------
# Mixer library (static)
# ---------------------------------------------------------------------------
set(MIXER_SOURCES
    src/MixerBus.cpp
    src/MixerChannelStrip.cpp
    src/MixerEngine.cpp
)

set(MIXER_HEADERS
    include/mixer/Types.hpp
    include/mixer/MixerBus.hpp
    include/mixer/MixerChannelStrip.hpp
    include/mixer/MixerEngine.hpp
)

add_library(mixer STATIC ${MIXER_SOURCES})
target_include_directories(mixer PUBLIC include)
target_link_libraries(mixer PUBLIC nlohmann_json::nlohmann_json)
target_compile_options(mixer PRIVATE -Wall -Wextra -Wpedantic)

# ---------------------------------------------------------------------------
# Control server library (depends on mixer + JACK)
# ---------------------------------------------------------------------------
add_library(mixer-control ${MIXER_SOURCES} src/MixerControlServer.cpp)
target_include_directories(mixer-control PUBLIC include)
target_link_libraries(mixer-control PUBLIC nlohmann_json::nlohmann_json)
target_compile_options(mixer-control PRIVATE -Wall -Wextra -Wpedantic)

# ---------------------------------------------------------------------------
# Daemon executable
# ---------------------------------------------------------------------------
add_executable(mixer-daemon src/main.cpp)
target_link_libraries(mixer-daemon
    mixer-control
    nlohmann_json::nlohmann_json
)
if(JACK_FOUND)
    target_link_libraries(mixer-daemon ${JACK_LIBRARIES})
    target_include_directories(mixer-daemon PRIVATE ${JACK_INCLUDE_DIRS})
    target_compile_options(mixer-daemon PRIVATE ${JACK_CFLAGS_OTHER})
    target_compile_definitions(mixer-daemon PRIVATE HAS_JACK=1)
    message(STATUS "JACK found: ${JACK_LIBRARIES}")
else()
    message(WARNING "JACK not found — building without JACK support. Install libjack-jackd2-dev (Debian) or jack-audio-connection-kit-devel (Fedora).")
endif()
target_compile_options(mixer-daemon PRIVATE -Wall -Wextra -Wpedantic)

# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------
enable_testing()

add_executable(mixer-test
    tests/test_main.cpp
    ${MIXER_SOURCES}
)
target_include_directories(mixer-test PRIVATE include)
target_link_libraries(mixer-test nlohmann_json::nlohmann_json)
target_compile_options(mixer-test PRIVATE -Wall -Wextra -Wpedantic)
add_test(NAME mixer-core-test COMMAND mixer-test)

# ---------------------------------------------------------------------------
# Install
# ---------------------------------------------------------------------------
install(TARGETS mixer-daemon RUNTIME DESTINATION bin)
install(FILES ${MIXER_HEADERS} DESTINATION include/mixer)
