cmake_minimum_required(VERSION 3.16)
project(fn_registry_cpp LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# --- Options ---
option(TRACY_ENABLE "Enable Tracy profiling" OFF)

# --- Vendor: Dear ImGui ---
set(IMGUI_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/imgui)
add_library(imgui STATIC
    ${IMGUI_DIR}/imgui.cpp
    ${IMGUI_DIR}/imgui_draw.cpp
    ${IMGUI_DIR}/imgui_tables.cpp
    ${IMGUI_DIR}/imgui_widgets.cpp
    ${IMGUI_DIR}/imgui_demo.cpp
    ${IMGUI_DIR}/backends/imgui_impl_glfw.cpp
    ${IMGUI_DIR}/backends/imgui_impl_opengl3.cpp
)
target_include_directories(imgui PUBLIC
    ${IMGUI_DIR}
    ${IMGUI_DIR}/backends
)

# --- Vendor: ImPlot ---
set(IMPLOT_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/implot)
add_library(implot STATIC
    ${IMPLOT_DIR}/implot.cpp
    ${IMPLOT_DIR}/implot_items.cpp
)
target_include_directories(implot PUBLIC ${IMPLOT_DIR})
target_link_libraries(implot PUBLIC imgui)

# --- Vendor: ImPlot3D ---
# Pinned to v0.4 (commit 41ae3e447c0de20ecab95d38a4b4dc0835a3efc2).
# See cpp/vendor/implot3d.VENDORING.md for update procedure.
set(IMPLOT3D_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/implot3d)
add_library(implot3d STATIC
    ${IMPLOT3D_DIR}/implot3d.cpp
    ${IMPLOT3D_DIR}/implot3d_items.cpp
    ${IMPLOT3D_DIR}/implot3d_meshes.cpp
)
target_include_directories(implot3d PUBLIC ${IMPLOT3D_DIR})
target_link_libraries(implot3d PUBLIC imgui)

# --- Vendor: Tracy (optional) ---
if(TRACY_ENABLE)
    set(TRACY_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/tracy)
    add_library(tracy STATIC
        ${TRACY_DIR}/public/TracyClient.cpp
    )
    target_include_directories(tracy PUBLIC ${TRACY_DIR}/public)
    target_compile_definitions(tracy PUBLIC TRACY_ENABLE)
endif()

# --- Vendor: imgui-node-editor ---
set(NODE_EDITOR_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/imgui-node-editor)
add_library(imgui_node_editor STATIC
    ${NODE_EDITOR_DIR}/imgui_node_editor.cpp
    ${NODE_EDITOR_DIR}/imgui_node_editor_api.cpp
    ${NODE_EDITOR_DIR}/imgui_canvas.cpp
    ${NODE_EDITOR_DIR}/crude_json.cpp
)
target_include_directories(imgui_node_editor PUBLIC ${NODE_EDITOR_DIR})
target_link_libraries(imgui_node_editor PUBLIC imgui)

# --- Platform dependencies ---
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
    # Cross-compile: use vendored or system GLFW, link opengl32/gdi32
    find_package(glfw3 QUIET)
    if(NOT glfw3_FOUND)
        # Build GLFW from source if available
        if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/vendor/glfw/CMakeLists.txt)
            set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
            set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
            set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
            add_subdirectory(vendor/glfw)
        else()
            message(FATAL_ERROR "GLFW not found. For Windows cross-compile, add GLFW source to cpp/vendor/glfw/")
        endif()
    endif()
    set(PLATFORM_LIBS glfw opengl32 gdi32 imm32)
else()
    # Linux native
    find_package(glfw3 REQUIRED)
    find_package(OpenGL REQUIRED)
    set(PLATFORM_LIBS glfw OpenGL::GL ${CMAKE_DL_LIBS})
endif()

target_link_libraries(imgui PUBLIC ${PLATFORM_LIBS})

# --- SQLite3 (shared by every app that uses it, including fn_framework for
# layout_storage) ---
# System on Linux, vendored amalgamation on Windows cross-compile.
find_package(SQLite3 QUIET)
if(NOT SQLite3_FOUND AND NOT TARGET sqlite3_vendored)
    set(SQLITE3_AMALG_DIR ${CMAKE_CURRENT_SOURCE_DIR}/vendor/sqlite3)
    add_library(sqlite3_vendored STATIC ${SQLITE3_AMALG_DIR}/sqlite3.c)
    target_include_directories(sqlite3_vendored PUBLIC ${SQLITE3_AMALG_DIR})
    target_compile_definitions(sqlite3_vendored PRIVATE
        SQLITE_THREADSAFE=1
        SQLITE_ENABLE_FTS5
        SQLITE_ENABLE_JSON1
    )
    add_library(SQLite::SQLite3 ALIAS sqlite3_vendored)
endif()

# --- Framework ---
# Incluye tokens.cpp (identidad visual Mantine dark + indigo), icon_font.cpp
# (Karla/Roboto/... + Tabler), app_settings.cpp (persistencia y ventana de
# settings) y fps_overlay.cpp (overlay opcional). Ver cpp/DESIGN_SYSTEM.md
add_library(fn_framework STATIC
    framework/app_base.cpp
    functions/core/tokens.cpp
    functions/core/icon_font.cpp
    functions/core/app_settings.cpp
    functions/core/app_about.cpp
    functions/core/fps_overlay.cpp
    functions/core/panel_menu.cpp
    functions/core/layouts_menu.cpp
    functions/core/app_menubar.cpp
    functions/gfx/gl_loader.cpp
    functions/core/layout_storage.cpp
)
target_include_directories(fn_framework PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/framework
    ${CMAKE_CURRENT_SOURCE_DIR}/functions
)
# FN_CPP_ROOT permite que icon_font.cpp localice vendor/tabler-icons/tabler-icons.ttf
# en builds de desarrollo desde el repo (en deploys, la TTF se copia junto al exe).
target_compile_definitions(fn_framework PUBLIC
    FN_CPP_ROOT="${CMAKE_CURRENT_SOURCE_DIR}"
)
target_link_libraries(fn_framework PUBLIC imgui implot implot3d SQLite::SQLite3)
if(TRACY_ENABLE)
    target_link_libraries(fn_framework PUBLIC tracy)
endif()

# --- Macro for creating ImGui apps ---
# Capturamos la raiz del modulo cpp/ para que add_imgui_app la use desde
# subdirectorios (donde CMAKE_CURRENT_SOURCE_DIR apunta al app, no al root).
set(FN_CPP_ROOT_DIR ${CMAKE_CURRENT_SOURCE_DIR} CACHE INTERNAL "fn_registry cpp root")

function(add_imgui_app target)
    add_executable(${target} ${ARGN})
    target_link_libraries(${target} PRIVATE fn_framework)
    target_include_directories(${target} PRIVATE
        ${FN_CPP_ROOT_DIR}/functions
    )
    # Copia las fuentes junto al ejecutable para deploys autonomos (sin
    # FN_CPP_ROOT en runtime). 4 TTFs vectoriales para el menu Settings + Tabler
    # para los iconos TI_*.
    add_custom_command(TARGET ${target} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
                ${FN_CPP_ROOT_DIR}/vendor/imgui/misc/fonts/Karla-Regular.ttf
                $<TARGET_FILE_DIR:${target}>/Karla-Regular.ttf
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
                ${FN_CPP_ROOT_DIR}/vendor/imgui/misc/fonts/Roboto-Medium.ttf
                $<TARGET_FILE_DIR:${target}>/Roboto-Medium.ttf
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
                ${FN_CPP_ROOT_DIR}/vendor/imgui/misc/fonts/DroidSans.ttf
                $<TARGET_FILE_DIR:${target}>/DroidSans.ttf
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
                ${FN_CPP_ROOT_DIR}/vendor/imgui/misc/fonts/Cousine-Regular.ttf
                $<TARGET_FILE_DIR:${target}>/Cousine-Regular.ttf
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
                ${FN_CPP_ROOT_DIR}/vendor/tabler-icons/tabler-icons.ttf
                $<TARGET_FILE_DIR:${target}>/tabler-icons.ttf
        VERBATIM
    )
endfunction()

# --- Function libraries (headers for composition) ---
# Functions are compiled as part of apps that use them via add_imgui_app.
# Each function is a .h/.cpp pair included by the app's CMakeLists.txt.

# --- Demo app ---
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/chart_demo/CMakeLists.txt)
    add_subdirectory(apps/chart_demo)
endif()

# --- Shaders Lab ---
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/shaders_lab/CMakeLists.txt)
    add_subdirectory(apps/shaders_lab)
endif()

# --- Primitives Gallery (catalogo visual de primitivos core/viz/gfx) ---
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/primitives_gallery/CMakeLists.txt)
    add_subdirectory(apps/primitives_gallery)
endif()

# --- text_editor + file_watcher smoke test (issue 0025) ---
# Build gate para validar que text_editor.cpp + file_watcher.cpp + vendor enlazan.
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/apps/text_editor_smoke/CMakeLists.txt)
    add_subdirectory(apps/text_editor_smoke)
endif()

# --- Registry Dashboard (lives in projects/fn_monitoring/apps/) ---
set(_DASH_DIR ${CMAKE_SOURCE_DIR}/../projects/fn_monitoring/apps/registry_dashboard)
if(EXISTS ${_DASH_DIR}/CMakeLists.txt)
    add_subdirectory(${_DASH_DIR} ${CMAKE_BINARY_DIR}/apps/registry_dashboard)
endif()

# --- Tests (Catch2 amalgamated, ctest-driven) ---
option(BUILD_TESTING "Build C++ tests" ON)
if(BUILD_TESTING)
    enable_testing()
    add_subdirectory(tests)
endif()
