/*
 * NIP-17: Private Direct Messages
 * https://github.com/nostr-protocol/nips/blob/master/17.md
 */

#ifndef NOSTR_NIP017_H
#define NOSTR_NIP017_H

#include <stddef.h>
#include "../cjson/cJSON.h"

#ifdef __cplusplus
extern "C" {
#endif

/**
 * NIP-17: Create a chat message event (kind 14)
 *
 * @param message Plain text message content
 * @param recipient_pubkeys Array of recipient public keys (hex strings)
 * @param num_recipients Number of recipients
 * @param subject Optional conversation subject/title (can be NULL)
 * @param reply_to_event_id Optional event ID this message replies to (can be NULL)
 * @param reply_relay_url Optional relay URL for reply reference (can be NULL)
 * @param sender_pubkey_hex Sender's public key in hex format
 * @return cJSON object representing the unsigned chat event, or NULL on error
 */
cJSON* nostr_nip17_create_chat_event(const char* message,
                                    const char** recipient_pubkeys,
                                    int num_recipients,
                                    const char* subject,
                                    const char* reply_to_event_id,
                                    const char* reply_relay_url,
                                    const char* sender_pubkey_hex);

/**
 * NIP-17: Create a file message event (kind 15)
 *
 * @param file_url URL of the encrypted file
 * @param file_type MIME type of the original file (e.g., "image/jpeg")
 * @param encryption_algorithm Encryption algorithm used ("aes-gcm")
 * @param decryption_key Base64-encoded decryption key
 * @param decryption_nonce Base64-encoded decryption nonce
 * @param file_hash SHA-256 hash of the encrypted file (hex)
 * @param original_file_hash SHA-256 hash of the original file before encryption (hex, optional)
 * @param file_size Size of encrypted file in bytes (optional, 0 to skip)
 * @param dimensions Image dimensions in "WxH" format (optional, NULL to skip)
 * @param blurhash Blurhash for preview (optional, NULL to skip)
 * @param thumbnail_url URL of encrypted thumbnail (optional, NULL to skip)
 * @param recipient_pubkeys Array of recipient public keys (hex strings)
 * @param num_recipients Number of recipients
 * @param subject Optional conversation subject/title (can be NULL)
 * @param reply_to_event_id Optional event ID this message replies to (can be NULL)
 * @param reply_relay_url Optional relay URL for reply reference (can be NULL)
 * @param sender_pubkey_hex Sender's public key in hex format
 * @return cJSON object representing the unsigned file event, or NULL on error
 */
cJSON* nostr_nip17_create_file_event(const char* file_url,
                                   const char* file_type,
                                   const char* encryption_algorithm,
                                   const char* decryption_key,
                                   const char* decryption_nonce,
                                   const char* file_hash,
                                   const char* original_file_hash,
                                   size_t file_size,
                                   const char* dimensions,
                                   const char* blurhash,
                                   const char* thumbnail_url,
                                   const char** recipient_pubkeys,
                                   int num_recipients,
                                   const char* subject,
                                   const char* reply_to_event_id,
                                   const char* reply_relay_url,
                                   const char* sender_pubkey_hex);

/**
 * NIP-17: Create a relay list event (kind 10050)
 *
 * @param relay_urls Array of relay URLs for DM delivery
 * @param num_relays Number of relay URLs
 * @param private_key Sender's private key for signing
 * @return cJSON object representing the signed relay list event, or NULL on error
 */
cJSON* nostr_nip17_create_relay_list_event(const char** relay_urls,
                                         int num_relays,
                                         const unsigned char* private_key);

/**
 * NIP-17: Send a direct message to recipients
 *
 * This function creates the appropriate rumor, seals it, gift wraps it,
 * and returns the final gift wrap events ready for publishing.
 *
 * @param dm_event The unsigned DM event (kind 14 or 15)
 * @param recipient_pubkeys Array of recipient public keys (hex strings)
 * @param num_recipients Number of recipients
 * @param sender_private_key 32-byte sender private key
 * @param gift_wraps_out Array to store resulting gift wrap events (caller must free)
 * @param max_gift_wraps Maximum number of gift wraps to create
 * @param max_delay_sec Maximum random timestamp delay in seconds (0 = no randomization)
 * @return Number of gift wrap events created, or -1 on error
 */
int nostr_nip17_send_dm(cJSON* dm_event,
                       const char** recipient_pubkeys,
                       int num_recipients,
                       const unsigned char* sender_private_key,
                       cJSON** gift_wraps_out,
                       int max_gift_wraps,
                       long max_delay_sec);

/**
 * NIP-17: Receive and decrypt a direct message
 *
 * This function unwraps a gift wrap, unseals the rumor, and returns the original DM event.
 *
 * @param gift_wrap The received gift wrap event (kind 1059)
 * @param recipient_private_key 32-byte recipient private key
 * @return cJSON object representing the decrypted DM event, or NULL on error
 */
cJSON* nostr_nip17_receive_dm(cJSON* gift_wrap,
                            const unsigned char* recipient_private_key);

/**
 * NIP-17: Extract DM relay URLs from a user's kind 10050 event
 *
 * @param relay_list_event The kind 10050 event
 * @param relay_urls_out Array to store extracted relay URLs (caller must free)
 * @param max_relays Maximum number of relays to extract
 * @return Number of relay URLs extracted, or -1 on error
 */
int nostr_nip17_extract_dm_relays(cJSON* relay_list_event,
                                char** relay_urls_out,
                                int max_relays);

#ifdef __cplusplus
}
#endif

#endif // NOSTR_NIP017_H