cmake_minimum_required(VERSION 3.20)
project(wrp_cae_core VERSION 1.0.0)

#------------------------------------------------------------------------------
# Find Chimaera package
#------------------------------------------------------------------------------
# Skip find_package if targets already exist (unified build as subdirectory)
if(NOT TARGET chimaera::cxx AND NOT TARGET chimaera_cxx)
  find_package(chimaera REQUIRED)
  # Add Chimaera CMake modules to the module path
  list(APPEND CMAKE_MODULE_PATH "${chimaera_DIR}")
  include(ChimaeraCommon)
  message(STATUS "Found Chimaera package")
else()
  # When built as subdirectory, manually include ChimaeraCommon.cmake
  message(STATUS "Using Chimaera as subdirectory - including ChimaeraCommon.cmake")
  list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../context-runtime/cmake")
  include(ChimaeraCommon)
endif()

#------------------------------------------------------------------------------
# Find wrp_cte package
#------------------------------------------------------------------------------
# Skip find_package if targets already exist (unified build as subdirectory)
if(NOT TARGET wrp_cte::core_client AND NOT TARGET wrp_cte_core_client)
  find_package(wrp_cte_core REQUIRED)
  message(STATUS "Found wrp_cte_core package")
else()
  message(STATUS "Using wrp_cte_core as subdirectory")
endif()

#------------------------------------------------------------------------------
# Find yaml-cpp package (required for OMNI file parsing)
#------------------------------------------------------------------------------
# Skip find_package if target already exists (from submodule)
if(NOT TARGET yaml-cpp AND NOT TARGET yaml-cpp::yaml-cpp)
  find_package(yaml-cpp REQUIRED)
endif()

#------------------------------------------------------------------------------
# Optional HDF5 support (controlled by root CMakeLists.txt)
#------------------------------------------------------------------------------
if(CAE_ENABLE_HDF5)
  find_package(HDF5 COMPONENTS C)

  if(HDF5_FOUND)
    message(STATUS "CAE HDF5 support: ENABLED")
    add_compile_definitions(CAE_ENABLE_HDF5)
    set(HDF5_ENABLED TRUE)
  else()
    message(WARNING "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(CAE_ENABLE_HDF5 OFF)
  endif()
else()
  message(STATUS "CAE HDF5 support: DISABLED")
  set(HDF5_ENABLED FALSE)
endif()

#------------------------------------------------------------------------------
# Optional Globus support (controlled by root CMakeLists.txt)
#------------------------------------------------------------------------------
if(CAE_ENABLE_GLOBUS)
  # Find POCO libraries for Globus HTTP requests
  find_package(Poco COMPONENTS Net NetSSL Crypto JSON)

  # Find nlohmann_json for JSON parsing
  find_package(nlohmann_json)

  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
)

#------------------------------------------------------------------------------
# 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
    yaml-cpp
)
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()
