# The core: the domain, the ports it owns, and the use cases. Nothing in
# the core links an adapter.
add_library(arch_hex_domain STATIC core/domain/course.cpp)
target_include_directories(arch_hex_domain PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/core/domain)

# The driving port: header only, and free of domain types.
add_library(arch_hex_driving_port INTERFACE)
target_include_directories(arch_hex_driving_port INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/core/driving)

# The driven ports: header only; they name domain types.
add_library(arch_hex_driven_ports INTERFACE)
target_include_directories(arch_hex_driven_ports INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/core/driven)
target_link_libraries(arch_hex_driven_ports INTERFACE arch_hex_domain)

add_library(arch_hex_core STATIC core/service/enrollment_service.cpp)
target_include_directories(arch_hex_core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/core/service)
target_link_libraries(arch_hex_core PUBLIC arch_hex_driving_port arch_hex_driven_ports)

# Adapters: one library each. A driven adapter sees only the driven
# ports; a driving adapter sees only the driving port.
add_library(arch_hex_memory STATIC adapters/memory/memory_courses.cpp adapters/memory/memory_students.cpp)
target_include_directories(arch_hex_memory PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/adapters/memory)
target_link_libraries(arch_hex_memory PUBLIC arch_hex_driven_ports)

add_library(arch_hex_stream STATIC adapters/stream/stream_notifier.cpp)
target_include_directories(arch_hex_stream PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/adapters/stream)
target_link_libraries(arch_hex_stream PUBLIC arch_hex_driven_ports)

add_library(arch_hex_console STATIC adapters/console/console.cpp)
target_include_directories(arch_hex_console PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/adapters/console)
target_link_libraries(arch_hex_console PUBLIC arch_hex_driving_port)

add_library(arch_hex_cli STATIC adapters/cli/cli.cpp)
target_include_directories(arch_hex_cli PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/adapters/cli)
target_link_libraries(arch_hex_cli PUBLIC arch_hex_driving_port)

# main is the composition root: it picks one adapter for each port.
lld_add_example(arch_enrollment_hexagonal SOURCES main.cpp EXPECTED_OUTPUT expected_output.txt)
target_link_libraries(arch_enrollment_hexagonal PRIVATE
  arch_hex_console arch_hex_core arch_hex_memory arch_hex_stream)

lld_add_test(arch_hex_core_tests SOURCES tests/test_core.cpp)
target_link_libraries(arch_hex_core_tests PRIVATE arch_hex_core arch_hex_memory)

lld_add_test(arch_hex_course_store_contract SOURCES tests/test_course_store_contract.cpp)
target_link_libraries(arch_hex_course_store_contract PRIVATE arch_hex_memory)

lld_add_test(arch_hex_console_tests SOURCES tests/test_console.cpp)
target_link_libraries(arch_hex_console_tests PRIVATE arch_hex_console)

lld_add_test(arch_hex_cli_tests SOURCES tests/test_cli.cpp)
target_link_libraries(arch_hex_cli_tests PRIVATE arch_hex_cli arch_hex_core arch_hex_memory arch_hex_stream)

# A file that includes the domain header, built with only the driving
# port, as a driving adapter is. It is not part of the normal build; the
# test builds it and expects the compiler to report a missing header.
add_executable(arch_hex_driving_adapter_violation EXCLUDE_FROM_ALL checks/driving_adapter_uses_domain.cpp)
target_link_libraries(arch_hex_driving_adapter_violation PRIVATE arch_hex_driving_port)
add_test(NAME arch_hex_driving_adapter_cannot_see_domain
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target arch_hex_driving_adapter_violation)
set_tests_properties(arch_hex_driving_adapter_cannot_see_domain PROPERTIES
  PASS_REGULAR_EXPRESSION "course.hpp.*(No such file or directory|file not found)"
  LABELS "test")
