cmake_minimum_required(VERSION 3.12)
project(nostr_core VERSION 1.0.0 LANGUAGES C)

# Set C standard
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

# Build options
option(NOSTR_BUILD_STATIC "Build static library" ON)
option(NOSTR_BUILD_SHARED "Build shared library" ON)
option(NOSTR_BUILD_EXAMPLES "Build examples" ON)
option(NOSTR_BUILD_TESTS "Build tests" ON)
option(NOSTR_ENABLE_WEBSOCKETS "Enable WebSocket support" ON)
option(NOSTR_USE_MBEDTLS "Use mbedTLS for crypto (otherwise use built-in)" OFF)

# Compiler flags
if(CMAKE_COMPILER_IS_GNUCC OR CMAKE_C_COMPILER_ID MATCHES "Clang")
    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Werror")
    set(CMAKE_C_FLAGS_DEBUG "-g -O0")
    set(CMAKE_C_FLAGS_RELEASE "-O3 -DNDEBUG")
endif()

# Include directories
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/cjson)

# Source files
set(NOSTR_CORE_SOURCES
    nostr_core/core.c
    nostr_core/core_relays.c
    nostr_core/core_relay_pool.c
    nostr_core/nostr_crypto.c
    nostr_core/nostr_secp256k1.c
    cjson/cJSON.c
)

set(NOSTR_CORE_HEADERS
    nostr_core.h
    nostr_crypto.h
    cjson/cJSON.h
)

# Add mbedTLS if enabled
if(NOSTR_USE_MBEDTLS)
    add_subdirectory(mbedtls)
    list(APPEND NOSTR_CORE_SOURCES mbedtls_wrapper.c)
    add_definitions(-DNOSTR_USE_MBEDTLS=1)
endif()

# Add WebSocket support if enabled
if(NOSTR_ENABLE_WEBSOCKETS)
    file(GLOB WEBSOCKET_SOURCES "nostr_websocket/*.c")
    file(GLOB WEBSOCKET_HEADERS "nostr_websocket/*.h")
    list(APPEND NOSTR_CORE_SOURCES ${WEBSOCKET_SOURCES})
    list(APPEND NOSTR_CORE_HEADERS ${WEBSOCKET_HEADERS})
    add_definitions(-DNOSTR_ENABLE_WEBSOCKETS=1)
endif()

# Create static library
if(NOSTR_BUILD_STATIC)
    add_library(nostr_core_static STATIC ${NOSTR_CORE_SOURCES})
    set_target_properties(nostr_core_static PROPERTIES OUTPUT_NAME nostr_core)
    target_link_libraries(nostr_core_static m)
    
    if(NOSTR_USE_MBEDTLS)
        target_link_libraries(nostr_core_static mbedcrypto mbedx509 mbedtls)
    endif()
endif()

# Create shared library
if(NOSTR_BUILD_SHARED)
    add_library(nostr_core_shared SHARED ${NOSTR_CORE_SOURCES})
    set_target_properties(nostr_core_shared PROPERTIES OUTPUT_NAME nostr_core)
    target_link_libraries(nostr_core_shared m)
    
    if(NOSTR_USE_MBEDTLS)
        target_link_libraries(nostr_core_shared mbedcrypto mbedx509 mbedtls)
    endif()
    
    # Set version information
    set_target_properties(nostr_core_shared PROPERTIES
        VERSION ${PROJECT_VERSION}
        SOVERSION 1
    )
endif()

# Create alias targets for easier integration
if(NOSTR_BUILD_STATIC)
    add_library(nostr_core::static ALIAS nostr_core_static)
endif()

if(NOSTR_BUILD_SHARED)
    add_library(nostr_core::shared ALIAS nostr_core_shared)
endif()

# Examples
if(NOSTR_BUILD_EXAMPLES)
    add_subdirectory(examples)
endif()

# Tests
if(NOSTR_BUILD_TESTS)
    enable_testing()
    add_subdirectory(tests)
endif()

# Installation
include(GNUInstallDirs)

# Install libraries
if(NOSTR_BUILD_STATIC)
    install(TARGETS nostr_core_static
        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
    )
endif()

if(NOSTR_BUILD_SHARED)
    install(TARGETS nostr_core_shared
        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
        RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
    )
endif()

# Install headers
install(FILES ${NOSTR_CORE_HEADERS}
    DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/nostr
)

# Install pkg-config file
configure_file(nostr_core.pc.in nostr_core.pc @ONLY)
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/nostr_core.pc
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
)

# Generate export configuration
include(CMakePackageConfigHelpers)

configure_package_config_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/nostr_core-config.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/nostr_core-config.cmake
    INSTALL_DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/nostr_core
)

write_basic_package_version_file(
    ${CMAKE_CURRENT_BINARY_DIR}/nostr_core-config-version.cmake
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion
)

install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/nostr_core-config.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/nostr_core-config-version.cmake
    DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/nostr_core
)

# Export targets
if(NOSTR_BUILD_STATIC OR NOSTR_BUILD_SHARED)
    set(EXPORT_TARGETS "")
    if(NOSTR_BUILD_STATIC)
        list(APPEND EXPORT_TARGETS nostr_core_static)
    endif()
    if(NOSTR_BUILD_SHARED)
        list(APPEND EXPORT_TARGETS nostr_core_shared)
    endif()
    
    install(EXPORT nostr_core-targets
        FILE nostr_core-targets.cmake
        NAMESPACE nostr_core::
        DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/nostr_core
    )
    
    export(TARGETS ${EXPORT_TARGETS}
        FILE ${CMAKE_CURRENT_BINARY_DIR}/nostr_core-targets.cmake
        NAMESPACE nostr_core::
    )
endif()

# Summary
message(STATUS "NOSTR Core Library Configuration:")
message(STATUS "  Version: ${PROJECT_VERSION}")
message(STATUS "  Build static library: ${NOSTR_BUILD_STATIC}")
message(STATUS "  Build shared library: ${NOSTR_BUILD_SHARED}")
message(STATUS "  Build examples: ${NOSTR_BUILD_EXAMPLES}")
message(STATUS "  Build tests: ${NOSTR_BUILD_TESTS}")
message(STATUS "  Enable WebSockets: ${NOSTR_ENABLE_WEBSOCKETS}")
message(STATUS "  Use mbedTLS: ${NOSTR_USE_MBEDTLS}")
message(STATUS "  Install prefix: ${CMAKE_INSTALL_PREFIX}")
