cmake_minimum_required(VERSION 3.20)
project(q_ai_drive_check VERSION 1.1.1 LANGUAGES CXX)
if(APPLE AND NOT CMAKE_OSX_DEPLOYMENT_TARGET)
  set(CMAKE_OSX_DEPLOYMENT_TARGET "12.0" CACHE STRING "Minimum macOS version")
endif()
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(WIN32)
  set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
endif()
add_library(benchmark src/benchmark.cpp src/report.cpp src/results.cpp)
target_include_directories(benchmark PUBLIC src)
if(APPLE)
  set(Q_AI_LOCAL_SIGN_CERT_SHA1 "" CACHE STRING "Pinned local code-signing certificate SHA-1; local testing only")
  if(Q_AI_LOCAL_SIGN_CERT_SHA1)
    string(LENGTH "${Q_AI_LOCAL_SIGN_CERT_SHA1}" _pin_length)
    if(NOT _pin_length EQUAL 40 OR NOT Q_AI_LOCAL_SIGN_CERT_SHA1 MATCHES "^[0-9A-F]+$")
      message(FATAL_ERROR "Local certificate pin must be 40 uppercase hexadecimal characters")
    endif()
    set(Q_AI_HELPER_SERVICE "com.q-ai.drivecheck.format-helper.local")
    set(Q_AI_LOCAL_HELPER_REQUIREMENT "identifier &quot;${Q_AI_HELPER_SERVICE}&quot; and certificate leaf = H&quot;${Q_AI_LOCAL_SIGN_CERT_SHA1}&quot;")
    set(Q_AI_LOCAL_PRIVILEGED_EXECUTABLES "<key>SMPrivilegedExecutables</key><dict><key>${Q_AI_HELPER_SERVICE}</key><string>${Q_AI_LOCAL_HELPER_REQUIREMENT}</string></dict>")
    target_compile_definitions(benchmark PUBLIC Q_AI_LOCAL_SIGN_CERT_SHA1="${Q_AI_LOCAL_SIGN_CERT_SHA1}" Q_AI_HELPER_SERVICE="${Q_AI_HELPER_SERVICE}")
  endif()
  enable_language(OBJCXX)
  set(CMAKE_OBJCXX_STANDARD 17)
  set(CMAKE_OBJCXX_STANDARD_REQUIRED ON)
  target_sources(benchmark PRIVATE src/macos_disk.cpp src/macos_format.mm src/macos_helper_client.mm)
  set_target_properties(benchmark PROPERTIES OBJCXX_STANDARD 17 OBJCXX_STANDARD_REQUIRED ON)
  target_compile_options(benchmark PRIVATE $<$<COMPILE_LANGUAGE:OBJCXX>:-fobjc-arc>)
  target_link_libraries(benchmark PUBLIC "-framework CoreFoundation" "-framework Foundation" "-framework Security" "-framework ServiceManagement")
  set_source_files_properties(src/brand/q-ai.icns PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
  add_executable(q_ai_drive_check MACOSX_BUNDLE src/macos_main.mm src/macos_results.mm src/brand/q-ai.icns)
  target_compile_options(q_ai_drive_check PRIVATE -fobjc-arc -Wall -Wextra)
  target_compile_options(benchmark PRIVATE -Wall -Wextra)
  target_link_libraries(q_ai_drive_check PRIVATE benchmark "-framework AppKit" "-framework Foundation" "-framework UniformTypeIdentifiers")
  add_executable(q_ai_drive_check_helper src/macos_helper_main.mm)
  target_compile_options(q_ai_drive_check_helper PRIVATE -fobjc-arc -Wall -Wextra)
  target_link_libraries(q_ai_drive_check_helper PRIVATE benchmark)
  set_target_properties(q_ai_drive_check_helper PROPERTIES OUTPUT_NAME "q-ai-drive-check-helper")
  if(Q_AI_LOCAL_SIGN_CERT_SHA1)
    string(TIMESTAMP Q_AI_LOCAL_HELPER_VERSION "%Y%m%d%H%M%S" UTC)
    configure_file(src/local-helper-info.plist.in local-helper-info.plist @ONLY)
    configure_file(src/local-helper-launchd.plist.in local-helper-launchd.plist @ONLY)
    set_target_properties(q_ai_drive_check_helper PROPERTIES OUTPUT_NAME "${Q_AI_HELPER_SERVICE}")
    target_link_options(q_ai_drive_check_helper PRIVATE "LINKER:-sectcreate,__TEXT,__info_plist,${CMAKE_CURRENT_BINARY_DIR}/local-helper-info.plist" "LINKER:-sectcreate,__TEXT,__launchd_plist,${CMAKE_CURRENT_BINARY_DIR}/local-helper-launchd.plist")
    add_custom_command(TARGET q_ai_drive_check POST_BUILD
      COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_BUNDLE_DIR:q_ai_drive_check>/Contents/Library/LaunchServices"
      COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:q_ai_drive_check_helper>" "$<TARGET_BUNDLE_DIR:q_ai_drive_check>/Contents/Library/LaunchServices/${Q_AI_HELPER_SERVICE}")
  endif()
  add_dependencies(q_ai_drive_check q_ai_drive_check_helper)
  add_custom_command(TARGET q_ai_drive_check POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory "$<TARGET_BUNDLE_DIR:q_ai_drive_check>/Contents/Library/LaunchDaemons"
    COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:q_ai_drive_check_helper>" "$<TARGET_BUNDLE_DIR:q_ai_drive_check>/Contents/MacOS/q-ai-drive-check-helper"
    COMMAND ${CMAKE_COMMAND} -E copy "${CMAKE_CURRENT_SOURCE_DIR}/src/com.q-ai.drivecheck.format-helper.plist" "$<TARGET_BUNDLE_DIR:q_ai_drive_check>/Contents/Library/LaunchDaemons/com.q-ai.drivecheck.format-helper.plist")
  set_target_properties(q_ai_drive_check PROPERTIES
    MACOSX_BUNDLE_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/src/Info.plist.in")
elseif(WIN32)
  target_sources(benchmark PRIVATE src/windows_disk.cpp src/format.cpp)
  target_compile_definitions(benchmark PUBLIC UNICODE _UNICODE NOMINMAX WIN32_LEAN_AND_MEAN)
  target_link_libraries(benchmark PUBLIC bcrypt crypt32 shell32)
  add_executable(q_ai_drive_check WIN32 src/main.cpp src/app.rc)
  target_link_libraries(q_ai_drive_check PRIVATE benchmark comctl32 ole32 oleacc uuid shell32 comdlg32)
else()
  message(FATAL_ERROR "Drive Check supports Windows and macOS.")
endif()
set_target_properties(q_ai_drive_check PROPERTIES OUTPUT_NAME "q-ai-drive-check")
if(MSVC)
  target_compile_options(benchmark PRIVATE /W4 /utf-8)
  target_compile_options(q_ai_drive_check PRIVATE /W4 /utf-8)
  target_link_options(q_ai_drive_check PRIVATE /MANIFEST:NO)
endif()
enable_testing()
add_executable(report_tests tests/report_tests.cpp)
target_link_libraries(report_tests PRIVATE benchmark)
add_test(NAME report_tests COMMAND report_tests)
add_executable(benchmark_tests tests/tests.cpp)
if(WIN32)
  # Exercise the shared exporter's macOS compile-time policy with synthetic records.
  # Windows Unicode helpers supply conversions; no disk benchmark is run by this test.
  add_executable(report_macos_policy_tests tests/report_tests.cpp src/report.cpp src/results.cpp src/windows_disk.cpp)
  target_include_directories(report_macos_policy_tests PRIVATE src)
  target_compile_definitions(report_macos_policy_tests PRIVATE __APPLE__ UNICODE _UNICODE NOMINMAX WIN32_LEAN_AND_MEAN)
  target_compile_options(report_macos_policy_tests PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/utf-8>)
  target_link_libraries(report_macos_policy_tests PRIVATE bcrypt)
  add_test(NAME report_macos_policy_tests COMMAND report_macos_policy_tests)
  target_sources(benchmark_tests PRIVATE src/format-resource.rc)
  add_executable(results_tests tests/results_tests.cpp src/format-resource.rc)
  target_compile_options(results_tests PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/utf-8>)
  target_link_libraries(results_tests PRIVATE benchmark)
  add_test(NAME results_tests COMMAND results_tests)
endif()
target_link_libraries(benchmark_tests PRIVATE benchmark)
add_test(NAME benchmark_tests COMMAND benchmark_tests)
if(APPLE)
  add_executable(macos_format_tests tests/macos_format_tests.mm)
  target_compile_options(macos_format_tests PRIVATE -fobjc-arc)
  target_link_libraries(macos_format_tests PRIVATE benchmark "-framework Foundation")
  add_test(NAME macos_format_tests COMMAND macos_format_tests)
  add_executable(macos_results_tests tests/macos_results_tests.mm src/macos_results.mm)
  target_compile_options(macos_results_tests PRIVATE -fobjc-arc)
  target_include_directories(macos_results_tests PRIVATE src)
  target_link_libraries(macos_results_tests PRIVATE "-framework Foundation" "-framework CoreFoundation")
  add_test(NAME macos_results_tests COMMAND macos_results_tests)
  option(Q_AI_GUI_TESTS "Run AppKit smoke tests (requires a logged-in graphical session)" OFF)
  if(Q_AI_GUI_TESTS)
    add_test(NAME macos_gui_smoke COMMAND $<TARGET_FILE:q_ai_drive_check> --gui-smoke)
    add_test(NAME macos_gui_close_smoke COMMAND $<TARGET_FILE:q_ai_drive_check> --gui-close-smoke)
    add_test(NAME macos_save_prompt_smoke COMMAND $<TARGET_FILE:q_ai_drive_check> --save-prompt-smoke)
    add_test(NAME macos_format_dialog_smoke COMMAND $<TARGET_FILE:q_ai_drive_check> --format-dialog-smoke)
    set_tests_properties(macos_gui_smoke macos_gui_close_smoke macos_format_dialog_smoke macos_save_prompt_smoke PROPERTIES TIMEOUT 60 RUN_SERIAL TRUE)
  endif()
endif()
