cmake_minimum_required(VERSION 3.20)
project(linux_ticket_vertical LANGUAGES C CXX)

find_package(Python3 REQUIRED COMPONENTS Interpreter)
add_test(NAME ticket_negative_stamp
    COMMAND "${Python3_EXECUTABLE}" "${CMAKE_CURRENT_LIST_DIR}/test_negative_stamp.py")

option(F02_BUILD_TICKET_VERTICAL "Build actual Worker ticket ingress/cold sender companion" OFF)
if(F02_BUILD_TICKET_VERTICAL)
    if(NOT TARGET linux_ratchet_runtime)
        message(FATAL_ERROR "Ticket vertical requires the one coordinated linux_ratchet_runtime")
    endif()
    # Reuse only the existing physical-radio/command fixture and passive prepared
    # capture. All protocol, provider, owner and transport code comes from the
    # single production archive; no component-only replacement is linked here.
    set(FIXTURE "${CMAKE_CURRENT_LIST_DIR}/../linux_delivery_postage")
    get_target_property(RUNTIME_INCLUDES linux_ratchet_runtime INCLUDE_DIRECTORIES)
    if(NOT RUNTIME_INCLUDES)
        message(FATAL_ERROR "Shared runtime include closure is unavailable")
    endif()
    set(SYMBOL_INCLUDES)
    foreach(path IN LISTS RUNTIME_INCLUDES)
        list(APPEND SYMBOL_INCLUDES "-I${path}")
    endforeach()
    execute_process(COMMAND "${CMAKE_CXX_COMPILER}" -std=c++20 ${SYMBOL_INCLUDES}
        -c "${FIXTURE}/prepared_symbol.cpp" -o "${CMAKE_CURRENT_BINARY_DIR}/prepared_symbol.o"
        COMMAND_ERROR_IS_FATAL ANY)
    execute_process(COMMAND "${CMAKE_NM}" --undefined-only "${CMAKE_CURRENT_BINARY_DIR}/prepared_symbol.o"
        OUTPUT_VARIABLE PREPARED_SYMBOLS COMMAND_ERROR_IS_FATAL ANY)
    string(REGEX MATCH "[^ \r\n]+encodePropagatedEnvelope[^ \r\n]+" PREPARED_SYMBOL "${PREPARED_SYMBOLS}")
    if(NOT PREPARED_SYMBOL)
        message(FATAL_ERROR "Production prepared encoder ABI symbol not found")
    endif()
    file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/prepared_symbol.h" "#define PREPARED_SYMBOL \"${PREPARED_SYMBOL}\"\n")
    add_executable(f02_ticket_vertical "${FIXTURE}/probe_sender.cpp" "${FIXTURE}/prepared_capture.cpp")
    target_include_directories(f02_ticket_vertical PRIVATE "${CMAKE_CURRENT_BINARY_DIR}" "${FIXTURE}")
    target_compile_options(f02_ticket_vertical PRIVATE -UNDEBUG -fsanitize=address,undefined -fno-omit-frame-pointer -fno-pie)
    target_link_options(f02_ticket_vertical PRIVATE -fsanitize=address,undefined -no-pie "-Wl,--wrap=${PREPARED_SYMBOL}"
        "-Wl,-Map=${CMAKE_CURRENT_BINARY_DIR}/f02_ticket_vertical.map")
    target_link_libraries(f02_ticket_vertical PRIVATE linux_ratchet_runtime)
endif()
