# D:\Dev\Mosis\MosisService\designer\CMakeLists.txt
cmake_minimum_required(VERSION 3.22.1)
project(mosis-designer)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find dependencies via vcpkg
find_package(glfw3 CONFIG REQUIRED)
find_package(freetype CONFIG REQUIRED)
find_package(PNG REQUIRED)
find_package(Lua REQUIRED)
find_package(glad CONFIG REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)

# Fetch RmlUi
include(FetchContent)
FetchContent_Declare(
    rmlui
    GIT_REPOSITORY https://github.com/mikke89/RmlUi.git
    GIT_TAG 6.2
)
set(RMLUI_LUA_BINDINGS ON CACHE BOOL "" FORCE)
set(BUILD_SHARED_LIBS OFF CACHE BOOL "" FORCE)
set(RMLUI_SAMPLES OFF CACHE BOOL "" FORCE)
set(RMLUI_TESTS OFF CACHE BOOL "" FORCE)
set(RMLUI_FONT_ENGINE "freetype" CACHE STRING "" FORCE)
FetchContent_MakeAvailable(rmlui)

# Get glad include directories explicitly
get_target_property(GLAD_INCLUDE_DIRS glad::glad INTERFACE_INCLUDE_DIRECTORIES)

#==============================================================================
# Mosis Core Library (shared sandbox APIs)
#==============================================================================
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../core ${CMAKE_CURRENT_BINARY_DIR}/mosis-core)

# Point mosis-core to use the same Lua and JSON as designer
target_include_directories(mosis-core PUBLIC ${LUA_INCLUDE_DIR})
target_link_libraries(mosis-core PUBLIC ${LUA_LIBRARIES})

#==============================================================================
# Mosis Kernel (RmlUi renderer - platform specific for now)
#==============================================================================
add_library(mosis-kernel STATIC
    ../src/main/cpp/RmlUi_Renderer_GL3.cpp
)
target_include_directories(mosis-kernel PUBLIC
    ../src/main/kernel/include
    ../src/main/cpp
    ${GLAD_INCLUDE_DIRS}
    ${LUA_INCLUDE_DIR}
)
target_link_libraries(mosis-kernel PUBLIC
    rmlui
    rmlui_lua
    ${LUA_LIBRARIES}
    glad::glad
)
target_include_directories(mosis-kernel PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/src
)
target_compile_definitions(mosis-kernel PUBLIC
    MOSIS_PLATFORM_DESKTOP
    # Use our wrapper header that sets up RMLUI_SHADER_HEADER_VERSION before including glad
    RMLUI_GL3_CUSTOM_LOADER="glad_loader.h"
)

#==============================================================================
# Designer Executable
#==============================================================================
add_executable(mosis-designer
    main.cpp
    src/desktop_platform.cpp
    src/desktop_file_interface.cpp
    src/hot_reload.cpp
    src/platform_singleton.cpp
    src/desktop_sandbox.cpp
    src/app_discovery.cpp
    src/testing/action_recorder.cpp
    src/testing/action_player.cpp
    src/testing/ui_inspector.cpp
    src/testing/visual_capture.cpp
    # Local backend with input recording hooks
    src/backend/RmlUi_Backend_GLFW_GL3.cpp
    src/backend/RmlUi_Platform_GLFW.cpp
)

target_include_directories(mosis-designer PRIVATE
    src
    src/testing
    src/backend
    ../src/main/kernel/include
    ../src/main/cpp
    # Core library includes
    ../core/include
    ../core/include/mosis/sandbox
)

target_link_libraries(mosis-designer PRIVATE
    mosis-kernel
    mosis-core
    glad::glad
    glfw
    freetype
    PNG::PNG
    nlohmann_json::nlohmann_json
)

# Windows-specific
if(WIN32)
    target_link_libraries(mosis-designer PRIVATE opengl32)
endif()

# Copy assets for development
add_custom_command(TARGET mosis-designer POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_CURRENT_SOURCE_DIR}/../src/main/assets
        $<TARGET_FILE_DIR:mosis-designer>/assets
)

# Copy simulator assets
add_custom_command(TARGET mosis-designer POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_CURRENT_SOURCE_DIR}/assets/simulator
        $<TARGET_FILE_DIR:mosis-designer>/simulator
)
