/**
 * Synor Privacy SDK for C
 * Confidential transactions, ring signatures, stealth addresses, and cryptographic commitments.
 */

#ifndef SYNOR_PRIVACY_H
#define SYNOR_PRIVACY_H

#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>

#ifdef __cplusplus
extern "C" {
#endif

#define SYNOR_PRIVACY_VERSION "0.1.0"

/* ==================== Error Handling ==================== */

typedef enum {
    SYNOR_PRIVACY_OK = 0,
    SYNOR_PRIVACY_ERR_NULL_PARAM = -1,
    SYNOR_PRIVACY_ERR_HTTP = -2,
    SYNOR_PRIVACY_ERR_JSON = -3,
    SYNOR_PRIVACY_ERR_API = -4,
    SYNOR_PRIVACY_ERR_CLOSED = -5,
    SYNOR_PRIVACY_ERR_MEMORY = -6,
    SYNOR_PRIVACY_ERR_TIMEOUT = -7
} SynorPrivacyError;

typedef struct {
    int status_code;
    char *code;
    char *message;
} SynorPrivacyErrorInfo;

/* ==================== Configuration ==================== */

typedef struct {
    const char *api_key;
    const char *endpoint;
    int timeout_ms;
    int retries;
} SynorPrivacyConfig;

/* ==================== Client Handle ==================== */

typedef struct SynorPrivacyClient SynorPrivacyClient;

/* ==================== Confidential Transactions ==================== */

typedef struct {
    char *commitment;
    char *blinding_factor;
    char *value;
    char *key_image;
} SynorConfidentialTxInput;

typedef struct {
    char *commitment;
    char *blinding_factor;
    char *value;
    char *recipient_public_key;
    char *range_proof;
} SynorConfidentialTxOutput;

typedef struct {
    char *id;
    SynorConfidentialTxInput *inputs;
    size_t input_count;
    SynorConfidentialTxOutput *outputs;
    size_t output_count;
    char *fee;
    char *excess;
    char *excess_sig;
    char *kernel_offset;
} SynorConfidentialTransaction;

/* ==================== Commitments ==================== */

typedef struct {
    char *commitment;
    char *blinding_factor;
} SynorCommitment;

typedef struct {
    char *proof;
    char *commitment;
    int64_t min_value;
    int64_t max_value;
} SynorRangeProof;

/* ==================== Ring Signatures ==================== */

typedef struct {
    char *c0;
    char **s;
    size_t s_count;
    char *key_image;
    char **ring;
    size_t ring_count;
} SynorRingSignature;

/* ==================== Stealth Addresses ==================== */

typedef struct {
    char *spend_public_key;
    char *spend_private_key;
    char *view_public_key;
    char *view_private_key;
} SynorStealthKeyPair;

typedef struct {
    char *address;
    char *ephemeral_public_key;
    char *tx_public_key;
} SynorStealthAddress;

typedef struct {
    char *tx_hash;
    int output_index;
    char *stealth_address;
    char *amount;
    int64_t block_height;
} SynorStealthOutput;

/* ==================== Client Lifecycle ==================== */

/**
 * Create a new Privacy client.
 * @param config Configuration options
 * @param client Output pointer to client handle
 * @return Error code
 */
SynorPrivacyError synor_privacy_client_create(
    const SynorPrivacyConfig *config,
    SynorPrivacyClient **client
);

/**
 * Close and free the client.
 */
void synor_privacy_client_close(SynorPrivacyClient *client);

/**
 * Check if the client is closed.
 */
bool synor_privacy_client_is_closed(const SynorPrivacyClient *client);

/**
 * Check service health.
 */
bool synor_privacy_health_check(SynorPrivacyClient *client);

/**
 * Get the last error info.
 */
const SynorPrivacyErrorInfo* synor_privacy_get_last_error(const SynorPrivacyClient *client);

/* ==================== Confidential Transactions ==================== */

SynorPrivacyError synor_privacy_create_confidential_tx(
    SynorPrivacyClient *client,
    const SynorConfidentialTxInput *inputs,
    size_t input_count,
    const SynorConfidentialTxOutput *outputs,
    size_t output_count,
    SynorConfidentialTransaction *result
);

SynorPrivacyError synor_privacy_verify_confidential_tx(
    SynorPrivacyClient *client,
    const SynorConfidentialTransaction *tx,
    bool *valid
);

SynorPrivacyError synor_privacy_create_commitment(
    SynorPrivacyClient *client,
    const char *value,
    const char *blinding_factor,
    SynorCommitment *result
);

SynorPrivacyError synor_privacy_verify_commitment(
    SynorPrivacyClient *client,
    const char *commitment,
    const char *value,
    const char *blinding_factor,
    bool *valid
);

SynorPrivacyError synor_privacy_create_range_proof(
    SynorPrivacyClient *client,
    const char *value,
    const char *blinding_factor,
    int64_t min_value,
    int64_t max_value,
    SynorRangeProof *result
);

SynorPrivacyError synor_privacy_verify_range_proof(
    SynorPrivacyClient *client,
    const SynorRangeProof *proof,
    bool *valid
);

/* ==================== Ring Signatures ==================== */

SynorPrivacyError synor_privacy_create_ring_signature(
    SynorPrivacyClient *client,
    const char *message,
    const char **ring,
    size_t ring_count,
    int signer_index,
    const char *private_key,
    SynorRingSignature *result
);

SynorPrivacyError synor_privacy_verify_ring_signature(
    SynorPrivacyClient *client,
    const SynorRingSignature *signature,
    const char *message,
    bool *valid
);

SynorPrivacyError synor_privacy_generate_decoys(
    SynorPrivacyClient *client,
    int count,
    const char *exclude_key,
    char ***decoys,
    size_t *decoy_count
);

SynorPrivacyError synor_privacy_check_key_image(
    SynorPrivacyClient *client,
    const char *key_image,
    bool *spent
);

/* ==================== Stealth Addresses ==================== */

SynorPrivacyError synor_privacy_generate_stealth_keypair(
    SynorPrivacyClient *client,
    SynorStealthKeyPair *result
);

SynorPrivacyError synor_privacy_derive_stealth_address(
    SynorPrivacyClient *client,
    const char *spend_public_key,
    const char *view_public_key,
    SynorStealthAddress *result
);

SynorPrivacyError synor_privacy_recover_stealth_private_key(
    SynorPrivacyClient *client,
    const char *stealth_address,
    const char *view_private_key,
    const char *spend_private_key,
    char **private_key
);

SynorPrivacyError synor_privacy_scan_outputs(
    SynorPrivacyClient *client,
    const char *view_private_key,
    const char *spend_public_key,
    int64_t from_block,
    int64_t to_block,
    SynorStealthOutput **outputs,
    size_t *output_count
);

/* ==================== Blinding ==================== */

SynorPrivacyError synor_privacy_generate_blinding_factor(
    SynorPrivacyClient *client,
    char **blinding_factor
);

SynorPrivacyError synor_privacy_blind_value(
    SynorPrivacyClient *client,
    const char *value,
    const char *blinding_factor,
    char **blinded_value
);

SynorPrivacyError synor_privacy_unblind_value(
    SynorPrivacyClient *client,
    const char *blinded_value,
    const char *blinding_factor,
    char **value
);

/* ==================== Memory Management ==================== */

void synor_privacy_free_string(char *str);
void synor_privacy_free_string_array(char **arr, size_t count);
void synor_privacy_free_confidential_tx(SynorConfidentialTransaction *tx);
void synor_privacy_free_ring_signature(SynorRingSignature *sig);
void synor_privacy_free_stealth_keypair(SynorStealthKeyPair *keypair);
void synor_privacy_free_stealth_address(SynorStealthAddress *addr);
void synor_privacy_free_stealth_outputs(SynorStealthOutput *outputs, size_t count);
void synor_privacy_free_commitment(SynorCommitment *commitment);
void synor_privacy_free_range_proof(SynorRangeProof *proof);

#ifdef __cplusplus
}
#endif

#endif /* SYNOR_PRIVACY_H */
