if(WIN32 AND MINGW)
    # windres fails on include paths with spaces; strip <INCLUDES> for icon-only RC files
    set(CMAKE_RC_COMPILE_OBJECT
        "<CMAKE_RC_COMPILER> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>")
    file(TO_NATIVE_PATH
        "${CMAKE_CURRENT_SOURCE_DIR}/resources/images/bloom.ico" _ICO_PATH)
    string(REPLACE "\\" "\\\\" _ICO_PATH "${_ICO_PATH}")
    # Generated in the build folder: the icon path is machine-specific, so it
    # must not rewrite a file in the source tree
    set(APP_ICON_RESOURCE "${CMAKE_CURRENT_BINARY_DIR}/bloom.rc")
    file(WRITE "${APP_ICON_RESOURCE}" "IDI_ICON1 ICON \"${_ICO_PATH}\"\n")
    qt_add_executable(BloomPlanner
        main.cpp
        ${APP_ICON_RESOURCE}
    )
else()
    qt_add_executable(BloomPlanner
        main.cpp
    )
endif()

# Core sources compiled directly into the executable for now (Phase 1)
target_sources(BloomPlanner PRIVATE
    core/database.h core/database.cpp
    core/settingsmanager.h core/settingsmanager.cpp
    models/category.h models/category.cpp
    repositories/categoryrepository.h repositories/categoryrepository.cpp
    viewmodels/categoryviewmodel.h viewmodels/categoryviewmodel.cpp
    models/event.h models/event.cpp
    repositories/eventrepository.h repositories/eventrepository.cpp
    viewmodels/eventviewmodel.h viewmodels/eventviewmodel.cpp
    viewmodels/calendarviewmodel.h viewmodels/calendarviewmodel.cpp
    models/recurrencerule.h models/recurrencerule.cpp
    repositories/recurrencerulerepository.h repositories/recurrencerulerepository.cpp
    core/dateutils.h core/dateutils.cpp
    models/task.h models/task.cpp
    repositories/taskrepository.h repositories/taskrepository.cpp
    viewmodels/taskviewmodel.h viewmodels/taskviewmodel.cpp
    models/presettask.h models/presettask.cpp
    models/plantemplate.h models/plantemplate.cpp
    repositories/presettaskrepository.h repositories/presettaskrepository.cpp
    repositories/plantemplaterepository.h repositories/plantemplaterepository.cpp
    viewmodels/rightnowviewmodel.h viewmodels/rightnowviewmodel.cpp
    core/searchengine.h core/searchengine.cpp
    core/themeprovider.h core/themeprovider.cpp
    core/datamanager.h core/datamanager.cpp
    models/reminder.h models/reminder.cpp
    repositories/reminderrepository.h repositories/reminderrepository.cpp
)

# Bloom 2.0 areas: picked up by folder so new files need no CMake edits
file(GLOB BLOOM_EXTRA_SOURCES CONFIGURE_DEPENDS
    ${CMAKE_CURRENT_SOURCE_DIR}/core/ui/*.h ${CMAKE_CURRENT_SOURCE_DIR}/core/ui/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/system/*.h ${CMAKE_CURRENT_SOURCE_DIR}/system/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/integrations/*.h ${CMAKE_CURRENT_SOURCE_DIR}/integrations/*.cpp
    ${CMAKE_CURRENT_SOURCE_DIR}/planner/*.h ${CMAKE_CURRENT_SOURCE_DIR}/planner/*.cpp
)
target_sources(BloomPlanner PRIVATE ${BLOOM_EXTRA_SOURCES})
# The publish script passes the release version; otherwise it's the project version
if(NOT BLOOM_APP_VERSION)
    set(BLOOM_APP_VERSION "${PROJECT_VERSION}")
endif()
target_compile_definitions(BloomPlanner PRIVATE BLOOM_VERSION="${BLOOM_APP_VERSION}")

# QML module with all Phase 1 QML files
qt_policy(SET QTP0004 OLD)
qt_add_qml_module(BloomPlanner
    URI BloomPlanner
    VERSION 1.0
    QML_FILES
        qml/Main.qml
        qml/views/HomePage.qml
        qml/widgets/WidgetFrame.qml
        qml/widgets/TodayWidget.qml
        qml/widgets/UpNextWidget.qml
        qml/widgets/DueSoonWidget.qml
        qml/widgets/ShiftWidget.qml
        qml/widgets/MiniCalendarWidget.qml
        qml/widgets/JournalWidget.qml
        qml/widgets/CountdownWidget.qml
        qml/widgets/PhotoWidget.qml
        qml/widgets/QuoteWidget.qml
        qml/widgets/FocusWidget.qml
        qml/widgets/util.js
        qml/widgets/affirmations.js
        qml/views/JournalPage.qml
        qml/views/StylePage.qml
        qml/views/CalendarPage.qml
        qml/views/DayPanel.qml
        qml/views/TasksPage.qml
        qml/views/RightNowPage.qml
        qml/views/SearchPage.qml
        qml/views/SettingsPage.qml
        qml/panels/NavigationRail.qml
        qml/panels/SearchOverlay.qml
        qml/panels/UpdateBanner.qml
        qml/components/Icon.qml
        qml/components/Sticker.qml
        qml/components/AppBackground.qml
        qml/components/GlassCard.qml
        qml/components/SegmentedControl.qml
        qml/components/ContextMenu.qml
        qml/components/StickerPicker.qml
        qml/components/BloomBadge.qml
        qml/components/BloomButton.qml
        qml/components/BloomCard.qml
        qml/components/BloomCheckBox.qml
        qml/components/BloomChip.qml
        qml/components/BloomComboBox.qml
        qml/components/BloomColorPicker.qml
        qml/components/BloomDatePicker.qml
        qml/components/BloomDialog.qml
        qml/components/BloomEmptyState.qml
        qml/components/BloomIconButton.qml
        qml/components/BloomProgressBar.qml
        qml/components/BloomSearchBar.qml
        qml/components/BloomSlider.qml
        qml/components/SettingsSection.qml
        qml/components/SettingRow.qml
        qml/components/PillChoice.qml
        qml/components/EventBlock.qml
        qml/components/AllDayChip.qml
        qml/components/BloomSwitch.qml
        qml/components/BloomTextArea.qml
        qml/components/BloomTextField.qml
        qml/components/BloomTimePicker.qml
        qml/components/BloomToast.qml
        qml/components/BloomToolTip.qml
        qml/dialogs/ConfirmDialog.qml
        qml/dialogs/ChoiceDialog.qml
        qml/dialogs/CategoryDialog.qml
        qml/dialogs/EventDialog.qml
        qml/views/MonthView.qml
        qml/views/DayView.qml
        qml/views/WeekView.qml
        qml/views/YearView.qml
        qml/views/AgendaView.qml
        qml/dialogs/RecurrenceDialog.qml
        qml/dialogs/TaskDialog.qml
        qml/dialogs/PresetTaskDialog.qml
        qml/dialogs/TemplateDialog.qml
        qml/views/RightNowPresets.qml
        qml/views/RightNowSession.qml
        qml/dialogs/ReminderDialog.qml
        qml/dialogs/ScheduleImportDialog.qml
        qml/dialogs/SyllabusImportDialog.qml
        qml/dialogs/WelcomeDialog.qml
        qml/dialogs/FontPickerDialog.qml
)

# Embed SQL migrations as Qt resources
qt_add_resources(BloomPlanner "migrations"
    PREFIX "/migrations"
    BASE ${CMAKE_CURRENT_SOURCE_DIR}/core/migrations
    FILES
        core/migrations/001_initial_schema.sql
        core/migrations/002_local_timestamps.sql
        core/migrations/003_bloom2.sql
)

# Fonts (Fredoka/Nunito by default plus Google fonts to pick from), icons
# (Phosphor) and stickers (Fluent Emoji 3D), all embedded so the app is one file
file(GLOB BLOOM_FONT_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS
    resources/fonts/*.ttf)
qt_add_resources(BloomPlanner "fonts"
    PREFIX "/fonts"
    BASE ${CMAKE_CURRENT_SOURCE_DIR}/resources/fonts
    FILES ${BLOOM_FONT_FILES}
)
file(GLOB_RECURSE BLOOM_ICON_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS
    resources/icons/*.svg)
qt_add_resources(BloomPlanner "icons"
    PREFIX "/icons"
    BASE ${CMAKE_CURRENT_SOURCE_DIR}/resources/icons
    FILES ${BLOOM_ICON_FILES}
)
file(GLOB_RECURSE BLOOM_STICKER_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} CONFIGURE_DEPENDS
    resources/stickers/*.png resources/stickers/manifest.json)
qt_add_resources(BloomPlanner "stickers"
    PREFIX "/stickers"
    BASE ${CMAKE_CURRENT_SOURCE_DIR}/resources/stickers
    FILES ${BLOOM_STICKER_FILES}
)
# Embed app images as Qt resources
qt_add_resources(BloomPlanner "images"
    PREFIX "/images"
    BASE ${CMAKE_CURRENT_SOURCE_DIR}/resources/images
    FILES
        resources/images/bloom_logo.png
)

target_link_libraries(BloomPlanner PRIVATE
    Qt6::Core
    Qt6::Gui
    Qt6::Quick
    Qt6::QuickControls2
    Qt6::Sql
    Qt6::Widgets
    Qt6::Network
    Qt6::Svg
)

if(WIN32)
    set_target_properties(BloomPlanner PROPERTIES WIN32_EXECUTABLE TRUE)
    # Windows CNG: verifies the signature on updates
    target_link_libraries(BloomPlanner PRIVATE bcrypt)
endif()

# Static (single-exe) builds: link Windows' TLS backend so the Canvas calendar
# feed can be fetched over HTTPS
if(NOT QT6_IS_SHARED_LIBS_BUILD AND TARGET Qt6::QSchannelBackendPlugin)
    qt_import_plugins(BloomPlanner INCLUDE Qt6::QSchannelBackendPlugin)
endif()
