cmake_minimum_required(VERSION 3.20)
cmake_policy(SET CMP0091 NEW)
# Statically link the MSVC runtime so the end-user installer does not depend
# on a separately installed Visual C++ Redistributable.
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
project(NexusPCControl VERSION 1.6.1 LANGUAGES CXX RC)

# GNU windres passes RC include directories through an intermediate
# preprocessor command line. Paths with spaces can be split there. Generated
# resources below use quoted absolute asset paths, so omit <INCLUDES> for RC.
if(MINGW)
    set(CMAKE_RC_COMPILE_OBJECT
        "<CMAKE_RC_COMPILER> -O coff <DEFINES> <FLAGS> <SOURCE> <OBJECT>")
endif()

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

option(NEXUS_ENABLE_TEST_CLI "Enable test-only command-line hooks (never use in release builds)" OFF)
option(NEXUS_BUILD_TEST_TOOLS "Build local E2E test tools" OFF)

add_executable(NexusPCControl WIN32
    src/main.cpp
    src/app.cpp
    src/util.cpp
    src/log.cpp
    src/json.cpp
    src/config.cpp
    src/tgapi.cpp
    src/bot.cpp
    src/sysmon.cpp
    src/sysproc.cpp
    src/syspower.cpp
    src/screenshot.cpp
    src/wol.cpp
    src/ui/d2d.cpp
    src/ui/window.cpp
)

set(NEXUS_RESOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/src/res")
file(TO_CMAKE_PATH "${NEXUS_RESOURCE_DIR}" NEXUS_RESOURCE_DIR)
set(NEXUS_GENERATED_RESOURCE_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated-resources")
file(MAKE_DIRECTORY "${NEXUS_GENERATED_RESOURCE_DIR}")
configure_file(src/res/app.rc.in "${NEXUS_GENERATED_RESOURCE_DIR}/app.rc" @ONLY)
target_sources(NexusPCControl PRIVATE "${NEXUS_GENERATED_RESOURCE_DIR}/app.rc")

target_include_directories(NexusPCControl PRIVATE src)
target_compile_definitions(NexusPCControl PRIVATE UNICODE _UNICODE NOMINMAX WINVER=0x0A00 _WIN32_WINNT=0x0A00)
if(NEXUS_ENABLE_TEST_CLI)
    target_compile_definitions(NexusPCControl PRIVATE NEXUS_ENABLE_TEST_CLI=1)
endif()

# Windows system libraries are required by both MSVC and MinGW. Do not hide
# this list inside the MinGW branch: otherwise a Visual Studio build links no
# Win32/Direct2D/WinHTTP libraries and fails at the final link step.
target_link_libraries(NexusPCControl PRIVATE
    user32 gdi32 shell32 advapi32 ole32 oleaut32 uuid
    winhttp crypt32 ws2_32 psapi d2d1 dwrite windowscodecs comctl32 bcrypt
)

if(MINGW)
    target_compile_options(NexusPCControl PRIVATE
        -municode -Wall -Wno-unknown-pragmas -Wno-unused-function
    )
    target_link_options(NexusPCControl PRIVATE
        -municode -static -static-libgcc -static-libstdc++
        # Do not embed MinGW's default manifest (our .rc supplies the manifest)
        "-specs=${CMAKE_SOURCE_DIR}/cmake/no-default-manifest.spec"
    )
elseif(MSVC)
    target_compile_options(NexusPCControl PRIVATE /utf-8 /W4 /permissive- /EHsc)
endif()

# Release optimizations
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()
set_target_properties(NexusPCControl PROPERTIES
    RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin"
)
foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
    set_target_properties(NexusPCControl PROPERTIES
        RUNTIME_OUTPUT_DIRECTORY_${config} "${CMAKE_BINARY_DIR}/bin")
endforeach()


# Local Telegram Bot API mock: development/E2E only, not part of installer.
if(NEXUS_BUILD_TEST_TOOLS)
    add_executable(mocktg tests/mocktg.cpp)
    target_compile_features(mocktg PRIVATE cxx_std_17)
    target_link_libraries(mocktg PRIVATE ws2_32)
    set_target_properties(mocktg PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
    foreach(config DEBUG RELEASE RELWITHDEBINFO MINSIZEREL)
        set_target_properties(mocktg PROPERTIES
            RUNTIME_OUTPUT_DIRECTORY_${config} "${CMAKE_BINARY_DIR}/bin")
    endforeach()
    if(MINGW)
        target_compile_options(mocktg PRIVATE -Wall -Wextra -Wpedantic)
        target_link_options(mocktg PRIVATE -static -static-libgcc -static-libstdc++)
    endif()
endif()
