# --- Library: dofs_switch ---
add_library(dofs_switch STATIC)

target_include_directories(dofs_switch
  PUBLIC
    $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src>          # "network/...", "core/..."
    $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src/network>  # **enables "switch/..." **
    $<INSTALL_INTERFACE:include>
)

# Sources
if(DOFS_GLOB_SOURCES)
  file(GLOB SWITCH_CC CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.cc")
  target_sources(dofs_switch PRIVATE ${SWITCH_CC})
else()
  target_sources(dofs_switch
    PUBLIC
      routing_alg.h
      routing_tables.h
      unicast_table.h
      multicast_table.h
      switch_buffer.h
      shared_buffer.h
      dedicated_buffer.h
      ecn_engine.h
      ecn_shared_red.h
      ecn_dedicated_red.h
    PRIVATE
      routing_alg.cc
      unicast_table.cc
      multicast_table.cc
      switch_buffer.cc
      shared_buffer.cc
      dedicated_buffer.cc
      ecn_shared_red.cc
      ecn_dedicated_red.cc
  )
endif()

target_link_libraries(dofs_switch PUBLIC dofs_network dofs_core)
add_library(dofs::switch ALIAS dofs_switch)

# --- Tooling stubs for switch headers ---
file(GLOB SWITCH_HEADERS CONFIGURE_DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/*.h")

set(SWITCH_STUBS)
foreach(h ${SWITCH_HEADERS})
  get_filename_component(_base "${h}" NAME_WE)
  set(_stub "${CMAKE_CURRENT_BINARY_DIR}/switch_${_base}.cpp")
  file(WRITE "${_stub}" "// stub switch/${_base}.h\n#include \"switch/${_base}.h\"\n")
  list(APPEND SWITCH_STUBS "${_stub}")
endforeach()

add_library(dofs_switch_headers_tooling STATIC ${SWITCH_STUBS})

# Make sure stubs see the SAME include roots
target_include_directories(dofs_switch_headers_tooling
  PRIVATE
    $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src>
    $<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}/src/network>  # **not src/network/switch **
)

target_link_libraries(dofs_switch_headers_tooling PRIVATE dofs_switch dofs_network dofs_core)
