cmake_minimum_required(VERSION 3.18)
project(mosis-core VERSION 1.0.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Core library sources - portable sandbox APIs
set(MOSIS_CORE_SOURCES
    # Utility
    src/logger.cpp

    # Sandbox APIs (portable)
    src/sandbox/timer_manager.cpp
    src/sandbox/json_api.cpp
    src/sandbox/crypto_api.cpp
    src/sandbox/virtual_fs.cpp
    src/sandbox/lua_sandbox.cpp
    src/sandbox/permission_gate.cpp
    src/sandbox/audit_log.cpp
    src/sandbox/rate_limiter.cpp
    src/sandbox/path_sandbox.cpp
)

# Optional sources that require additional dependencies
if(MOSIS_ENABLE_DATABASE)
    list(APPEND MOSIS_CORE_SOURCES src/sandbox/database_manager.cpp)
endif()

if(MOSIS_ENABLE_NETWORK)
    list(APPEND MOSIS_CORE_SOURCES
        src/sandbox/network_manager.cpp
        src/sandbox/http_validator.cpp
    )
endif()

# Create static library
add_library(mosis-core STATIC ${MOSIS_CORE_SOURCES})

# Include directories
target_include_directories(mosis-core PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include/mosis>
    $<INSTALL_INTERFACE:include>
)

# Also add internal include path for relative includes within library
target_include_directories(mosis-core PRIVATE
    ${CMAKE_CURRENT_SOURCE_DIR}/include/mosis/sandbox
    ${CMAKE_CURRENT_SOURCE_DIR}/include/mosis/util
    ${CMAKE_CURRENT_SOURCE_DIR}/include/mosis/apps
)

# Platform-specific definitions
if(ANDROID)
    target_compile_definitions(mosis-core PUBLIC MOSIS_PLATFORM_ANDROID)
elseif(WIN32)
    target_compile_definitions(mosis-core PUBLIC MOSIS_PLATFORM_WINDOWS)
    # Windows crypto library
    target_link_libraries(mosis-core PRIVATE bcrypt)
elseif(APPLE)
    target_compile_definitions(mosis-core PUBLIC MOSIS_PLATFORM_MACOS)
else()
    target_compile_definitions(mosis-core PUBLIC MOSIS_PLATFORM_LINUX)
endif()

# OpenSSL for crypto on non-Windows platforms
if(NOT WIN32)
    find_package(OpenSSL QUIET)
    if(OpenSSL_FOUND)
        target_link_libraries(mosis-core PRIVATE OpenSSL::Crypto)
        target_compile_definitions(mosis-core PRIVATE MOSIS_HAS_OPENSSL)
    else()
        message(WARNING "OpenSSL not found - crypto functions will be stubs")
    endif()
endif()

# Lua is required - parent project must provide it
if(TARGET lua_static)
    target_link_libraries(mosis-core PUBLIC lua_static)
elseif(TARGET lua)
    target_link_libraries(mosis-core PUBLIC lua)
else()
    # Try to find Lua
    find_package(Lua QUIET)
    if(LUA_FOUND)
        target_include_directories(mosis-core PUBLIC ${LUA_INCLUDE_DIR})
        target_link_libraries(mosis-core PUBLIC ${LUA_LIBRARIES})
    else()
        message(WARNING "Lua not found - parent project must provide Lua target")
    endif()
endif()

# JSON library (nlohmann/json - header only)
if(TARGET nlohmann_json::nlohmann_json)
    target_link_libraries(mosis-core PUBLIC nlohmann_json::nlohmann_json)
else()
    find_package(nlohmann_json QUIET)
    if(nlohmann_json_FOUND)
        target_link_libraries(mosis-core PUBLIC nlohmann_json::nlohmann_json)
    endif()
endif()

# SQLite for database_manager (optional)
if(MOSIS_ENABLE_DATABASE)
    find_package(SQLite3 REQUIRED)
    target_link_libraries(mosis-core PRIVATE SQLite::SQLite3)
    target_compile_definitions(mosis-core PRIVATE MOSIS_HAS_SQLITE)
endif()

# Export compile commands for IDE support
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
