/**
 * @file governance.h
 * @brief Synor Governance SDK for C
 *
 * Proposals, voting, DAOs, and vesting operations.
 */

#ifndef SYNOR_GOVERNANCE_H
#define SYNOR_GOVERNANCE_H

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

#ifdef __cplusplus
extern "C" {
#endif

/* ==================== Error Types ==================== */

typedef enum {
    SYNOR_GOVERNANCE_OK = 0,
    SYNOR_GOVERNANCE_ERROR_CLIENT_CLOSED,
    SYNOR_GOVERNANCE_ERROR_NETWORK,
    SYNOR_GOVERNANCE_ERROR_HTTP,
    SYNOR_GOVERNANCE_ERROR_ENCODING,
    SYNOR_GOVERNANCE_ERROR_DECODING,
    SYNOR_GOVERNANCE_ERROR_INVALID_RESPONSE,
    SYNOR_GOVERNANCE_ERROR_MEMORY,
    SYNOR_GOVERNANCE_ERROR_TIMEOUT,
    SYNOR_GOVERNANCE_ERROR_UNKNOWN
} synor_governance_error_t;

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

typedef struct {
    const char* api_key;
    const char* endpoint;       /* Default: https://governance.synor.io/v1 */
    uint32_t timeout_ms;        /* Default: 30000 */
    uint32_t retries;           /* Default: 3 */
    bool debug;
} synor_governance_config_t;

/* ==================== Enums ==================== */

typedef enum {
    SYNOR_PROPOSAL_PENDING,
    SYNOR_PROPOSAL_ACTIVE,
    SYNOR_PROPOSAL_PASSED,
    SYNOR_PROPOSAL_REJECTED,
    SYNOR_PROPOSAL_EXECUTED,
    SYNOR_PROPOSAL_CANCELLED
} synor_proposal_status_t;

typedef enum {
    SYNOR_VOTE_FOR,
    SYNOR_VOTE_AGAINST,
    SYNOR_VOTE_ABSTAIN
} synor_vote_choice_t;

typedef enum {
    SYNOR_DAO_TOKEN,
    SYNOR_DAO_MULTISIG,
    SYNOR_DAO_HYBRID
} synor_dao_type_t;

typedef enum {
    SYNOR_VESTING_ACTIVE,
    SYNOR_VESTING_COMPLETED,
    SYNOR_VESTING_REVOKED
} synor_vesting_status_t;

/* ==================== Proposal Types ==================== */

typedef struct {
    char* target;
    char* method;
    char* data;
    char* value;
} synor_proposal_action_t;

typedef struct {
    char* title;
    char* description;
    char* discussion_url;
    int64_t voting_start_time;
    int64_t voting_end_time;
    char* dao_id;
    synor_proposal_action_t* actions;
    size_t actions_count;
} synor_proposal_draft_t;

typedef struct {
    char* for_votes;
    char* against_votes;
    char* abstain_votes;
    char* quorum;
    char* quorum_required;
    int32_t total_voters;
} synor_vote_tally_t;

typedef struct {
    char* id;
    char* title;
    char* description;
    char* discussion_url;
    char* proposer;
    synor_proposal_status_t status;
    int64_t created_at;
    int64_t voting_start_time;
    int64_t voting_end_time;
    int64_t execution_time;
    synor_vote_tally_t vote_tally;
    synor_proposal_action_t* actions;
    size_t actions_count;
    char* dao_id;
} synor_proposal_t;

typedef struct {
    synor_proposal_t* proposals;
    size_t count;
} synor_proposal_list_t;

typedef struct {
    synor_proposal_status_t* status;  /* NULL for all */
    char* proposer;                    /* NULL for all */
    char* dao_id;                      /* NULL for all */
    int32_t limit;
    int32_t offset;
} synor_proposal_filter_t;

/* ==================== Voting Types ==================== */

typedef struct {
    synor_vote_choice_t choice;
    char* reason;
} synor_vote_t;

typedef struct {
    char* id;
    char* proposal_id;
    char* voter;
    synor_vote_choice_t choice;
    char* weight;
    char* reason;
    int64_t voted_at;
    char* tx_hash;
} synor_vote_receipt_t;

typedef struct {
    synor_vote_receipt_t* votes;
    size_t count;
} synor_vote_receipt_list_t;

typedef struct {
    char* address;
    char* delegated_power;
    char* own_power;
    char* total_power;
    char** delegators;
    size_t delegators_count;
} synor_voting_power_t;

typedef struct {
    char* id;
    char* from;
    char* to;
    char* amount;
    int64_t delegated_at;
    char* tx_hash;
} synor_delegation_receipt_t;

typedef struct {
    synor_delegation_receipt_t* delegations;
    size_t count;
} synor_delegation_receipt_list_t;

/* ==================== DAO Types ==================== */

typedef struct {
    char* name;
    char* description;
    synor_dao_type_t type;
    int32_t voting_period_days;
    int32_t timelock_days;
    char* token_address;
    double quorum_percent;
    char* proposal_threshold;
    char** multisig_members;
    size_t multisig_members_count;
    int32_t multisig_threshold;
} synor_dao_config_t;

typedef struct {
    char* id;
    char* name;
    char* description;
    synor_dao_type_t type;
    char* token_address;
    int32_t voting_period_days;
    int32_t timelock_days;
    double quorum_percent;
    char* proposal_threshold;
    int32_t total_proposals;
    int32_t active_proposals;
    char* treasury_value;
    int32_t member_count;
    int64_t created_at;
} synor_dao_t;

typedef struct {
    synor_dao_t* daos;
    size_t count;
} synor_dao_list_t;

typedef struct {
    char* address;
    char* balance;
    char* name;
    char* symbol;
} synor_treasury_token_t;

typedef struct {
    char* dao_id;
    char* total_value;
    synor_treasury_token_t* tokens;
    size_t tokens_count;
    int64_t last_updated;
} synor_dao_treasury_t;

/* ==================== Vesting Types ==================== */

typedef struct {
    char* beneficiary;
    char* total_amount;
    int64_t start_time;
    int64_t cliff_duration;
    int64_t vesting_duration;
    bool revocable;
} synor_vesting_schedule_t;

typedef struct {
    char* id;
    char* beneficiary;
    char* grantor;
    char* total_amount;
    char* released_amount;
    char* releasable_amount;
    int64_t start_time;
    int64_t cliff_time;
    int64_t end_time;
    synor_vesting_status_t status;
    bool revocable;
    int64_t created_at;
} synor_vesting_contract_t;

typedef struct {
    synor_vesting_contract_t* contracts;
    size_t count;
} synor_vesting_contract_list_t;

typedef struct {
    char* id;
    char* contract_id;
    char* amount;
    char* tx_hash;
    int64_t claimed_at;
} synor_claim_receipt_t;

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

typedef struct synor_governance synor_governance_t;

/* ==================== Lifecycle Functions ==================== */

synor_governance_t* synor_governance_create(const synor_governance_config_t* config);
void synor_governance_destroy(synor_governance_t* governance);
bool synor_governance_is_closed(synor_governance_t* governance);
bool synor_governance_health_check(synor_governance_t* governance);

/* ==================== Proposal Operations ==================== */

synor_governance_error_t synor_governance_create_proposal(synor_governance_t* governance,
    const synor_proposal_draft_t* draft, synor_proposal_t* result);
synor_governance_error_t synor_governance_get_proposal(synor_governance_t* governance,
    const char* proposal_id, synor_proposal_t* result);
synor_governance_error_t synor_governance_list_proposals(synor_governance_t* governance,
    const synor_proposal_filter_t* filter, synor_proposal_list_t* result);
synor_governance_error_t synor_governance_cancel_proposal(synor_governance_t* governance,
    const char* proposal_id, synor_proposal_t* result);
synor_governance_error_t synor_governance_execute_proposal(synor_governance_t* governance,
    const char* proposal_id, synor_proposal_t* result);
synor_governance_error_t synor_governance_wait_for_proposal(synor_governance_t* governance,
    const char* proposal_id, uint32_t poll_interval_ms, uint32_t max_wait_ms,
    synor_proposal_t* result);

void synor_proposal_free(synor_proposal_t* proposal);
void synor_proposal_list_free(synor_proposal_list_t* list);

/* ==================== Voting Operations ==================== */

synor_governance_error_t synor_governance_vote(synor_governance_t* governance,
    const char* proposal_id, const synor_vote_t* vote, const char* weight,
    synor_vote_receipt_t* result);
synor_governance_error_t synor_governance_get_votes(synor_governance_t* governance,
    const char* proposal_id, synor_vote_receipt_list_t* result);
synor_governance_error_t synor_governance_get_my_vote(synor_governance_t* governance,
    const char* proposal_id, synor_vote_receipt_t* result);
synor_governance_error_t synor_governance_delegate(synor_governance_t* governance,
    const char* delegatee, const char* amount, synor_delegation_receipt_t* result);
synor_governance_error_t synor_governance_undelegate(synor_governance_t* governance,
    const char* delegatee, synor_delegation_receipt_t* result);
synor_governance_error_t synor_governance_get_voting_power(synor_governance_t* governance,
    const char* address, synor_voting_power_t* result);
synor_governance_error_t synor_governance_get_delegations(synor_governance_t* governance,
    const char* address, synor_delegation_receipt_list_t* result);

void synor_vote_receipt_free(synor_vote_receipt_t* receipt);
void synor_vote_receipt_list_free(synor_vote_receipt_list_t* list);
void synor_voting_power_free(synor_voting_power_t* power);
void synor_delegation_receipt_free(synor_delegation_receipt_t* receipt);
void synor_delegation_receipt_list_free(synor_delegation_receipt_list_t* list);

/* ==================== DAO Operations ==================== */

synor_governance_error_t synor_governance_create_dao(synor_governance_t* governance,
    const synor_dao_config_t* config, synor_dao_t* result);
synor_governance_error_t synor_governance_get_dao(synor_governance_t* governance,
    const char* dao_id, synor_dao_t* result);
synor_governance_error_t synor_governance_list_daos(synor_governance_t* governance,
    int32_t limit, int32_t offset, synor_dao_list_t* result);
synor_governance_error_t synor_governance_get_dao_treasury(synor_governance_t* governance,
    const char* dao_id, synor_dao_treasury_t* result);
synor_governance_error_t synor_governance_get_dao_members(synor_governance_t* governance,
    const char* dao_id, char*** members, size_t* count);

void synor_dao_free(synor_dao_t* dao);
void synor_dao_list_free(synor_dao_list_t* list);
void synor_dao_treasury_free(synor_dao_treasury_t* treasury);

/* ==================== Vesting Operations ==================== */

synor_governance_error_t synor_governance_create_vesting(synor_governance_t* governance,
    const synor_vesting_schedule_t* schedule, synor_vesting_contract_t* result);
synor_governance_error_t synor_governance_get_vesting(synor_governance_t* governance,
    const char* contract_id, synor_vesting_contract_t* result);
synor_governance_error_t synor_governance_list_vesting(synor_governance_t* governance,
    const char* beneficiary, synor_vesting_contract_list_t* result);
synor_governance_error_t synor_governance_claim_vested(synor_governance_t* governance,
    const char* contract_id, synor_claim_receipt_t* result);
synor_governance_error_t synor_governance_revoke_vesting(synor_governance_t* governance,
    const char* contract_id, synor_vesting_contract_t* result);
synor_governance_error_t synor_governance_get_releasable(synor_governance_t* governance,
    const char* contract_id, char** amount);

void synor_vesting_contract_free(synor_vesting_contract_t* contract);
void synor_vesting_contract_list_free(synor_vesting_contract_list_t* list);
void synor_claim_receipt_free(synor_claim_receipt_t* receipt);

#ifdef __cplusplus
}
#endif

#endif /* SYNOR_GOVERNANCE_H */
