# One library per ring, or per part of a ring, so that each part can
# include only what the dependency rule allows.

# Entities: the innermost ring. It links nothing.
add_library(arch_clean_entities STATIC entities/course.cpp)
target_include_directories(arch_clean_entities PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/entities)

# Use cases. The boundary (request and response models, input
# boundaries) names no entity; the gateways do; the interactors use both.
add_library(arch_clean_boundary INTERFACE)
target_include_directories(arch_clean_boundary INTERFACE
  ${CMAKE_CURRENT_SOURCE_DIR}/use_cases/boundary)

add_library(arch_clean_gateways INTERFACE)
target_include_directories(arch_clean_gateways INTERFACE
  ${CMAKE_CURRENT_SOURCE_DIR}/use_cases/gateways)
target_link_libraries(arch_clean_gateways INTERFACE arch_clean_entities)

add_library(arch_clean_interactors STATIC
  use_cases/interactors/add_course.cpp use_cases/interactors/add_student.cpp
  use_cases/interactors/enroll_student.cpp use_cases/interactors/drop_student.cpp
  use_cases/interactors/report_course.cpp)
target_include_directories(arch_clean_interactors PUBLIC
  ${CMAKE_CURRENT_SOURCE_DIR}/use_cases/interactors)
target_link_libraries(arch_clean_interactors PUBLIC arch_clean_boundary arch_clean_gateways)

# Interface adapters. The console sees only the boundary; the gateway
# adapters see only the gateways.
add_library(arch_clean_console STATIC
  interface_adapters/console/console_controller.cpp
  interface_adapters/console/console_presenter.cpp)
target_include_directories(arch_clean_console PUBLIC
  ${CMAKE_CURRENT_SOURCE_DIR}/interface_adapters/console)
target_link_libraries(arch_clean_console PUBLIC arch_clean_boundary)

add_library(arch_clean_gateway_adapters STATIC
  interface_adapters/memory/memory_gateways.cpp interface_adapters/stream/stream_notifier.cpp)
target_include_directories(arch_clean_gateway_adapters PUBLIC
  ${CMAKE_CURRENT_SOURCE_DIR}/interface_adapters/memory
  ${CMAKE_CURRENT_SOURCE_DIR}/interface_adapters/stream)
target_link_libraries(arch_clean_gateway_adapters PUBLIC arch_clean_gateways)

# Frameworks and drivers: main, the outermost ring, links everything.
lld_add_example(arch_enrollment_clean SOURCES main.cpp EXPECTED_OUTPUT expected_output.txt)
target_link_libraries(arch_enrollment_clean PRIVATE
  arch_clean_console arch_clean_interactors arch_clean_gateway_adapters)

lld_add_test(arch_clean_interactor_tests SOURCES tests/test_interactors.cpp)
target_link_libraries(arch_clean_interactor_tests PRIVATE
  arch_clean_interactors arch_clean_gateway_adapters)

lld_add_test(arch_clean_presenter_tests SOURCES tests/test_presenter.cpp)
target_link_libraries(arch_clean_presenter_tests PRIVATE arch_clean_console)

lld_add_test(arch_clean_controller_tests SOURCES tests/test_controller.cpp)
target_link_libraries(arch_clean_controller_tests PRIVATE arch_clean_console)

# The push-style variant: only its test builds it.
lld_add_test(arch_clean_output_boundary_tests
  SOURCES tests/test_output_boundary.cpp variants/presenting_enroll.cpp)
target_include_directories(arch_clean_output_boundary_tests PRIVATE
  ${CMAKE_CURRENT_SOURCE_DIR}/variants)
target_link_libraries(arch_clean_output_boundary_tests PRIVATE
  arch_clean_console arch_clean_interactors arch_clean_gateway_adapters)

# The dependency rule, checked: console code that includes an entity
# header, built with exactly what the console library provides, must
# fail to compile. If the console library ever links the entities, the
# file compiles and this test fails.
add_executable(arch_clean_console_violation EXCLUDE_FROM_ALL checks/presenter_uses_entities.cpp)
target_link_libraries(arch_clean_console_violation PRIVATE arch_clean_console)
add_test(NAME arch_clean_console_cannot_see_entities
  COMMAND ${CMAKE_COMMAND} --build ${CMAKE_BINARY_DIR} --target arch_clean_console_violation)
set_tests_properties(arch_clean_console_cannot_see_entities PROPERTIES
  PASS_REGULAR_EXPRESSION "course.hpp.*(No such file or directory|file not found)"
  LABELS "test")
