cmake_minimum_required(VERSION 4.0)
project(siege_deck CXX)

set(CMAKE_CXX_STANDARD 26)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

if (WIN32)
    add_compile_definitions(PLATFORM_WINDOWS)
elseif (UNIX)
    add_compile_definitions(PLATFORM_LINUX)
endif ()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(Cpm)

# ---------------------------------------------------------------------------
# Dependencies
#
# Every entry below is a CPMAddPackage, so this tree configures unchanged on a
# machine with no nix (Windows, CI, a bare checkout) — CPM fetches and builds
# each one from the pinned tag. Inside the devShell, CPM_USE_LOCAL_PACKAGES=ON
# flips each call to try find_package first, and every one of these resolves
# against nixpkgs, so nothing is downloaded or rebuilt.
#
# That only holds because NAME is the find_package name (SDL3, not SDL) and
# VERSION matches what nixpkgs ships. Both paths are pinned to the SAME
# version deliberately: a dependency that differs between "on my machine" and
# "in the nix shell" is the exact bug this setup exists to avoid. Bumping one
# means bumping the other, or waiting for nixpkgs to catch up.
# ---------------------------------------------------------------------------

set(LIBS)

# Boost, for pretty type names via boost::typeindex (see Common.h).
#
# Boost::type_index only exists in the modular boost-cmake build CPM pulls;
# nixpkgs' b2-based install exposes only the aggregate Boost::headers. Since
# type_index is header-only, Boost::headers carries it in both worlds.
CPMAddPackage(
        NAME Boost
        VERSION 1.89.0
        URL "https://github.com/boostorg/boost/releases/download/boost-1.89.0/boost-1.89.0-cmake.tar.xz"
        FIND_PACKAGE_ARGUMENTS "CONFIG COMPONENTS headers")
list(APPEND LIBS Boost::headers)

# Logging.
CPMAddPackage(
        NAME spdlog
        VERSION 1.17.0
        GITHUB_REPOSITORY gabime/spdlog
        GIT_TAG v1.17.0
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS spdlog::spdlog)

# Windowing, input, 2D rendering.
CPMAddPackage(
        NAME SDL3
        VERSION 3.4.14
        GITHUB_REPOSITORY libsdl-org/SDL
        GIT_TAG release-3.4.14
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS SDL3::SDL3)

# Sprite loading (PNG, JPG, ...).
CPMAddPackage(
        NAME SDL3_image
        VERSION 3.4.4
        GITHUB_REPOSITORY libsdl-org/SDL_image
        GIT_TAG release-3.4.4
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS SDL3_image::SDL3_image)

# Text: gold, wave counters, tower costs, tooltips.
CPMAddPackage(
        NAME SDL3_ttf
        VERSION 3.2.2
        GITHUB_REPOSITORY libsdl-org/SDL_ttf
        GIT_TAG release-3.2.2
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS SDL3_ttf::SDL3_ttf)

# Audio.
CPMAddPackage(
        NAME SDL3_mixer
        VERSION 3.2.4
        GITHUB_REPOSITORY libsdl-org/SDL_mixer
        GIT_TAG release-3.2.4
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS SDL3_mixer::SDL3_mixer)

# Vector math.
CPMAddPackage(
        NAME glm
        VERSION 1.0.3
        GITHUB_REPOSITORY g-truc/glm
        GIT_TAG 1.0.3
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS glm::glm)

# ECS.
CPMAddPackage(
        NAME EnTT
        VERSION 3.16.0
        GITHUB_REPOSITORY skypjack/entt
        GIT_TAG v3.16.0
        FIND_PACKAGE_ARGUMENTS "CONFIG")
list(APPEND LIBS EnTT::EnTT)

# Debug/tools UI. Not a CPMAddPackage call because upstream ImGui ships no
# CMakeLists at all — see cmake/ImGui.cmake for what that costs.
include(ImGui)
list(APPEND LIBS imgui::imgui)

# Unit tests (siege_tests only, not in LIBS).
CPMAddPackage(
        NAME Catch2
        VERSION 3.15.3
        GITHUB_REPOSITORY catchorg/Catch2
        GIT_TAG v3.15.3
        FIND_PACKAGE_ARGUMENTS "CONFIG")

# ---------------------------------------------------------------------------
# Targets
#
# siege_engine is game-agnostic; siege_deck is the game on top of it. Engine code
# must never include anything from Source/Game (see Docs: Engine Overview).
# Both share Source/ as their include root, so the compiler can't stop that on
# its own; check_engine_deps does, on every build.
# ---------------------------------------------------------------------------

add_library(siege_engine STATIC
        Source/Common.h
        Source/Pch.h
        Source/Engine/Application.cpp
        Source/Engine/Application.h
        Source/Engine/Platform/SdlContext.cpp
        Source/Engine/Platform/SdlContext.h
        Source/Engine/Platform/Window.cpp
        Source/Engine/Platform/Window.h
        Source/Engine/Time/FrameClock.cpp
        Source/Engine/Time/FrameClock.h
        Source/Engine/Time/SimClock.cpp
        Source/Engine/Time/SimClock.h)

target_include_directories(siege_engine PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/Source)

# PUBLIC: engine headers expose SDL, glm and Common.h types, so the game needs them too.
target_link_libraries(siege_engine PUBLIC ${LIBS})

# PUBLIC so siege_deck builds its own PCH from the same header: engine headers
# rely on Common.h being there without including it.
target_precompile_headers(siege_engine PUBLIC Source/Pch.h)

add_custom_target(check_engine_deps
        COMMAND ${CMAKE_COMMAND} -DENGINE_DIR=${CMAKE_CURRENT_SOURCE_DIR}/Source/Engine
        -P ${CMAKE_CURRENT_SOURCE_DIR}/cmake/CheckEngineDeps.cmake
        COMMENT "Checking that Engine/ doesn't include Game/"
        VERBATIM)
add_dependencies(siege_engine check_engine_deps)

add_executable(${PROJECT_NAME}
        Source/Main.cpp)

target_link_libraries(${PROJECT_NAME} PRIVATE siege_engine)

# Assets are looked up relative to the executable, so the same path works from
# the build dir, from CLion's runner, and from an installed copy.
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_directory
        ${CMAKE_CURRENT_SOURCE_DIR}/Assets
        $<TARGET_FILE_DIR:${PROJECT_NAME}>/Assets)

# ---------------------------------------------------------------------------
# Tests
# ---------------------------------------------------------------------------

enable_testing()

# Boots the whole engine and runs 30 frames. The offscreen video driver needs no
# display, so this runs unattended in CI: ctest --preset debug
add_test(NAME smoke COMMAND ${PROJECT_NAME} --frames 30)
set_tests_properties(smoke PROPERTIES
        ENVIRONMENT "SDL_VIDEO_DRIVER=offscreen"
        TIMEOUT 30)

# Unit tests: engine (and later game) logic, no window. Each TEST_CASE shows up in
# ctest on its own, next to the smoke test.
add_executable(siege_tests
        Tests/Engine/AppConfigTests.cpp
        Tests/Engine/Time/FrameClockTests.cpp
        Tests/Engine/Time/SimClockTests.cpp)

target_link_libraries(siege_tests PRIVATE siege_engine Catch2::Catch2WithMain)

# catch_discover_tests lives in Catch.cmake: under extras/ in a source checkout
# (CPM download), next to Catch2Config.cmake in an installed one (nixpkgs).
if (Catch2_SOURCE_DIR)
    list(APPEND CMAKE_MODULE_PATH ${Catch2_SOURCE_DIR}/extras)
else ()
    list(APPEND CMAKE_MODULE_PATH ${Catch2_DIR})
endif ()
include(Catch)
catch_discover_tests(siege_tests)
