# SPDX-License-Identifier: MPL-2.0

configure_file(src/version_config.hpp.in "${CMAKE_CURRENT_BINARY_DIR}/generated/sequa/core/version_config.hpp" @ONLY)

# --- sequa::core : simulation, rejeu (aucune dépendance réseau) -------------------------
add_library(sequa_core
    src/version.cpp
    src/sim/simulation.cpp
    src/sim/replay.cpp)
add_library(sequa::core ALIAS sequa_core)

target_include_directories(sequa_core
    PUBLIC
        "${CMAKE_CURRENT_SOURCE_DIR}/include"
    PRIVATE
        "${CMAKE_CURRENT_SOURCE_DIR}/src"
        "${CMAKE_CURRENT_BINARY_DIR}/generated")
target_compile_features(sequa_core PUBLIC cxx_std_20)
# Enregistrement : table Recording du contrat (ADR 0013), en-têtes générés.
target_link_libraries(sequa_core PUBLIC sequa::contract)
sequa_target_defaults(sequa_core)

# --- sequa::core_render : projection du monde simulé vers la scène SEQUA 3D --------------
add_library(sequa_core_render src/render/scene_projection.cpp)
add_library(sequa::core_render ALIAS sequa_core_render)
target_link_libraries(sequa_core_render PUBLIC sequa::core sequa::3d)
sequa_target_defaults(sequa_core_render)

# --- sequa::core_server : protocole Studio - Core et serveur WebSocket ------------------
if(SEQUA_BUILD_SERVER)
    # --- sequa::core_project : projet ouvert, fichiers du projet (ADR 0015) -------------
    # Lecture et écriture des fichiers par le code généré (contract/generated/cpp/project/),
    # fondé sur nlohmann/json, récupéré avec le serveur.
    add_library(sequa_core_project
        src/project/file_browser.cpp
        src/project/project_store.cpp
        src/project/recent_projects.cpp)
    add_library(sequa::core_project ALIAS sequa_core_project)
    target_include_directories(sequa_core_project PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
    target_compile_features(sequa_core_project PUBLIC cxx_std_20)
    target_link_libraries(sequa_core_project PRIVATE sequa::contract nlohmann_json::nlohmann_json)
    sequa_target_defaults(sequa_core_project)

    # --- sequa::core_catalog : catalogue d'assets (ADR 0016) ---------------------------
    # SQLite (amalgamation) et BLAKE3, tous deux récupérés avec le serveur ; l'API C de
    # SQLite reste confinée à sqlite_catalog.cpp et au code généré (contract/generated/cpp/catalog/).
    add_library(sequa_core_catalog
        src/catalog/content_hash.cpp
        src/catalog/sqlite_catalog.cpp)
    add_library(sequa::core_catalog ALIAS sequa_core_catalog)
    target_include_directories(sequa_core_catalog PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
    target_compile_features(sequa_core_catalog PUBLIC cxx_std_20)
    # Les lignes du catalogue (types générés, fondés sur nlohmann/json) font partie de l'interface.
    target_link_libraries(sequa_core_catalog
        PUBLIC sequa::core_project sequa::contract nlohmann_json::nlohmann_json
        PRIVATE sequa::sqlite sequa::blake3)
    sequa_target_defaults(sequa_core_catalog)

    add_library(sequa_core_server
        src/server/live_session.cpp
        src/server/protocol.cpp
        src/server/protocol_log.cpp
        src/server/render_contract.cpp
        src/server/render_hub.cpp
        src/server/render_supervisor.cpp
        src/server/runtime.cpp
        src/server/server.cpp
        src/server/studio_contract.cpp)
    target_include_directories(sequa_core_server PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/server")
    add_library(sequa::core_server ALIAS sequa_core_server)
    target_link_libraries(sequa_core_server
        PUBLIC sequa::core sequa::3d sequa::core_project
        PRIVATE sequa::core_render sequa::3d_wire sequa::process sequa::layout sequa::contract ixwebsocket::ixwebsocket)
    if(WIN32)
        target_link_libraries(sequa_core_server PRIVATE ws2_32) # vérification exclusive du port
    endif()
    sequa_target_defaults(sequa_core_server)

    add_executable(sequa_core_app app/main.cpp)
    set_target_properties(sequa_core_app PROPERTIES OUTPUT_NAME sequa-core)
    # L'application utilise le superviseur du rendu, interne à sequa_core_server.
    target_include_directories(sequa_core_app PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/server")
    target_link_libraries(sequa_core_app PRIVATE sequa::core_server sequa::core_catalog sequa::core_render sequa::process sequa::layout sequa::measures)
    sequa_target_defaults(sequa_core_app)
    sequa_install_executable(sequa_core_app core)
endif()

# --- Tests ------------------------------------------------------------------------------
if(SEQUA_BUILD_TESTS)
    set(_sequa_core_test_sources
        tests/main.cpp
        tests/test_version.cpp
        tests/sim/test_fixed.cpp
        tests/sim/test_rng.cpp
        tests/sim/test_clock.cpp
        tests/sim/test_simulation.cpp
        tests/sim/test_replay.cpp
        tests/render/test_scene_projection.cpp)
    if(SEQUA_BUILD_SERVER)
        list(APPEND _sequa_core_test_sources
            tests/server/test_live_session.cpp
            tests/server/test_protocol.cpp
            tests/server/test_protocol_log.cpp
            tests/server/test_render_channel.cpp
            tests/server/test_render_contract.cpp
            tests/server/test_runtime.cpp
            tests/server/test_server.cpp
            tests/server/test_studio_contract.cpp
            tests/project/test_uuid.cpp
            tests/project/test_file_browser.cpp
            tests/project/test_project_files.cpp
            tests/project/test_project_store.cpp
            tests/project/test_project_open.cpp
            tests/project/test_project_spaces.cpp
            tests/project/test_recent_projects.cpp
            tests/catalog/test_content_hash.cpp
            tests/catalog/test_catalog_tables.cpp
            tests/catalog/test_sqlite_catalog.cpp)
    endif()

    add_executable(sequa_core_tests ${_sequa_core_test_sources})
    target_link_libraries(sequa_core_tests PRIVATE sequa::core sequa::core_render doctest::doctest)
    if(SEQUA_BUILD_SERVER)
        target_link_libraries(sequa_core_tests PRIVATE
            sequa::core_server sequa::core_project sequa::core_catalog sequa::3d_wire sequa::contract ixwebsocket::ixwebsocket
            nlohmann_json::nlohmann_json sequa::sqlite)
        # Les liaisons du contrat (studio_contract, render_contract) sont internes à sequa_core_server.
        target_include_directories(sequa_core_tests PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/src/server")
    endif()
    sequa_target_defaults(sequa_core_tests)
    doctest_discover_tests(sequa_core_tests)
endif()
