# src/CMakeLists.txt - Build configuration for the main executable

# Collect source files
set(SOURCES
    main.cpp
    server.cpp
    model_manager.cpp
    model_detector.cpp
    generation_queue.cpp
    stable_diffusion_wrapper.cpp
    logger.cpp
    jwt_auth.cpp
    user_manager.cpp
    auth_middleware.cpp
)

# Collect header files
set(HEADERS
    ../include/server.h
    ../include/model_manager.h
    ../include/model_detector.h
    ../include/generation_queue.h
    ../include/stable_diffusion_wrapper.h
    ../include/jwt_auth.h
    ../include/user_manager.h
    ../include/auth_middleware.h
    ../include/server_config.h
)

# Add PAM authentication files if enabled
if(ENABLE_PAM_AUTH AND PAM_FOUND)
    list(APPEND SOURCES pam_auth.cpp)
    list(APPEND HEADERS ../include/pam_auth.h)
    message(STATUS "PAM authentication support enabled")
else()
    message(STATUS "PAM authentication support disabled")
endif()

# Create the executable target
add_executable(stable-diffusion-rest-server ${SOURCES} ${HEADERS})

# Set target properties
set_target_properties(stable-diffusion-rest-server PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
)

# Add include directories
target_include_directories(stable-diffusion-rest-server PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/../include
)

# Link against dependencies
target_link_libraries(stable-diffusion-rest-server PRIVATE
    sd-cpp
    ggml
    ggml-base
    ggml-cpu
    ${DEPENDENCY_LIBRARIES}
    OpenMP::OpenMP_CXX
)

# Link PAM library if enabled
if(ENABLE_PAM_AUTH AND PAM_FOUND)
    target_link_libraries(stable-diffusion-rest-server PRIVATE ${PAM_LIBRARIES})
endif()

# Set compiler flags based on build type
target_compile_options(stable-diffusion-rest-server PRIVATE
    $<$<CONFIG:Debug>:-g -O0 -Wall -Wextra>
    $<$<CONFIG:Release>:-O3 -DNDEBUG>
)

# Add CUDA flags if CUDA is enabled
if(SD_CUDA_SUPPORT AND CUDA_FOUND)
    target_compile_definitions(stable-diffusion-rest-server PRIVATE SD_CUDA_SUPPORT)
endif()

# Add PAM flags if PAM is enabled
if(ENABLE_PAM_AUTH AND PAM_FOUND)
    target_compile_definitions(stable-diffusion-rest-server PRIVATE ENABLE_PAM_AUTH)
endif()

# Add dependency on web UI build if enabled
if(BUILD_WEBUI AND WEBUI_BUILT)
    add_dependencies(stable-diffusion-rest-server webui-build)
    message(STATUS "stable-diffusion-rest-server will build web UI automatically")
endif()

# Install target
install(TARGETS stable-diffusion-rest-server
    RUNTIME DESTINATION bin
)

# Install headers
install(FILES ${HEADERS}
    DESTINATION include/stable-diffusion-rest
)

# Add the test for model detection
if (CMAKE_BUILD_TYPE STREQUAL "Debug")
    add_executable(test_model_detection
        ../test_model_detection.cpp
        model_detector.cpp
        stable_diffusion_wrapper.cpp
        model_manager.cpp
        generation_queue.cpp
        logger.cpp
    )
endif()
# Set target properties
set_target_properties(test_model_detection PROPERTIES
    CXX_STANDARD 17
    CXX_STANDARD_REQUIRED ON
    CXX_EXTENSIONS OFF
)

# Add include directories
target_include_directories(test_model_detection PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/../include
)

# Link against dependencies - same as main server executable
target_link_libraries(test_model_detection PRIVATE
    sd-cpp
    ggml
    ggml-base
    ggml-cpu
    ${DEPENDENCY_LIBRARIES}
    OpenMP::OpenMP_CXX
    nlohmann_json::nlohmann_json
    pthread
    ${CMAKE_DL_LIBS}
)

# Add Apple frameworks if needed
if(APPLE)
    target_link_libraries(test_model_detection PRIVATE "-framework Foundation" "-framework Metal" "-framework MetalKit")
endif()

# Add dependency on sd-cpp target to ensure correct build order
add_dependencies(test_model_detection sd-cpp)

message(STATUS "Configured stable-diffusion-rest-server executable")
