cmake_minimum_required(VERSION 3.20)
project(wrp_cae_core VERSION 1.0.0)

#------------------------------------------------------------------------------
# ChiMod utilities and dependencies
#------------------------------------------------------------------------------
# ChiMod utilities are now provided by IowarpCoreCommon.cmake in /workspace/cmake
# All find_package calls are handled by root CMakeLists.txt
# Targets chimaera::cxx, wrp_cte::core_client, etc. are already available

# All required packages (chimaera, wrp_cte_core, yaml-cpp) are already found
# by IowarpCoreCommon.cmake in the root CMakeLists.txt

#------------------------------------------------------------------------------
# Optional HDF5 support (found by root CMakeLists.txt)
#------------------------------------------------------------------------------
if(WRP_CAE_ENABLE_HDF5)
  # HDF5 is already found by root CMakeLists.txt

  if(HDF5_FOUND)
    message(STATUS "CAE HDF5 support: ENABLED")
    add_compile_definitions(WRP_CAE_ENABLE_HDF5)
    set(HDF5_ENABLED TRUE)
  else()
    message(WARNING "WRP_CAE_ENABLE_HDF5 is ON but HDF5 package not found")
    message(WARNING "HDF5 support will be disabled. Install HDF5 to enable.")
    set(HDF5_ENABLED FALSE)
    set(WRP_CAE_ENABLE_HDF5 OFF PARENT_SCOPE)
  endif()
else()
  message(STATUS "CAE HDF5 support: DISABLED")
  set(HDF5_ENABLED FALSE)
endif()

#------------------------------------------------------------------------------
# Optional Globus support (found by root CMakeLists.txt)
#------------------------------------------------------------------------------
if(CAE_ENABLE_GLOBUS)
  # Poco and nlohmann_json are already found by root CMakeLists.txt

  if(Poco_FOUND AND nlohmann_json_FOUND)
    message(STATUS "CAE Globus support: ENABLED")
    add_compile_definitions(CAE_ENABLE_GLOBUS)
    set(GLOBUS_ENABLED TRUE)
  else()
    message(WARNING "CAE_ENABLE_GLOBUS is ON but required dependencies not found:")
    if(NOT Poco_FOUND)
      message(WARNING "  - POCO library (Net, NetSSL, Crypto, JSON components) not found")
    endif()
    if(NOT nlohmann_json_FOUND)
      message(WARNING "  - nlohmann_json library not found")
    endif()
    message(WARNING "Globus support will be disabled. Install dependencies to enable.")
    set(GLOBUS_ENABLED FALSE)
    set(CAE_ENABLE_GLOBUS OFF)
  endif()
else()
  message(STATUS "CAE Globus support: DISABLED")
  set(GLOBUS_ENABLED FALSE)
endif()

#------------------------------------------------------------------------------
# Client Library
#------------------------------------------------------------------------------
add_chimod_client(
  SOURCES
    src/core_client.cc
  LINK_LIBRARIES
    wrp_cte_core_client
)

#------------------------------------------------------------------------------
# Runtime Library
#------------------------------------------------------------------------------
set(RUNTIME_SOURCES
  src/core_runtime.cc
  src/factory/assimilator_factory.cc
  src/factory/binary_file_assimilator.cc
  src/autogen/core_lib_exec.cc  # Auto-generated by chi_refresh_repo
)

set(RUNTIME_LINK_LIBRARIES
  wrp_cte_core_client
)

# Conditionally add HDF5 assimilator if enabled
if(HDF5_ENABLED)
  list(APPEND RUNTIME_SOURCES src/factory/hdf5_file_assimilator.cc)
  list(APPEND RUNTIME_LINK_LIBRARIES ${HDF5_LIBRARIES})
endif()

# Conditionally add Globus assimilator if enabled
if(GLOBUS_ENABLED)
  list(APPEND RUNTIME_SOURCES src/factory/globus_file_assimilator.cc)
  list(APPEND RUNTIME_LINK_LIBRARIES Poco::Net Poco::NetSSL Poco::Crypto Poco::JSON nlohmann_json::nlohmann_json)
endif()

add_chimod_runtime(
  SOURCES
    ${RUNTIME_SOURCES}
  LINK_LIBRARIES
    ${RUNTIME_LINK_LIBRARIES}
)

# Add HDF5 include directories to runtime if enabled
if(HDF5_ENABLED)
  target_include_directories(wrp_cae_core_runtime PRIVATE ${HDF5_INCLUDE_DIRS})
endif()

#------------------------------------------------------------------------------
# Utilities
#------------------------------------------------------------------------------

# wrp_cae_omni utility
add_executable(wrp_cae_omni util/wrp_cae_omni.cc)
target_link_libraries(wrp_cae_omni
  PRIVATE
    wrp_cae_core_client
    chimaera::cxx
    hshm::configure
)
install(TARGETS wrp_cae_omni DESTINATION bin)

#------------------------------------------------------------------------------
# Tests
#------------------------------------------------------------------------------
# Enable CTest support
include(CTest)

# Add test subdirectory (contains unit tests with CTest integration)
if(BUILD_TESTING)
  add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../test ${CMAKE_CURRENT_BINARY_DIR}/test)

  # Install common test configuration
  install(FILES
    ${CMAKE_CURRENT_SOURCE_DIR}/../test/unit/wrp_config.yaml
    ${CMAKE_CURRENT_SOURCE_DIR}/../test/unit/README.md
    DESTINATION share/wrp_cae/test
  )
endif()
