cmake_minimum_required(VERSION 3.10)
project(leveldb_extension LANGUAGES CXX)

# Set C++ standard to match the main project
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Disable RTTI to match LevelDB's compilation settings
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti")

# Create the extension library
add_library(leveldb_extension STATIC
    src/logger.cpp
)

include_directories(
    ${CMAKE_CURRENT_SOURCE_DIR}/include
)

# Add LevelDB headers from LEVELDB_INSTALL_DIR if provided
if(LEVELDB_INSTALL_DIR)
    include_directories(${LEVELDB_INSTALL_DIR}/include)
else()
    # Fallback to relative path for development
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../deps/leveldb/include)
endif()

# Find LevelDB library in the provided directory
find_library(LEVELDB_LIBRARY
    NAMES leveldb
    PATHS ${LEVELDB_INSTALL_DIR}/lib
    NO_DEFAULT_PATH
)

# Add dependency on LevelDB
if(LEVELDB_LIBRARY)
    target_link_libraries(leveldb_extension leveldb)
    message(STATUS "Found LevelDB library: ${LEVELDB_LIBRARY}")
else()
    message(WARNING "LevelDB library not found in ${LEVELDB_INSTALL_DIR}/lib, assuming it will be linked later")
endif()

# Set target properties
set_target_properties(leveldb_extension
    PROPERTIES
    POSITION_INDEPENDENT_CODE ON
)

# Platform-specific linking
if(WIN32)
    target_link_libraries(leveldb_extension c++)
else()
    target_link_libraries(leveldb_extension stdc++)
endif()

# Find and link threads
find_package(Threads REQUIRED)
target_link_libraries(leveldb_extension Threads::Threads)

# Install the library
install(TARGETS leveldb_extension
    ARCHIVE DESTINATION lib
    LIBRARY DESTINATION lib
)

# Option to build tests
option(BUILD_TESTS "Build unit tests for extensions" OFF)

if(BUILD_TESTS)
    # Enable testing
    enable_testing()

    # Find Google Test
    find_package(GTest)
    if(NOT GTest_FOUND)
        message(FATAL_ERROR "Google Test is required for building tests but was not found. Consider to disable tests with -DBUILD_TESTS=OFF")
    endif()

    # Create test executable
    add_executable(logger_test
        tests/logger_test.cpp
        src/logger.cpp
    )

    # Link test executable with libraries
    target_link_libraries(logger_test
        GTest::gtest
        GTest::gtest_main
        GTest::gmock
        GTest::gmock_main
        ${LEVELDB_LIBRARY}
        Threads::Threads
    )

    # Add LevelDB include directories for tests
    if(LEVELDB_INSTALL_DIR)
        target_include_directories(logger_test PRIVATE ${LEVELDB_INSTALL_DIR}/include)
    else()
        target_include_directories(logger_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../deps/leveldb/include)
    endif()

    # Add current include directory
    target_include_directories(logger_test PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)

    # Set test properties
    set_target_properties(logger_test
        PROPERTIES
        POSITION_INDEPENDENT_CODE ON
    )

    # Platform-specific linking for tests
    if(WIN32)
        target_link_libraries(logger_test c++)
    else()
        target_link_libraries(logger_test stdc++)
    endif()

    # Register test
    add_test(NAME LoggerTest COMMAND logger_test)
endif()
