/*
 * NOSTR Core Library - Common Utilities
 * 
 * Common functions and utilities shared across the library
 */

#include "nostr_common.h"
#include "utils.h"

/**
 * Convert error code to human-readable string
 * Handles all error codes defined in nostr_common.h
 */
const char* nostr_strerror(int error_code) {
    switch (error_code) {
        case NOSTR_SUCCESS: return "Success";
        case NOSTR_ERROR_INVALID_INPUT: return "Invalid input";
        case NOSTR_ERROR_CRYPTO_FAILED: return "Cryptographic operation failed";
        case NOSTR_ERROR_MEMORY_FAILED: return "Memory allocation failed";
        case NOSTR_ERROR_IO_FAILED: return "I/O operation failed";
        case NOSTR_ERROR_NETWORK_FAILED: return "Network operation failed";
        case NOSTR_ERROR_NIP04_INVALID_FORMAT: return "NIP-04 invalid format";
        case NOSTR_ERROR_NIP04_DECRYPT_FAILED: return "NIP-04 decryption failed";
        case NOSTR_ERROR_NIP04_BUFFER_TOO_SMALL: return "NIP-04 buffer too small";
        case NOSTR_ERROR_NIP44_INVALID_FORMAT: return "NIP-44: Invalid format";
        case NOSTR_ERROR_NIP44_DECRYPT_FAILED: return "NIP-44: Decryption failed";
        case NOSTR_ERROR_NIP44_BUFFER_TOO_SMALL: return "NIP-44: Buffer too small";
        case NOSTR_ERROR_NIP05_INVALID_IDENTIFIER: return "NIP-05: Invalid identifier format";
        case NOSTR_ERROR_NIP05_HTTP_FAILED: return "NIP-05: HTTP request failed";
        case NOSTR_ERROR_NIP05_JSON_PARSE_FAILED: return "NIP-05: JSON parsing failed";
        case NOSTR_ERROR_NIP05_NAME_NOT_FOUND: return "NIP-05: Name not found in .well-known";
        case NOSTR_ERROR_NIP05_PUBKEY_MISMATCH: return "NIP-05: Public key mismatch";
        case NOSTR_ERROR_EVENT_INVALID_STRUCTURE: return "Event has invalid structure";
        case NOSTR_ERROR_EVENT_INVALID_ID: return "Event has invalid ID";
        case NOSTR_ERROR_EVENT_INVALID_PUBKEY: return "Event has invalid public key";
        case NOSTR_ERROR_EVENT_INVALID_SIGNATURE: return "Event has invalid signature";
        case NOSTR_ERROR_EVENT_INVALID_CREATED_AT: return "Event has invalid timestamp";
        case NOSTR_ERROR_EVENT_INVALID_KIND: return "Event has invalid kind";
        case NOSTR_ERROR_EVENT_INVALID_TAGS: return "Event has invalid tags";
        case NOSTR_ERROR_EVENT_INVALID_CONTENT: return "Event has invalid content";
        case NOSTR_ERROR_NIP13_INSUFFICIENT: return "NIP-13: Insufficient PoW difficulty";
        case NOSTR_ERROR_NIP13_NO_NONCE_TAG: return "NIP-13: Missing nonce tag";
        case NOSTR_ERROR_NIP13_INVALID_NONCE_TAG: return "NIP-13: Invalid nonce tag format";
        case NOSTR_ERROR_NIP13_TARGET_MISMATCH: return "NIP-13: Target difficulty mismatch";
        case NOSTR_ERROR_NIP13_CALCULATION: return "NIP-13: PoW calculation error";
        case NOSTR_ERROR_NIP42_INVALID_CHALLENGE: return "NIP-42: Invalid challenge";
        case NOSTR_ERROR_NIP42_CHALLENGE_EXPIRED: return "NIP-42: Challenge expired";
        case NOSTR_ERROR_NIP42_AUTH_EVENT_INVALID: return "NIP-42: Authentication event invalid";
        case NOSTR_ERROR_NIP42_URL_MISMATCH: return "NIP-42: Relay URL mismatch";
        case NOSTR_ERROR_NIP42_TIME_TOLERANCE: return "NIP-42: Timestamp outside tolerance";
        case NOSTR_ERROR_NIP42_NOT_AUTHENTICATED: return "NIP-42: Client not authenticated";
        case NOSTR_ERROR_NIP42_INVALID_MESSAGE_FORMAT: return "NIP-42: Invalid message format";
        case NOSTR_ERROR_NIP42_CHALLENGE_TOO_SHORT: return "NIP-42: Challenge too short";
        case NOSTR_ERROR_NIP42_CHALLENGE_TOO_LONG: return "NIP-42: Challenge too long";
        case NOSTR_ERROR_NIP46_INVALID_BUNKER_URL: return "NIP-46: Invalid bunker URL";
        case NOSTR_ERROR_NIP46_INVALID_NOSTRCONNECT: return "NIP-46: Invalid nostrconnect URL";
        case NOSTR_ERROR_NIP46_INVALID_REQUEST: return "NIP-46: Invalid request payload";
        case NOSTR_ERROR_NIP46_INVALID_RESPONSE: return "NIP-46: Invalid response payload";
        case NOSTR_ERROR_NIP46_ENCRYPTION_FAILED: return "NIP-46: Encryption failed";
        case NOSTR_ERROR_NIP46_DECRYPTION_FAILED: return "NIP-46: Decryption failed";
        case NOSTR_ERROR_NIP46_CONNECTION_FAILED: return "NIP-46: Connection failed";
        case NOSTR_ERROR_NIP46_TIMEOUT: return "NIP-46: Timeout";
        case NOSTR_ERROR_NIP46_SECRET_MISMATCH: return "NIP-46: Secret mismatch";
        case NOSTR_ERROR_NIP46_UNKNOWN_METHOD: return "NIP-46: Unknown method";
        case NOSTR_ERROR_NIP46_AUTH_CHALLENGE: return "NIP-46: Auth challenge required";
        case NOSTR_ERROR_NIP46_NOT_CONNECTED: return "NIP-46: Not connected";
        case NOSTR_ERROR_NIP60_INVALID_WALLET: return "NIP-60: Invalid wallet event";
        case NOSTR_ERROR_NIP60_INVALID_TOKEN: return "NIP-60: Invalid token event";
        case NOSTR_ERROR_NIP60_INVALID_HISTORY: return "NIP-60: Invalid history event";
        case NOSTR_ERROR_NIP60_INVALID_QUOTE: return "NIP-60: Invalid quote event";
        case NOSTR_ERROR_NIP60_DECRYPT_FAILED: return "NIP-60: Decryption failed";
        case NOSTR_ERROR_NIP60_INVALID_PROOFS: return "NIP-60: Invalid proofs payload";
        case NOSTR_ERROR_NIP60_INSUFFICIENT_FUNDS: return "NIP-60: Insufficient funds";
        case NOSTR_ERROR_NIP61_INVALID_INFO: return "NIP-61: Invalid info event";
        case NOSTR_ERROR_NIP61_INVALID_NUTZAP: return "NIP-61: Invalid nutzap event";
        case NOSTR_ERROR_NIP61_MINT_MISMATCH: return "NIP-61: Mint mismatch";
        case NOSTR_ERROR_NIP61_PUBKEY_MISMATCH: return "NIP-61: Pubkey mismatch";
        case NOSTR_ERROR_NIP61_VERIFICATION_FAILED: return "NIP-61: Verification failed";
        case NOSTR_ERROR_CASHU_HTTP_FAILED: return "Cashu: HTTP request failed";
        case NOSTR_ERROR_CASHU_JSON_PARSE_FAILED: return "Cashu: JSON parsing failed";
        case NOSTR_ERROR_CASHU_MINT_ERROR: return "Cashu: Mint returned an error";
        case NOSTR_ERROR_CASHU_QUOTE_NOT_PAID: return "Cashu: Quote not paid";
        case NOSTR_ERROR_CASHU_QUOTE_EXPIRED: return "Cashu: Quote expired";
        case NOSTR_ERROR_CASHU_PROOFS_SPENT: return "Cashu: One or more proofs are already spent";
        case NOSTR_ERROR_CASHU_CRYPTO_FAILED: return "Cashu: Cryptographic operation failed";
        case NOSTR_ERROR_CASHU_INVALID_KEYSET: return "Cashu: Invalid keyset";
        default: return "Unknown error";
    }
}

/**
 * Initialize the NOSTR library
 */
int nostr_init(void) {
    if (nostr_crypto_init() != 0) {
        return NOSTR_ERROR_CRYPTO_FAILED;
    }
    return NOSTR_SUCCESS;
}

/**
 * Cleanup the NOSTR library
 */
void nostr_cleanup(void) {
    nostr_crypto_cleanup();
}
