# ============================================================
#  Node.js binding
# ============================================================
set(NODE_MODULE_MINIMUM_ABI 115)

include("${CMAKE_SOURCE_DIR}/cmake/nodejs.cmake")

add_node_module(authdb-nds INSTALL_PATH "nodejs/lib/{node_abi}/authdb-nds.node")

target_sources(authdb-nds INTERFACE
    ${CMAKE_CURRENT_SOURCE_DIR}/nodejs/nodesjs.cpp
)

target_include_directories(
    authdb-nds BEFORE INTERFACE "${CMAKE_SOURCE_DIR}/client"
)

if(GNU)    
    target_compile_options(authdb-nds INTERFACE -z,defs)
elseif (MSVC)
    target_compile_options(authdb-nds INTERFACE /Zc:__cplusplus)
endif()

if(CMAKE_HOST_WIN32)
    add_compile_definitions(NOMINMAX)
endif()

target_link_libraries(authdb-nds INTERFACE client)

# ============================================================
#  Python binding
# ============================================================
find_package(Python3 COMPONENTS Interpreter Development)

if(Python3_FOUND)
    message(STATUS "Python3 found: ${Python3_VERSION} – building authdb Python module")

    add_library(authdb-python SHARED
        ${CMAKE_CURRENT_SOURCE_DIR}/python/python.cpp
    )

    set_target_properties(authdb-python PROPERTIES
        PREFIX ""                          # no "lib" prefix
        OUTPUT_NAME "authdb"               # produces authdb.so
        SUFFIX ".${Python3_SOABI}.so"       # e.g. .cpython-313-x86_64-linux-gnu.so
    )

    target_include_directories(authdb-python PRIVATE
        ${Python3_INCLUDE_DIRS}
        "${CMAKE_SOURCE_DIR}/client"
    )

    target_link_libraries(authdb-python PRIVATE
        client
        ${Python3_LIBRARIES}
    )

    # Install to Python site-packages
    execute_process(
        COMMAND ${Python3_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_path('platlib'))"
        OUTPUT_VARIABLE PYTHON_SITE_PACKAGES OUTPUT_STRIP_TRAILING_WHITESPACE
    )
    message(STATUS "  Python install dir: ${PYTHON_SITE_PACKAGES}")
    install(TARGETS authdb-python DESTINATION "${PYTHON_SITE_PACKAGES}")
else()
    message(STATUS "Python3 development headers not found – skipping Python binding")
endif()

# ============================================================
#  PHP binding
# ============================================================
find_program(PHP_CONFIG_EXECUTABLE php-config)

if(PHP_CONFIG_EXECUTABLE)
    execute_process(COMMAND ${PHP_CONFIG_EXECUTABLE} --includes
                    OUTPUT_VARIABLE PHP_INCLUDES OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process(COMMAND ${PHP_CONFIG_EXECUTABLE} --ldflags
                    OUTPUT_VARIABLE PHP_LDFLAGS  OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process(COMMAND ${PHP_CONFIG_EXECUTABLE} --libs
                    OUTPUT_VARIABLE PHP_LIBS     OUTPUT_STRIP_TRAILING_WHITESPACE)
    execute_process(COMMAND ${PHP_CONFIG_EXECUTABLE} --extension-dir
                    OUTPUT_VARIABLE PHP_EXTENSION_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)

    message(STATUS "PHP found via php-config – building authdb PHP extension")
    message(STATUS "  PHP includes : ${PHP_INCLUDES}")
    message(STATUS "  PHP ext dir  : ${PHP_EXTENSION_DIR}")

    add_library(authdb-php SHARED
        ${CMAKE_CURRENT_SOURCE_DIR}/php/php.cpp
    )

    set_target_properties(authdb-php PROPERTIES
        PREFIX ""                          # no "lib" prefix
        OUTPUT_NAME "authdb"               # produces authdb.so
    )

    # php-config --includes gives flags like -I/path -I/path2 …
    # We split them into a list for proper CMake handling.
    separate_arguments(PHP_INCLUDES_LIST UNIX_COMMAND "${PHP_INCLUDES}")
    target_compile_options(authdb-php PRIVATE ${PHP_INCLUDES_LIST})

    target_include_directories(authdb-php PRIVATE
        "${CMAKE_SOURCE_DIR}/client"
    )

    separate_arguments(PHP_LDFLAGS_LIST UNIX_COMMAND "${PHP_LDFLAGS}")
    target_link_options(authdb-php PRIVATE ${PHP_LDFLAGS_LIST})

    target_link_libraries(authdb-php PRIVATE client)

    # Install to PHP extension directory
    message(STATUS "  PHP install dir: ${PHP_EXTENSION_DIR}")
    install(TARGETS authdb-php DESTINATION "${PHP_EXTENSION_DIR}")
else()
    message(STATUS "php-config not found – skipping PHP binding")
endif()
