/*
 * NOSTR Core Library - NIP-013: Proof of Work
 */

#ifndef NIP013_H
#define NIP013_H

#include "nip001.h"
#include <stdint.h>

// PoW validation flags
#define NOSTR_POW_VALIDATE_NONCE_TAG     0x01  // Require and validate nonce tag format
#define NOSTR_POW_VALIDATE_TARGET_COMMIT  0x02  // Validate committed target difficulty
#define NOSTR_POW_REJECT_LOWER_TARGET    0x04  // Reject if committed target < actual difficulty
#define NOSTR_POW_STRICT_FORMAT          0x08  // Strict nonce tag format validation

// Convenience combinations
#define NOSTR_POW_VALIDATE_BASIC         (NOSTR_POW_VALIDATE_NONCE_TAG)
#define NOSTR_POW_VALIDATE_FULL          (NOSTR_POW_VALIDATE_NONCE_TAG | NOSTR_POW_VALIDATE_TARGET_COMMIT)
#define NOSTR_POW_VALIDATE_ANTI_SPAM     (NOSTR_POW_VALIDATE_FULL | NOSTR_POW_REJECT_LOWER_TARGET)

// Result information structure (optional output)
typedef struct {
    int actual_difficulty;        // Calculated difficulty (leading zero bits)
    int committed_target;         // Target difficulty from nonce tag (-1 if none)
    uint64_t nonce_value;        // Nonce value from tag (0 if none)
    int has_nonce_tag;           // 1 if nonce tag present, 0 otherwise
    char error_detail[256];      // Detailed error description
} nostr_pow_result_t;

// Function declarations
int nostr_add_proof_of_work(cJSON* event, const unsigned char* private_key,
                           int target_difficulty, int max_attempts,
                           int progress_report_interval, int timestamp_update_interval,
                           void (*progress_callback)(int current_difficulty, uint64_t nonce, void* user_data),
                           void* user_data);

// PoW validation functions
int nostr_validate_pow(cJSON* event, int min_difficulty, int validation_flags,
                      nostr_pow_result_t* result_info);

int nostr_calculate_pow_difficulty(const char* event_id_hex);

int nostr_extract_nonce_info(cJSON* event, uint64_t* nonce_out, int* target_difficulty_out);

int nostr_validate_nonce_tag(cJSON* nonce_tag_array);

#endif // NIP013_H
