cmake_minimum_required(VERSION 3.5)
project(win-dictation C CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Path to modules
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# Set output directory
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Options
option(BUILD_SHARED_LIBS "build shared libraries" OFF)
option(WHISPER_SDL2 "whisper: support for libSDL2" ON)
option(WHISPER_NO_AVX "whisper: disable AVX" OFF)
option(WHISPER_NO_AVX2 "whisper: disable AVX2" OFF)
option(WHISPER_NO_FMA "whisper: disable FMA" OFF)
option(WHISPER_NO_F16C "whisper: disable F16C" OFF)

# ----------------------------
# SDL2
# ----------------------------
if(NOT SDL2_DIR)
    set(SDL2_DIR "${CMAKE_CURRENT_SOURCE_DIR}/SDL2-mingw/cmake")
endif()

find_package(SDL2 REQUIRED)

string(STRIP "${SDL2_LIBRARIES}" SDL2_LIBRARIES)

# ----------------------------
# Whisper (ONLY dependency layer)
# ----------------------------
# IMPORTANT:
# We no longer build ggml separately.
# whisper/ must contain its own CMakeLists.txt (modern whisper.cpp layout)
add_subdirectory(whisper)

# ----------------------------
# Common Library (optional legacy utilities)
# ----------------------------
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/common/common.h")

    set(COMMON_TARGET common)

    add_library(${COMMON_TARGET} STATIC
        common/common.h
        common/common.cpp
        common/common-ggml.h
        common/common-ggml.cpp
        common/common-whisper.h
        common/common-whisper.cpp
        common/grammar-parser.h
        common/grammar-parser.cpp
    )

    target_include_directories(${COMMON_TARGET} PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/common
    )

    # Link against whisper target only (no ggml exposure)
    target_link_libraries(${COMMON_TARGET} PRIVATE whisper)

    set(COMMON_SDL_TARGET common-sdl)

    add_library(${COMMON_SDL_TARGET} STATIC
        common/common-sdl.h
        common/common-sdl.cpp
    )

    target_include_directories(${COMMON_SDL_TARGET} PUBLIC
        ${CMAKE_CURRENT_SOURCE_DIR}/common
        ${SDL2_INCLUDE_DIRS}
    )

    target_link_libraries(${COMMON_SDL_TARGET} PRIVATE ${SDL2_LIBRARIES})

else()

    # Empty fallback targets
    add_library(common INTERFACE)
    add_library(common-sdl INTERFACE)

endif()

# ----------------------------
# Main executable
# ----------------------------
add_executable(win-dictation WIN32
    src/main.cpp
    src/transcriber.cpp
    src/transcriber.h
    src/win-dictation.rc
)

target_link_libraries(win-dictation PRIVATE
    whisper
    ${SDL2_LIBRARIES}
    comctl32
    dwmapi
)

target_include_directories(win-dictation PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/common
)

target_compile_definitions(win-dictation PRIVATE UNICODE _UNICODE)

# ----------------------------
# Tests
# ----------------------------
add_executable(test-core
    tests/test_core.cpp
    src/transcriber.cpp
    src/transcriber.h
)
target_link_libraries(test-core PRIVATE whisper ${SDL2_LIBRARIES})
target_include_directories(test-core PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}/common
)
target_compile_definitions(test-core PRIVATE UNICODE _UNICODE)

# ----------------------------
# MSVC optimisations
# ----------------------------
if(MSVC)
    target_compile_options(win-dictation PRIVATE
        $<$<CONFIG:Release>:/O2 /GL>
    )
    target_link_options(win-dictation PRIVATE
        $<$<CONFIG:Release>:/LTCG>
    )
endif()

# ----------------------------
# Post build: runtime assets
# ----------------------------
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/deps/SDL2-2.28.5/lib/x64/SDL2.dll")
    add_custom_command(TARGET win-dictation POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${CMAKE_CURRENT_SOURCE_DIR}/deps/SDL2-2.28.5/lib/x64/SDL2.dll"
            $<TARGET_FILE_DIR:win-dictation>
    )
elseif(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/release/WinDictation/SDL2.dll")
    add_custom_command(TARGET win-dictation POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E copy_if_different
            "${CMAKE_CURRENT_SOURCE_DIR}/release/WinDictation/SDL2.dll"
            $<TARGET_FILE_DIR:win-dictation>
    )
endif()

add_custom_command(TARGET win-dictation POST_BUILD
    COMMAND ${CMAKE_COMMAND} -E make_directory $<TARGET_FILE_DIR:win-dictation>/models
    COMMAND ${CMAKE_COMMAND} -E copy_directory
        "${CMAKE_CURRENT_SOURCE_DIR}/models"
        $<TARGET_FILE_DIR:win-dictation>/models
)