cmake_minimum_required(VERSION 3.16)
project(TicTacToe CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Emit build/compile_commands.json so clangd (Zed, VS Code, etc.) picks up the include roots and defines.
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Release by default: a Debug build of the API takes about 500 MB of disk space.
# Multi-config generators (Visual Studio, Xcode) pick the configuration at build time instead.
if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES)
  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
endif()

# --- SFS3 client API, built from source as a subproject -------------------
# The API folder sits next to the demo folders. Building it here, in the same pass,
# gives the right library names, system libraries and configuration on every platform.
# Override the location with -DSFS3_DIR=/path/to/SFS3_API_Cpp if it moves.
set(SFS3_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../SFS3_API_Cpp"
    CACHE PATH "SFS3 C++ API root")
get_filename_component(SFS3_DIR "${SFS3_DIR}" ABSOLUTE)

if(NOT EXISTS "${SFS3_DIR}/src/CMakeLists.txt")
  message(FATAL_ERROR
    "SFS3 C++ API not found in ${SFS3_DIR}. Place it next to this folder, or set -DSFS3_DIR=...")
endif()

# A development checkout also contains the API's test harness: never build it here
set(SFS_BUILD_TESTS OFF CACHE BOOL "" FORCE)
add_subdirectory("${SFS3_DIR}/src" "${CMAKE_BINARY_DIR}/sfs3")

# --- raylib -----------------------------------------------------------------
# Use an installed raylib (Homebrew, vcpkg, a Linux package...) when there is one.
# Otherwise download a pinned release and build it from source, in this build folder.
# Force the download with -DSFS3_FETCH_RAYLIB=ON.
option(SFS3_FETCH_RAYLIB "Always download and build raylib, ignoring any installed copy" OFF)

# Let find_package see Homebrew on Apple Silicon (not a default CMake prefix).
if(APPLE AND EXISTS /opt/homebrew)
  list(APPEND CMAKE_PREFIX_PATH /opt/homebrew)
endif()

if(NOT SFS3_FETCH_RAYLIB)
  find_package(raylib 5.0 QUIET)

  # raylib accepts any newer version as compatible: allow only the 5.x series the examples are tested with
  if(raylib_FOUND AND NOT raylib_VERSION_MAJOR EQUAL 5)
    message(FATAL_ERROR
      "Found raylib ${raylib_VERSION}, but the examples are tested with raylib 5.x. "
      "Run again with -DSFS3_FETCH_RAYLIB=ON to download raylib 5.5.")
  endif()
endif()

if(NOT raylib_FOUND)
  message(STATUS "raylib: downloading raylib 5.5")

  # Extracted files get the extraction time, so a new download always triggers a rebuild
  if(POLICY CMP0135)
    cmake_policy(SET CMP0135 NEW)
  endif()

  include(FetchContent)
  FetchContent_Declare(raylib
    URL https://github.com/raysan5/raylib/archive/refs/tags/5.5.tar.gz)
  set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)

  # raylib 5.5 asks for CMake 3.5, which CMake 4 flags as deprecated: raise its policy level
  set(CMAKE_POLICY_VERSION_MINIMUM 3.10)
  FetchContent_MakeAvailable(raylib)
  unset(CMAKE_POLICY_VERSION_MINIMUM)
endif()

# --- The example app ------------------------------------------------------
# CONFIGURE_DEPENDS re-checks the glob at build time, so new *.cpp files are picked up
# without a manual reconfigure. Headers are listed for IDE/clangd visibility only.
file(GLOB TTT_SOURCES CONFIGURE_DEPENDS
  "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
  "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h")
add_executable(TicTacToe ${TTT_SOURCES})
target_link_libraries(TicTacToe PRIVATE sfs_client raylib)

# Same output folder for every configuration, so multi-config generators don't add bin/Debug etc.
set_target_properties(TicTacToe PROPERTIES
  RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/bin"
  RUNTIME_OUTPUT_DIRECTORY_DEBUG "${CMAKE_CURRENT_SOURCE_DIR}/bin"
  RUNTIME_OUTPUT_DIRECTORY_RELEASE "${CMAKE_CURRENT_SOURCE_DIR}/bin"
  RUNTIME_OUTPUT_DIRECTORY_RELWITHDEBINFO "${CMAKE_CURRENT_SOURCE_DIR}/bin"
  RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_CURRENT_SOURCE_DIR}/bin")

# The app loads its resources relative to its own folder: copy them next to it after each build
add_custom_command(TARGET TicTacToe POST_BUILD
  COMMAND ${CMAKE_COMMAND} -E copy_directory
          "${CMAKE_CURRENT_SOURCE_DIR}/resources"
          "$<TARGET_FILE_DIR:TicTacToe>/resources")
