# ============================================================================
# AISSIA Integration Tests
# ============================================================================

# Fetch Catch2
include(FetchContent)
FetchContent_Declare(
    Catch2
    GIT_REPOSITORY https://github.com/catchorg/Catch2.git
    GIT_TAG v3.4.0
)
FetchContent_MakeAvailable(Catch2)

# ============================================================================
# Test executable
# ============================================================================
add_executable(aissia_tests
    main.cpp

    # Mocks
    mocks/MockIO.cpp

    # Module sources (needed for testing)
    ${CMAKE_SOURCE_DIR}/src/modules/SchedulerModule.cpp
    ${CMAKE_SOURCE_DIR}/src/modules/NotificationModule.cpp
    ${CMAKE_SOURCE_DIR}/src/modules/MonitoringModule.cpp
    ${CMAKE_SOURCE_DIR}/src/modules/AIModule.cpp
    ${CMAKE_SOURCE_DIR}/src/modules/VoiceModule.cpp
    ${CMAKE_SOURCE_DIR}/src/modules/StorageModule.cpp
    ${CMAKE_SOURCE_DIR}/src/modules/WebModule.cpp

    # Module tests (70 TI)
    modules/SchedulerModuleTests.cpp
    modules/NotificationModuleTests.cpp
    modules/MonitoringModuleTests.cpp
    modules/AIModuleTests.cpp
    modules/VoiceModuleTests.cpp
    modules/StorageModuleTests.cpp
    modules/WebModuleTests.cpp

    # MCP tests (50 TI)
    mcp/MCPTypesTests.cpp
    mcp/StdioTransportTests.cpp
    mcp/MCPClientTests.cpp
)

target_link_libraries(aissia_tests PRIVATE
    Catch2::Catch2WithMain
    GroveEngine::impl
    AissiaTools
    spdlog::spdlog
)

# WebModule needs httplib and OpenSSL
target_include_directories(aissia_tests PRIVATE
    ${httplib_SOURCE_DIR}
)
if(OPENSSL_FOUND)
    target_link_libraries(aissia_tests PRIVATE OpenSSL::SSL OpenSSL::Crypto)
    target_compile_definitions(aissia_tests PRIVATE CPPHTTPLIB_OPENSSL_SUPPORT)
endif()

# Disable module factory functions during testing
target_compile_definitions(aissia_tests PRIVATE AISSIA_TEST_BUILD)

target_include_directories(aissia_tests PRIVATE
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_CURRENT_SOURCE_DIR}
)

# ============================================================================
# Copy test fixtures to build directory
# ============================================================================
file(COPY ${CMAKE_CURRENT_SOURCE_DIR}/fixtures/
     DESTINATION ${CMAKE_BINARY_DIR}/tests/fixtures)

# ============================================================================
# CTest integration
# ============================================================================
include(CTest)
# Note: catch_discover_tests requires running the exe at build time
# which can fail due to missing DLLs. Use manual test registration instead.
add_test(NAME aissia_tests COMMAND aissia_tests)

# ============================================================================
# Custom targets
# ============================================================================

# Run all tests
add_custom_target(test_all
    COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
    DEPENDS aissia_tests
    COMMENT "Running all integration tests"
)

# Run module tests only
add_custom_target(test_modules
    COMMAND $<TARGET_FILE:aissia_tests> "[scheduler],[notification],[monitoring],[ai],[voice],[storage],[web]"
    DEPENDS aissia_tests
    COMMENT "Running module integration tests"
)

# Run MCP tests only
add_custom_target(test_mcp
    COMMAND $<TARGET_FILE:aissia_tests> "[mcp]"
    DEPENDS aissia_tests
    COMMENT "Running MCP integration tests"
)

# ============================================================================
# Integration Test Modules (Dynamic .so files)
# ============================================================================

# Helper macro to create integration test modules
macro(add_integration_test TEST_NAME)
    add_library(${TEST_NAME} SHARED
        integration/${TEST_NAME}.cpp
    )
    target_include_directories(${TEST_NAME} PRIVATE
        ${CMAKE_SOURCE_DIR}/src
    )
    target_link_libraries(${TEST_NAME} PRIVATE
        GroveEngine::impl
        spdlog::spdlog
    )
    set_target_properties(${TEST_NAME} PROPERTIES
        PREFIX ""
        LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests/integration
    )
endmacro()

# Individual integration test modules (will be added as we create them)
# Phase 2: MCP Tests
add_integration_test(IT_001_GetCurrentTime)
add_integration_test(IT_002_FileSystemWrite)
add_integration_test(IT_003_FileSystemRead)
add_integration_test(IT_004_MCPToolsList)

# Phase 3: Flow Tests
add_integration_test(IT_005_VoiceToAI)
add_integration_test(IT_006_AIToLLM)
add_integration_test(IT_007_StorageWrite)
add_integration_test(IT_008_StorageRead)

# Phase 4: End-to-End Test
add_integration_test(IT_009_FullConversationLoop)

# Phase 5: Module Tests
add_integration_test(IT_010_SchedulerHyperfocus)
add_integration_test(IT_011_NotificationAlert)
add_integration_test(IT_012_MonitoringActivity)
add_integration_test(IT_013_WebRequest)

# Custom target to build all integration tests
add_custom_target(integration_tests
    DEPENDS
        IT_001_GetCurrentTime
        IT_002_FileSystemWrite
        IT_003_FileSystemRead
        IT_004_MCPToolsList
        IT_005_VoiceToAI
        IT_006_AIToLLM
        IT_007_StorageWrite
        IT_008_StorageRead
        IT_009_FullConversationLoop
        IT_010_SchedulerHyperfocus
        IT_011_NotificationAlert
        IT_012_MonitoringActivity
        IT_013_WebRequest
    COMMENT "Building all integration test modules"
)

