cmake_minimum_required(VERSION 3.20)
project(world-generation VERSION 1.0.0)

# C++ Standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Defensive programming by default
set(CMAKE_BUILD_TYPE Debug CACHE STRING "Build type" FORCE)

# AddressSanitizer and UndefinedBehaviorSanitizer by default
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -fsanitize=address -fsanitize=undefined -fno-omit-frame-pointer -g")
set(CMAKE_LINKER_FLAGS_DEBUG "${CMAKE_LINKER_FLAGS_DEBUG} -fsanitize=address -fsanitize=undefined")

# FetchContent for dependencies
include(FetchContent)

FetchContent_Declare(
    nlohmann_json
    GIT_REPOSITORY https://github.com/nlohmann/json.git
    GIT_TAG v3.11.2
)
FetchContent_MakeAvailable(nlohmann_json)

# Include directories
target_include_directories(world-generation PRIVATE include)
target_include_directories(world-generation PRIVATE ../../core/include)

# Source files
set(SOURCES
    src/WorldGenerationModule.cpp
    src/main.cpp
)

# Headers
set(HEADERS
    include/WorldGeneration.h
    include/GMap.h
)

# Create shared library (.so) for hot-reload
add_library(world-generation SHARED ${SOURCES} ${HEADERS})

# Link libraries
target_link_libraries(world-generation PRIVATE nlohmann_json::nlohmann_json)

# Create standalone executable for testing
add_executable(world-generation-test ${SOURCES})
target_link_libraries(world-generation-test PRIVATE nlohmann_json::nlohmann_json)

# Include directories for test executable
target_include_directories(world-generation-test PRIVATE include)
target_include_directories(world-generation-test PRIVATE ../../core/include)

# Compile definitions for testing
target_compile_definitions(world-generation-test PRIVATE TESTING)

# Build directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)

# Install targets
install(TARGETS world-generation DESTINATION lib)
install(FILES ${HEADERS} DESTINATION include/world-generation)

# Apply defensive programming (if available)
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/../../../cmake/WarfactoryDefenses.cmake")
    include(../../../cmake/WarfactoryDefenses.cmake)
    apply_warfactory_defenses(world-generation)
    apply_warfactory_defenses(world-generation-test)
endif()