# Smartbotic Database Client Library

if(BUILD_SHARED_LIBS)
    add_library(smartbotic-db-client SHARED
        src/client.cpp
        $<TARGET_OBJECTS:smartbotic_db_proto>
    )
    # SOVERSION stays 2 — and the rule that keeps it honest:
    #
    # BUMP THIS whenever a public struct changes size or layout. It stayed at 2
    # across v2.1-v2.7.0 while Client::CollectionConfig (v2.4.5) and FileRecord
    # (v2.6.0) both gained members, on the reasoning that "consumers must
    # rebuild". That reasoning failed the moment it was tested: a v2.7.1 attempt
    # to add `projection` to Client::QueryOptions changed the struct size without
    # changing the soname, and the already-installed smartbotic-webserver -
    # compiled against the v2.7.0 layout - bound to the new library and SIGSEGV'd
    # in a restart loop until the packages were rolled back.
    #
    # A changelog note is not an enforcement mechanism; a soname is. v2.7.1
    # therefore adds projection as an OVERLOAD rather than a struct member, so
    # existing binaries keep working untouched. The next deliberate layout change
    # bumps this to 3 and renames the package alongside it.
    set_target_properties(smartbotic-db-client PROPERTIES
        VERSION ${SMARTBOTIC_DB_VERSION}
        SOVERSION 2
    )
else()
    add_library(smartbotic-db-client STATIC
        src/client.cpp
    )
endif()

# v2.11.1 — clientLibraryVersion()/clientLibraryCommit() report what the loaded
# .so actually is. Same two macros the service and CLI targets define.
target_compile_definitions(smartbotic-db-client PRIVATE
    SMARTBOTIC_DB_VERSION_STRING="${SMARTBOTIC_DB_VERSION}"
    SMARTBOTIC_DB_GIT_COMMIT_STRING="${SMARTBOTIC_DB_GIT_COMMIT}"
)

target_include_directories(smartbotic-db-client PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

# Link dependencies
target_link_libraries(smartbotic-db-client PUBLIC
    smartbotic_db_proto
)

if(TARGET nlohmann_json::nlohmann_json)
    target_link_libraries(smartbotic-db-client PUBLIC nlohmann_json::nlohmann_json)
else()
    target_include_directories(smartbotic-db-client PUBLIC ${NLOHMANN_JSON_INCLUDE_DIRS})
endif()

if(TARGET spdlog::spdlog)
    target_link_libraries(smartbotic-db-client PUBLIC spdlog::spdlog)
else()
    target_link_libraries(smartbotic-db-client PUBLIC ${SPDLOG_LIBRARIES})
    target_include_directories(smartbotic-db-client PUBLIC ${SPDLOG_INCLUDE_DIRS})
endif()

# Export alias
add_library(smartbotic::db-client ALIAS smartbotic-db-client)

# Installation (standalone/packaging only)
if(SMARTBOTIC_DB_STANDALONE)
    include(GNUInstallDirs)

    install(TARGETS smartbotic-db-client
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    )

    install(DIRECTORY include/smartbotic
        DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
    )
endif()
