cmake_minimum_required(VERSION 3.16)

# 定义目标名称变量
set(TARGET_NAME dsearch)

project(${TARGET_NAME})

include(CTest)

# 核心对齐一：深度官方底层使用了 C++20 标准
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# 开启 Qt 专属的元对象编译器
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# 1. 寻找 Qt 依赖
find_package(Qt6 REQUIRED COMPONENTS Widgets Core Network)
set(QT_VERSION_MAJOR 6)
message(STATUS "Using Locked Qt Version: ${QT_VERSION_MAJOR}")

# 2. 寻找其他原生底层包
find_package(PkgConfig REQUIRED)

# 拆分 PkgConfig 检查，确保 glib 和 gio 的头文件及链接库都能被正确抓取
pkg_check_modules(GLIB   REQUIRED glib-2.0)
pkg_check_modules(GIO    REQUIRED gio-2.0)
pkg_check_modules(LUCENE REQUIRED liblucene++)
pkg_check_modules(LUCENE_CONTRIB REQUIRED liblucene++-contrib)

find_package(Threads REQUIRED)

# 3. 生成目标二进制
add_executable(${TARGET_NAME}
    resources.qrc
    main.cpp
    mainwindow.cpp
    searchworker.cpp
    searcher.cpp
    searchqueryutil.cpp
    indexservicestatus.cpp
    searchmanager.cpp
    filetablemodel.cpp
    filesortmodel.cpp
    naturalnamecompare.cpp
    configmanager.cpp
    contextmenumanager.cpp
    keyeventmanager.cpp
    compactitemdelegate.cpp
    traymanager.cpp
)

# 4. 头文件包含
target_include_directories(${TARGET_NAME} PRIVATE
    ${GLIB_INCLUDE_DIRS}
    ${GIO_INCLUDE_DIRS}
    ${LUCENE_INCLUDE_DIRS}
    ${LUCENE_CONTRIB_INCLUDE_DIRS}
)

# 5. 链接所有依赖库
target_link_libraries(${TARGET_NAME} PRIVATE
    Qt${QT_VERSION_MAJOR}::Widgets
    Qt${QT_VERSION_MAJOR}::Core
    Qt${QT_VERSION_MAJOR}::Network
    ${GLIB_LIBRARIES}
    ${GIO_LIBRARIES}
    ${LUCENE_LIBRARIES}
    ${LUCENE_CONTRIB_LIBRARIES}
    Threads::Threads
)

target_link_options(${TARGET_NAME} PRIVATE -pie)

# 性能诊断：手动 cmake .. -DDSEARCH_PROFILE=ON 开启
option(DSEARCH_PROFILE "Enable paint timing diagnostics" OFF)
if(DSEARCH_PROFILE)
    target_compile_definitions(${TARGET_NAME} PRIVATE DSEARCH_PROFILE)
endif()

# 6. 索引检查工具（纯 C++，无 Qt 依赖）
add_executable(idx_inspector idx_inspector.cpp)

set_target_properties(idx_inspector PROPERTIES
    AUTOMOC OFF
    AUTOUIC OFF
    AUTORCC OFF
)

target_include_directories(idx_inspector PRIVATE
    ${GLIB_INCLUDE_DIRS}
    ${GIO_INCLUDE_DIRS}
    ${LUCENE_INCLUDE_DIRS}
    ${LUCENE_CONTRIB_INCLUDE_DIRS}
)

target_link_libraries(idx_inspector PRIVATE
    ${GLIB_LIBRARIES}
    ${GIO_LIBRARIES}
    ${LUCENE_LIBRARIES}
    ${LUCENE_CONTRIB_LIBRARIES}
    Threads::Threads
)

if(BUILD_TESTING)
    add_executable(dsearch_core_tests
        ../tests/corelogic_test.cpp
        searchqueryutil.cpp
        indexservicestatus.cpp
        naturalnamecompare.cpp
    )

    target_include_directories(dsearch_core_tests PRIVATE
        ${CMAKE_CURRENT_SOURCE_DIR}
    )

    target_link_libraries(dsearch_core_tests PRIVATE
        Qt${QT_VERSION_MAJOR}::Core
    )

    add_test(NAME dsearch_core_logic COMMAND dsearch_core_tests)
endif()