cmake_minimum_required(VERSION 3.14)
project(permitcore-sdk VERSION 1.1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# ── Library ───────────────────────────────────────────────────────────────────

add_library(permitcore
    src/permit_client.cpp
)

target_include_directories(permitcore PUBLIC
    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
    $<INSTALL_INTERFACE:include>
)

find_package(CURL REQUIRED)
target_link_libraries(permitcore PUBLIC CURL::libcurl)

# OpenSSL is used directly for offline activation token verification (ECDSA P-256 /
# SHA-256, see permit_client.cpp). This is very likely already present transitively via
# libcurl's own OpenSSL linkage on most systems, but declaring it explicitly here makes
# it a direct SDK dependency rather than an accidental one, and keeps the build working
# on systems where curl is linked against a different TLS backend (e.g. Schannel/LibreSSL).
find_package(OpenSSL REQUIRED)
target_link_libraries(permitcore PUBLIC OpenSSL::SSL OpenSSL::Crypto)

# ── Example ───────────────────────────────────────────────────────────────────

add_executable(example_validate examples/validate_example.cpp)
target_link_libraries(example_validate PRIVATE permitcore)

# ── Tests ─────────────────────────────────────────────────────────────────────
#
# Shared cross-SDK protocol test-vector suite (CTO review F-27) — every SDK
# independently verifies the same fixed set of signed offline/grace-cache tokens
# against SDKs/test-vectors/vectors.json. No framework (GoogleTest/Catch2) is
# vendored in this SDK, so this is a small dependency-free assert-and-report
# executable; ctest just checks its exit code.

enable_testing()

add_executable(permitcore_test_vectors tests/test_vectors.cpp)
target_link_libraries(permitcore_test_vectors PRIVATE permitcore)

# Absolute path baked in at configure time rather than relying on the test binary's
# CWD at run time (which differs between a plain invocation, `ctest`, and an IDE's
# "run" button, and differs again across single- vs multi-config generators).
get_filename_component(PERMITCORE_TEST_VECTORS_JSON
    "${CMAKE_CURRENT_SOURCE_DIR}/../test-vectors/vectors.json" ABSOLUTE)
target_compile_definitions(permitcore_test_vectors PRIVATE
    PERMITCORE_TEST_VECTORS_JSON="${PERMITCORE_TEST_VECTORS_JSON}"
)

add_test(NAME permitcore_test_vectors COMMAND permitcore_test_vectors)

# ── Install ───────────────────────────────────────────────────────────────────

install(TARGETS permitcore EXPORT permitcoreTargets
    LIBRARY  DESTINATION lib
    ARCHIVE  DESTINATION lib
    RUNTIME  DESTINATION bin
)
install(DIRECTORY include/ DESTINATION include)

# CMake config package — without this, a consumer's find_package(permitcore-sdk CONFIG)
# has nothing to find, and vcpkg's own post-build checks reject a library port that
# doesn't provide one.
include(CMakePackageConfigHelpers)
write_basic_package_version_file(
    "${CMAKE_CURRENT_BINARY_DIR}/permitcore-sdk-config-version.cmake"
    VERSION ${PROJECT_VERSION}
    COMPATIBILITY SameMajorVersion
)

install(EXPORT permitcoreTargets
    FILE permitcore-sdk-targets.cmake
    NAMESPACE permitcore::
    DESTINATION lib/cmake/permitcore-sdk
)

configure_package_config_file(
    ${CMAKE_CURRENT_SOURCE_DIR}/cmake/permitcore-sdk-config.cmake.in
    ${CMAKE_CURRENT_BINARY_DIR}/permitcore-sdk-config.cmake
    INSTALL_DESTINATION lib/cmake/permitcore-sdk
)

install(FILES
    ${CMAKE_CURRENT_BINARY_DIR}/permitcore-sdk-config.cmake
    ${CMAKE_CURRENT_BINARY_DIR}/permitcore-sdk-config-version.cmake
    DESTINATION lib/cmake/permitcore-sdk
)
