cmake_minimum_required(VERSION 3.20)
project(lumacs VERSION 0.1.0 LANGUAGES CXX)

# C++20 standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)

# Export compile commands for clangd/LSP
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

# Build type
if(NOT CMAKE_BUILD_TYPE)
    set(CMAKE_BUILD_TYPE Release)
endif()


# Dependencies
include(FetchContent)

# ncurses for TUI (better control key support)
find_package(Curses REQUIRED)

# GTK4 / gtkmm for GUI
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM gtkmm-4.0)

# Lua and sol2
find_package(Lua 5.4 REQUIRED)

# sol2 - use develop branch for latest compatibility fixes
FetchContent_Declare(
    sol2
    GIT_REPOSITORY https://github.com/ThePhD/sol2.git
    GIT_TAG v3.5.0
    GIT_SHALLOW TRUE
)
FetchContent_MakeAvailable(sol2)

# spdlog - try system package first (from Nix), fallback to FetchContent
find_package(spdlog)

if(NOT spdlog_FOUND)
    message(STATUS "System spdlog not found. Fetching via Git...")
    FetchContent_Declare(
        spdlog
        GIT_REPOSITORY https://github.com/gabime/spdlog.git
        GIT_TAG v1.14.1
    )
    FetchContent_MakeAvailable(spdlog)
else()
    message(STATUS "Found system spdlog: ${spdlog_VERSION}")
endif()

# Try to find system GoogleTest first (Nix-shell provided)
find_package(GTest)

if(GTest_FOUND)
    message(STATUS "Found system GoogleTest: ${GTest_VERSION}")
    # Note: System GTest exports targets as GTest::gtest and GTest::gtest_main
else()
    message(STATUS "System GoogleTest not found. Fetching via Git...")
    
    FetchContent_Declare(
        googletest
        GIT_REPOSITORY https://github.com/google/googletest.git
        GIT_TAG        v1.15.2  # Keep this updated to prevent the MacOS CMake 4.1 error!
    )
    FetchContent_MakeAvailable(googletest)
endif()

# Core library (UI-independent)
add_library(lumacs_core STATIC
    src/buffer.cpp
    src/window.cpp
    src/editor_core.cpp
    src/lua_api.cpp
    src/kill_ring.cpp
    src/theme.cpp
    src/config.cpp
    src/keybinding.cpp
    src/command_system.cpp
    src/modeline.cpp
    src/minibuffer_manager.cpp
    src/completion_system.cpp
    src/buffer_manager.cpp
    src/window_manager.cpp
    src/kill_ring_manager.cpp
    src/register_manager.cpp
    src/macro_manager.cpp
    src/rectangle_manager.cpp
    src/plugin_manager.cpp
    src/isearch_manager.cpp
    src/mode_activator.cpp
    src/logger.cpp
)

target_include_directories(lumacs_core PUBLIC
    ${CMAKE_CURRENT_SOURCE_DIR}/include
    ${LUA_INCLUDE_DIR}
    ${sol2_SOURCE_DIR}/include # Explicitly add sol2 include directory
)

target_link_libraries(lumacs_core PUBLIC
    sol2::sol2
    spdlog::spdlog
    ${LUA_LIBRARIES}
    ${CURSES_LIBRARIES}
)


# Main Executable (Single binary)
add_executable(lumacs
    src/main.cpp
    src/tui_editor.cpp
    src/gtk_editor.cpp
    src/gtk_renderer.cpp
    src/gtk_completion_popup.cpp
    src/command_system.cpp
)

target_link_libraries(lumacs PRIVATE
    lumacs_core
    ${CURSES_LIBRARIES}
)

target_include_directories(lumacs PRIVATE
    ${CURSES_INCLUDE_DIR}
)


# Helper variable for your flags
if(MSVC)
    set(LUMACS_WARNINGS /W4 /WX)
else()
    set(LUMACS_WARNINGS -Wall -Wextra -Wpedantic -Werror)
endif()

# Apply ONLY to your core library
target_compile_options(lumacs_core PRIVATE ${LUMACS_WARNINGS})

# Apply ONLY to your executable
target_compile_options(lumacs PRIVATE ${LUMACS_WARNINGS})


# Configure GTK if available
if(GTKMM_FOUND)
    message(STATUS "GTKMM 4.0 found - Building with GUI support")
    target_compile_definitions(lumacs PRIVATE LUMACS_WITH_GTK)
    target_include_directories(lumacs PRIVATE ${GTKMM_INCLUDE_DIRS})
    target_link_libraries(lumacs PRIVATE ${GTKMM_LIBRARIES})
else()
    message(WARNING "GTKMM 4.0 not found - Building TUI only")
endif()

# Enable testing
enable_testing()
add_subdirectory(tests)
