cmake_minimum_required(VERSION 3.20)
project(ClientUIModule LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Find dependencies
find_package(nlohmann_json QUIET)
find_package(spdlog QUIET)
find_package(imgui QUIET)
find_package(OpenGL REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_check_modules(GLFW3 REQUIRED glfw3)

# Minimal FetchContent for missing deps
if(NOT nlohmann_json_FOUND)
    include(FetchContent)
    FetchContent_Declare(nlohmann_json
        URL https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz
        URL_HASH SHA256=d6c65aca6b1ed68e7a182f4757257b107ae403032760ed6ef121c9d55e81757d
    )
    FetchContent_MakeAvailable(nlohmann_json)
endif()

if(NOT spdlog_FOUND)
    include(FetchContent)
    FetchContent_Declare(spdlog
        GIT_REPOSITORY https://github.com/gabime/spdlog.git
        GIT_TAG v1.12.0
    )
    FetchContent_MakeAvailable(spdlog)
endif()

# ImGui via FetchContent
if(NOT imgui_FOUND)
    include(FetchContent)
    FetchContent_Declare(imgui
        GIT_REPOSITORY https://github.com/ocornut/imgui.git
        GIT_TAG v1.89.9
    )
    FetchContent_MakeAvailable(imgui)

    # Create ImGui OpenGL backend library
    add_library(imgui_opengl STATIC
        ${imgui_SOURCE_DIR}/imgui.cpp
        ${imgui_SOURCE_DIR}/imgui_demo.cpp
        ${imgui_SOURCE_DIR}/imgui_draw.cpp
        ${imgui_SOURCE_DIR}/imgui_tables.cpp
        ${imgui_SOURCE_DIR}/imgui_widgets.cpp
        ${imgui_SOURCE_DIR}/backends/imgui_impl_opengl3.cpp
        ${imgui_SOURCE_DIR}/backends/imgui_impl_glfw.cpp
    )
    target_include_directories(imgui_opengl PUBLIC
        ${imgui_SOURCE_DIR}
        ${imgui_SOURCE_DIR}/backends
    )
    target_link_libraries(imgui_opengl PUBLIC OpenGL::GL ${GLFW3_LIBRARIES})
    target_include_directories(imgui_opengl PUBLIC ${GLFW3_INCLUDE_DIRS})
    set_target_properties(imgui_opengl PROPERTIES POSITION_INDEPENDENT_CODE ON)
endif()

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/build)

# ClientUI module as shared library (.so) - Non-graphical version
add_library(client-ui-module SHARED
    src/ClientUIModule.cpp
)

# ClientUI graphical module as shared library (.so) - With OpenGL
add_library(client-ui-graphical-module SHARED
    src/ClientUIModuleGraphical.cpp
    ../../../core/src/IntraIO.cpp
    ../../../core/src/IntraIOManager.cpp
    ../../../core/src/IOFactory.cpp
)

# ClientUI simulated graphics module (.so) - Console-based "GUI"
add_library(client-ui-simulated-module SHARED
    src/ClientUIModuleSimulatedGL.cpp
)

set_target_properties(client-ui-module PROPERTIES
    PREFIX ""
    SUFFIX ".so"
    OUTPUT_NAME "client-ui"
)

set_target_properties(client-ui-graphical-module PROPERTIES
    PREFIX ""
    SUFFIX ".so"
    OUTPUT_NAME "client-ui-graphical"
)

set_target_properties(client-ui-simulated-module PROPERTIES
    PREFIX ""
    SUFFIX ".so"
    OUTPUT_NAME "client-ui-simulated"
)

# Standalone test executable (non-graphical)
add_executable(client-ui-standalone
    src/main.cpp
)

# Standalone graphical executable
add_executable(client-ui-graphical
    src/main_graphical.cpp
    ../../../core/src/IntraIO.cpp
    ../../../core/src/IntraIOManager.cpp
    ../../../core/src/IOFactory.cpp
)

# Standalone simulated graphics executable
add_executable(client-ui-simulated
    tests/ClientUISimulatedTest.cpp
)

# Test executable
add_executable(client-ui-test
    tests/ClientUITest.cpp
)

# Link dependencies
target_link_libraries(client-ui-module PRIVATE nlohmann_json::nlohmann_json)

target_link_libraries(client-ui-graphical-module PRIVATE
    nlohmann_json::nlohmann_json
    spdlog::spdlog
    imgui_opengl
    ${GLFW3_LIBRARIES}
    OpenGL::GL
)
target_include_directories(client-ui-graphical-module PRIVATE
    ${GLFW3_INCLUDE_DIRS}
    ../../../core/include
)

target_link_libraries(client-ui-standalone PRIVATE nlohmann_json::nlohmann_json)

target_link_libraries(client-ui-graphical PRIVATE
    nlohmann_json::nlohmann_json
    spdlog::spdlog
    imgui_opengl
    ${GLFW3_LIBRARIES}
    OpenGL::GL
)
target_include_directories(client-ui-graphical PRIVATE
    ${GLFW3_INCLUDE_DIRS}
    ../../../core/include
)

target_link_libraries(client-ui-simulated-module PRIVATE nlohmann_json::nlohmann_json)

target_link_libraries(client-ui-simulated PRIVATE nlohmann_json::nlohmann_json)

target_link_libraries(client-ui-test PRIVATE nlohmann_json::nlohmann_json)

message(STATUS "🎮 ClientUI Module configured:")
message(STATUS "   cmake .                    : Configure")
message(STATUS "   make client-ui-module      : Build client-ui.so")
message(STATUS "   make client-ui-test        : Run tests")
message(STATUS "   ImGui interface ready!")