cmake_minimum_required(VERSION 3.16)
project(WinoMailQt VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# FetchContent for mailio (C++ email library)
include(FetchContent)

# Option to use mailio-based IMAP (OFF by default to avoid Boost dependency issues)
option(USE_MAILIO_IMAP "Use mailio-based IMAP synchronizer (requires Boost)" ON)

if (USE_MAILIO_IMAP)
    # Use pre-built mailio and Boost from thirdparty/
    list(APPEND CMAKE_PREFIX_PATH "${PROJECT_SOURCE_DIR}/thirdparty/boost" "${PROJECT_SOURCE_DIR}/thirdparty/mailio")
    
    # Find Boost components
    find_package(Boost 1.90.0 EXACT COMPONENTS date_time regex REQUIRED CONFIG)
    
    # Find mailio
    find_package(mailio REQUIRED CONFIG)
endif()

find_package(Qt6 COMPONENTS Core Gui Widgets Network Sql Test Concurrent REQUIRED)

set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTORCC ON)

# Include directories
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/src/core)
include_directories(${PROJECT_SOURCE_DIR}/src/db)
include_directories(${PROJECT_SOURCE_DIR}/src/utils)
include_directories(${PROJECT_SOURCE_DIR}/src/services)
include_directories(${PROJECT_SOURCE_DIR}/src/services/gmail)
include_directories(${PROJECT_SOURCE_DIR}/src/services/outlook)
include_directories(${PROJECT_SOURCE_DIR}/src/services/imap)
include_directories(${PROJECT_SOURCE_DIR}/src/services/pop3)
include_directories(${PROJECT_SOURCE_DIR}/thirdparty/tags/include)
# Tag library headers for moc
set(TAG_HEADERS
    ${PROJECT_SOURCE_DIR}/thirdparty/tags/include/tags_line_edit.hpp
    ${PROJECT_SOURCE_DIR}/thirdparty/tags/include/tags_edit.hpp
)
qt6_wrap_cpp(TAG_MOCS ${TAG_HEADERS})


# Source files
set(SRC_FILES
    src/main.cpp
    src/core/translator.cpp
    src/core/mailitem.cpp
    src/core/models/account.cpp
    src/core/models/folder.cpp
    src/services/request.cpp
    src/services/concreterequests.cpp
    src/services/requestprocessor.cpp
    src/services/changetype.h
    src/services/changeprocessor.cpp
    src/services/accountservice.cpp
    src/services/mailservice.cpp
    src/services/mimestorage.cpp
    src/services/folderservice.cpp
    src/services/synchronizer.cpp
    src/services/rulesengine.cpp
    src/services/imap/imapsynchronizer.cpp
    src/services/imap/imapconnection.cpp
    src/services/outlook/outlooksynchronizer.cpp
    src/services/gmail/gmailsynchronizer.cpp
    src/services/pop3/pop3synchronizer.cpp
    src/db/databasemanager.cpp
    src/db/dao/accountdao.cpp
    src/db/dao/folderdao.cpp
    src/db/dao/mailitemdao.cpp
    src/db/dao/categorydao.cpp
    src/db/dao/ruledao.cpp
    src/db/dao/templatedao.cpp
    src/db/dao/signaturedao.cpp
    src/db/dbchangeprocessor.cpp
    src/core/eventbus.cpp
    src/utils/notificationmanager.cpp
    src/core/emailmanager.cpp
    src/core/emailcomposerbridge.cpp
    src/core/authenticator.cpp
    src/core/gmailauthenticator.cpp
    src/core/outlookauthenticator.cpp
    src/core/imapauthenticator.cpp
    src/core/synchronizerprovider.cpp
    src/core/accountsetupdialoglauncher.cpp
    src/core/oauthcallbackserver.cpp
    src/ui/mainmainwindow.cpp
    src/ui/mainmainwindow.h
    src/ui/newmessagedialog.cpp
    src/ui/newmessagedialog.h
    src/ui/accountsetupdialog.cpp
    src/ui/accountsetupdialog.h
    src/ui/models/FolderListModel.cpp
    src/ui/models/EmailListModel.cpp
    src/ui/models/EmailTreeModel.cpp
    src/ui/models/DateGroupProxyModel.cpp
    src/ui/readerview.cpp
    src/ui/readerview.h
    src/ui/maillistview.cpp
    src/ui/maillistview.h
    src/ui/composeview.cpp
    src/ui/composeview.h
    src/ui/richtexteditor.cpp
    src/ui/richtexteditor.h
    src/ui/settingsview.cpp
    src/ui/settingsview.h
    src/ui/contactsview.cpp
    src/ui/contactsview.h
    src/ui/calendarview.cpp
    src/ui/calendarview.h
    src/ui/connectionwizard.cpp
    src/ui/connectionwizard.h
    src/ui/categorytreewidget.cpp
    src/ui/categorytreewidget.h
    src/ui/rulesmanagerdialog.cpp
    src/ui/rulesmanagerdialog.h
    src/ui/ruleditordialog.cpp
    src/ui/ruleditordialog.h
    src/ui/templateeditordialog.cpp
    src/ui/templateeditordialog.h
    src/ui/templatesmanagerdialog.cpp
    src/ui/templatesmanagerdialog.h
    src/ui/signaturemanagerdialog.cpp
    src/ui/signaturemanagerdialog.h
    src/syncscheduler.cpp
    thirdparty/tags/src/tags_line_edit.cpp
    thirdparty/tags/src/tags_edit.cpp
    thirdparty/tags/src/config.cpp
    ${TAG_MOCS}
)

if (USE_MAILIO_IMAP)
    list(APPEND SRC_FILES
        src/services/imap/imapsynchronizer_mailio.cpp
        src/services/pop3/pop3synchronizer_mailio.cpp
    )
endif()
# Executable
add_executable(wino-mail-qt ${SRC_FILES}
    icons.qrc
)

# Link Qt
target_link_libraries(wino-mail-qt PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Network Qt6::Sql Qt6::Concurrent)
if (WIN32)
    target_link_libraries(wino-mail-qt PRIVATE crypt32)
endif()

if (USE_MAILIO_IMAP)
    target_link_libraries(wino-mail-qt PRIVATE mailio::mailio)
endif()

enable_testing()
add_executable(test_mimestorage
    tests/unit/test_mimestorage.cpp
    src/core/mailitem.cpp
    src/services/mimestorage.cpp
)
target_link_libraries(test_mimestorage PRIVATE Qt6::Core Qt6::Test)
add_test(NAME test_mimestorage COMMAND test_mimestorage)
