if(NOT ACCELNET)
  return()
endif()

find_package(Python3 REQUIRED COMPONENTS Interpreter)
if(ACCELNET_PROVIDER STREQUAL "FETCH")
  include(FetchContent)
  FetchContent_GetProperties(accelnet)
  add_executable(pimd_accelnet_fixtures
    fixtures.f90
    "${accelnet_SOURCE_DIR}/AccelNetModelConverter/fortran/model_conversion.f90")
  target_link_libraries(pimd_accelnet_fixtures PRIVATE AccelNet::AccelNet)
  set_target_properties(pimd_accelnet_fixtures PROPERTIES
    Fortran_MODULE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/modules")
  target_include_directories(pimd_accelnet_fixtures PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/modules")
  set(_fixtures "$<TARGET_FILE:pimd_accelnet_fixtures>")
  set(_model "${accelnet_SOURCE_DIR}/AccelNetPredictor/test/data/n2p2-virial-angular")
elseif(ACCELNET_TEST_FIXTURES AND ACCELNET_TEST_MODEL_DIR)
  if(NOT EXISTS "${ACCELNET_TEST_FIXTURES}" OR NOT EXISTS "${ACCELNET_TEST_MODEL_DIR}/input.nn")
    message(FATAL_ERROR "ACCELNET_TEST_FIXTURES / ACCELNET_TEST_MODEL_DIR do not exist")
  endif()
  # A separate CPU build supplies the oracle generator. Do not compile this
  # Fortran-native utility against an external compiler's .mod files.
  set(_fixtures "${ACCELNET_TEST_FIXTURES}")
  set(_model "${ACCELNET_TEST_MODEL_DIR}")
elseif(ACCELNET_TEST_FIXTURES OR ACCELNET_TEST_MODEL_DIR)
  message(FATAL_ERROR "Set both ACCELNET_TEST_FIXTURES and ACCELNET_TEST_MODEL_DIR")
else()
  message(STATUS "External AccelNet: synthetic tests need ACCELNET_TEST_FIXTURES and ACCELNET_TEST_MODEL_DIR; trained-model comparisons remain available with AENET/N2P2")
endif()

set(_devices CPU)
if(ACCELNET_TEST_GPU)
  if(ACCELNET_PROVIDER STREQUAL "FETCH")
    message(FATAL_ERROR "ACCELNET_TEST_GPU requires an EXTERNAL offload-enabled AccelNetC installation")
  endif()
  list(APPEND _devices GPU)
endif()
foreach(_device IN LISTS _devices)
  set(_prefix accelnet)
  set(_labels "accelnet;cpu")
  if(_device STREQUAL "GPU")
    set(_prefix accelnet.gpu)
    set(_labels "accelnet;gpu")
  endif()
  if(_fixtures)
    set(_accelnet_groups loading modes derivatives mpi)
    if(AENET)
      list(APPEND _accelnet_groups reference_aenet)
    endif()
    if(N2P2)
      list(APPEND _accelnet_groups reference_n2p2)
    endif()
    foreach(_group IN LISTS _accelnet_groups)
      add_test(NAME ${_prefix}.${_group}
        COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/integration.py"
          --pimd "$<TARGET_FILE:pimd.mpi.x>"
          --fixtures "${_fixtures}"
          --defaults "${PROJECT_SOURCE_DIR}/examples/input_default.dat"
          --model "${_model}"
          --mpiexec "${MPIEXEC_EXECUTABLE}"
          --numproc-flag=${MPIEXEC_NUMPROC_FLAG}
          --accelnet-backend "${_device}" --accelnet-device "${ACCELNET_TEST_DEVICE}"
          --work "${CMAKE_CURRENT_BINARY_DIR}/runs" --group "${_group}")
      set_tests_properties(${_prefix}.${_group} PROPERTIES
        TIMEOUT 600 PROCESSORS 4 LABELS "validation;${_labels};${_group}")
      if(_group STREQUAL "mpi")
        set_tests_properties(${_prefix}.${_group} PROPERTIES PROCESSORS 8)
      endif()
      if(_group MATCHES "^reference_")
        set_property(TEST ${_prefix}.${_group} APPEND PROPERTY LABELS reference)
      endif()
      if(_device STREQUAL "GPU")
        set_tests_properties(${_prefix}.${_group} PROPERTIES RESOURCE_LOCK accelnet_gpu
          ENVIRONMENT "OMP_TARGET_OFFLOAD=MANDATORY")
      endif()
    endforeach()
  endif()

  # Distributed trained models also test native binary aenet networks.
  foreach(_backend IN ITEMS aenet n2p2)
    string(TOUPPER "${_backend}" _backend_option)
    if(${_backend_option})
      add_test(NAME ${_prefix}.examples_${_backend}
        COMMAND "${Python3_EXECUTABLE}"
          "${PROJECT_SOURCE_DIR}/tests/external/AccelNet/compare.py"
          --pimd "$<TARGET_FILE:pimd.mpi.x>"
          --mpiexec "${MPIEXEC_EXECUTABLE}"
          --numproc-flag=${MPIEXEC_NUMPROC_FLAG}
          --accelnet-backend "${_device}" --accelnet-device "${ACCELNET_TEST_DEVICE}"
          --work "${CMAKE_CURRENT_BINARY_DIR}/real-models"
          --family "${_backend}")
      set_tests_properties(${_prefix}.examples_${_backend} PROPERTIES
        TIMEOUT 900 PROCESSORS 4 LABELS "validation;${_labels};reference;examples;${_backend}")
      if(_device STREQUAL "GPU")
        set_tests_properties(${_prefix}.examples_${_backend} PROPERTIES RESOURCE_LOCK accelnet_gpu
          ENVIRONMENT "OMP_TARGET_OFFLOAD=MANDATORY")
      endif()
    endif()
  endforeach()
endforeach()
