# Python bindings for PdfEngine using pybind11
#

find_package(pybind11 CONFIG REQUIRED)

# Declare the python module target. We name the target pdfengine_py to avoid
# target collision with the static C++ library pdfengine, but we set the
# OUTPUT_NAME to pdfengine to produce the correct importable module.
pybind11_add_module(pdfengine_py python/pdfengine_py.cpp $<TARGET_OBJECTS:pdfengine>)

set_target_properties(pdfengine_py PROPERTIES
    OUTPUT_NAME "pdfengine"
    ARCHIVE_OUTPUT_NAME "pdfengine_py_import"
)

target_link_libraries(pdfengine_py PRIVATE pdfengine::pdfengine)

target_include_directories(pdfengine_py PRIVATE 
    "${CMAKE_SOURCE_DIR}/engine/src"
)

if(MSVC)
    target_link_options(pdfengine_py PRIVATE "/FORCE:MULTIPLE")
endif()

# Set warnings and sanitizers for the bindings module
pdfengine_set_warnings(pdfengine_py)
pdfengine_enable_sanitizers(pdfengine_py)

# Copy the compiled .pyd (or .so) file to the gateway/ directory so the
# Python FastAPI app and its tests can import it immediately after building.
add_custom_command(TARGET pdfengine_py POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE:pdfengine_py> "${CMAKE_SOURCE_DIR}/gateway/"
    COMMENT "Copying compiled Python extension to gateway/ directory"
)
