package dev.quicproquo

/**
 * JNI bridge to libquicproquo_ffi native library.
 *
 * Load the library with:
 * ```kotlin
 * System.loadLibrary("quicproquo_ffi")
 * ```
 *
 * Or set `java.library.path` to the directory containing the shared object.
 */
internal object NativeBridge {

    /** Status codes (mirrors crates/quicproquo-ffi/src/lib.rs). */
    const val QPQ_OK = 0
    const val QPQ_ERROR = 1
    const val QPQ_AUTH_FAILED = 2
    const val QPQ_TIMEOUT = 3
    const val QPQ_NOT_CONNECTED = 4

    init {
        System.loadLibrary("quicproquo_ffi")
    }

    /**
     * Connect to a server. Returns a native handle (pointer as long),
     * or 0 on failure.
     */
    @JvmStatic
    external fun nativeConnect(server: String, caCert: String, serverName: String): Long

    /** Authenticate with OPAQUE. Returns a status code. */
    @JvmStatic
    external fun nativeLogin(handle: Long, username: String, password: String): Int

    /** Send a message. Returns a status code. */
    @JvmStatic
    external fun nativeSend(handle: Long, recipient: String, message: ByteArray): Int

    /** Receive messages. Returns JSON string or null. Throws on error. */
    @JvmStatic
    external fun nativeReceive(handle: Long, timeoutMs: Int): String?

    /** Disconnect and free the handle. */
    @JvmStatic
    external fun nativeDisconnect(handle: Long)

    /** Get last error message, or null. */
    @JvmStatic
    external fun nativeLastError(handle: Long): String?
}
