cmake_minimum_required(VERSION 3.20)
project(RemoveBGStudio LANGUAGES CXX)

if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
    message(FATAL_ERROR "RemoveBG Studio only ships a vendored x64 WebView2 loader. Configure for x64.")
endif()

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

option(RBG_CONSOLE "Build with a console window for debug logging" OFF)

set(THIRD_PARTY_DIR "${CMAKE_CURRENT_SOURCE_DIR}/third_party")

add_executable(RemoveBGStudio
    src/main.cpp
    src/WebViewHost.cpp
    src/SidecarProcess.cpp
    src/Dialogs.cpp
    src/PathUtil.cpp
)

if(RBG_CONSOLE)
    # Console subsystem still shows the window; stdout/stderr are visible for debugging.
    set_target_properties(RemoveBGStudio PROPERTIES WIN32_EXECUTABLE FALSE)
    target_compile_definitions(RemoveBGStudio PRIVATE RBG_CONSOLE=1)
else()
    set_target_properties(RemoveBGStudio PROPERTIES WIN32_EXECUTABLE TRUE)
endif()

target_include_directories(RemoveBGStudio PRIVATE
    "${THIRD_PARTY_DIR}/webview2/include"
    "${THIRD_PARTY_DIR}/json"
    "${CMAKE_CURRENT_SOURCE_DIR}/src"
)

target_compile_definitions(RemoveBGStudio PRIVATE
    UNICODE
    _UNICODE
    WIN32_LEAN_AND_MEAN
    NOMINMAX
)

target_link_libraries(RemoveBGStudio PRIVATE
    "${THIRD_PARTY_DIR}/webview2/lib/x64/WebView2LoaderStatic.lib"
    ole32
    oleaut32
    shlwapi
    shell32
    user32
    gdi32
    comctl32
    comdlg32
    advapi32
    version
)

target_compile_options(RemoveBGStudio PRIVATE /W4 /permissive-)

# WebView2LoaderStatic.lib is linked statically, so no WebView2Loader.dll is
# needed at runtime. Just stage the web UI next to the exe.
add_custom_command(TARGET RemoveBGStudio POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${CMAKE_CURRENT_SOURCE_DIR}/web"
        "$<TARGET_FILE_DIR:RemoveBGStudio>/web"
    COMMENT "Staging web/ assets next to RemoveBGStudio.exe"
)
