cmake_minimum_required(VERSION 3.20)
project(YourPartDaemon VERSION 1.0 LANGUAGES CXX)

# C++ Standard and Compiler Settings
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# Use best available GCC for C++23 support (OpenSUSE Tumbleweed)
# Try GCC 15 first (best C++23 support), then GCC 13, then system default
find_program(GCC15_CC gcc-15)
find_program(GCC15_CXX g++-15)
find_program(GCC13_CC gcc-13)
find_program(GCC13_CXX g++-13)

if(GCC15_CC AND GCC15_CXX)
    set(CMAKE_C_COMPILER ${GCC15_CC})
    set(CMAKE_CXX_COMPILER ${GCC15_CXX})
    message(STATUS "Using GCC 15 for best C++23 support")
elseif(GCC13_CC AND GCC13_CXX)
    set(CMAKE_C_COMPILER ${GCC13_CC})
    set(CMAKE_CXX_COMPILER ${GCC13_CXX})
    message(STATUS "Using GCC 13 for C++23 support")
else()
    message(STATUS "Using system default compiler")
endif()
# Optimize for GCC 13 with C++23
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto=auto -O3 -march=native -mtune=native")
set(CMAKE_CXX_FLAGS_DEBUG "-O1 -g -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -DNDEBUG -march=native -mtune=native")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
set(CMAKE_BUILD_TYPE Release)

# Include /usr/local if needed
list(APPEND CMAKE_PREFIX_PATH /usr/local)

# Find libwebsockets via pkg-config
find_package(PkgConfig REQUIRED)
pkg_check_modules(LWS REQUIRED libwebsockets)

# Find other dependencies
find_package(PostgreSQL REQUIRED)
find_package(Threads REQUIRED)
find_package(nlohmann_json CONFIG REQUIRED)

# PostgreSQL C++ libpqxx
find_package(PkgConfig REQUIRED)
pkg_check_modules(LIBPQXX REQUIRED libpqxx)

# Project sources and headers
set(SOURCES
    src/main.cpp
    src/config.cpp
    src/connection_pool.cpp
    src/database.cpp
    src/character_creation_worker.cpp
    src/produce_worker.cpp
    src/message_broker.cpp
    src/websocket_server.cpp
    src/stockagemanager.cpp
    src/director_worker.cpp
    src/valuerecalculationworker.cpp
    src/usercharacterworker.cpp
    src/houseworker.cpp
    src/politics_worker.cpp
)

set(HEADERS
    src/config.h
    src/database.h
    src/connection_pool.h
    src/worker.h
    src/character_creation_worker.h
    src/produce_worker.h
    src/message_broker.h
    src/websocket_server.h
    src/stockagemanager.h
    src/director_worker.h
    src/valuerecalculationworker.h
    src/usercharacterworker.h
    src/houseworker.h
    src/politics_worker.h
)

# Define executable target
add_executable(yourpart-daemon ${SOURCES} ${HEADERS}
  src/utils.h src/utils.cpp
  src/underground_worker.h src/underground_worker.cpp)

# Include directories
target_include_directories(yourpart-daemon PRIVATE
  ${PostgreSQL_INCLUDE_DIRS}
  ${LIBPQXX_INCLUDE_DIRS}
  ${LWS_INCLUDE_DIRS}
)

# Find systemd
find_package(PkgConfig REQUIRED)
pkg_check_modules(SYSTEMD REQUIRED libsystemd)

# Link libraries
target_link_libraries(yourpart-daemon PRIVATE
  ${PostgreSQL_LIBRARIES}
  Threads::Threads
  z ssl crypto
  ${LIBPQXX_LIBRARIES}
  ${LWS_LIBRARIES}
  nlohmann_json::nlohmann_json
  ${SYSTEMD_LIBRARIES}
)

# Installation rules
install(TARGETS yourpart-daemon DESTINATION /usr/local/bin)

# Installiere Template als Referenz ZUERST (wird vom install-Skript benötigt)
install(FILES daemon.conf DESTINATION /etc/yourpart/ RENAME daemon.conf.example)

# Intelligente Konfigurationsdatei-Installation
# Verwendet ein CMake-Skript, das nur fehlende Keys hinzufügt, ohne bestehende zu überschreiben
# Das Skript liest das Template aus /etc/yourpart/daemon.conf.example oder dem Source-Verzeichnis
install(SCRIPT cmake/install-config.cmake)
