#ifndef QUEUE_INDEX_H
#define QUEUE_INDEX_H

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

#ifdef __cplusplus
extern "C" {
#endif

// Opaque handle for queue index
typedef struct qi_index qi_index_t;

// Task structure - matches Go queue.Task fields
// Fixed-size for binary format (no dynamic allocation in hot path)
typedef struct qi_task {
    char id[64];           // Task ID
    char job_name[128];    // Job name
    int64_t priority;      // Higher = more important
    int64_t created_at;    // Unix timestamp (nanoseconds)
    int64_t next_retry;    // Unix timestamp (nanoseconds), 0 if none
    char status[16];       // "queued", "running", "finished", "failed"
    uint32_t retries;      // Current retry count
} qi_task_t;

// Index operations
qi_index_t* qi_open(const char* queue_dir);
void qi_close(qi_index_t* idx);

// Batch operations (amortize CGo overhead)
int qi_add_tasks(qi_index_t* idx, const qi_task_t* tasks, uint32_t count);
int qi_update_tasks(qi_index_t* idx, const qi_task_t* tasks, uint32_t count);
int qi_remove_tasks(qi_index_t* idx, const char** task_ids, uint32_t count);

// Priority queue operations
int qi_get_next_batch(qi_index_t* idx, qi_task_t* out_tasks, uint32_t max_count, uint32_t* out_count);
int qi_peek_next(qi_index_t* idx, qi_task_t* out_task);

// Query operations
int qi_get_task_by_id(qi_index_t* idx, const char* task_id, qi_task_t* out_task);
int qi_get_all_tasks(qi_index_t* idx, qi_task_t** out_tasks, size_t* count);
int qi_get_tasks_by_status(qi_index_t* idx, const char* status, qi_task_t** out_tasks, size_t* count);

// Task lifecycle operations
int qi_retry_task(qi_index_t* idx, const char* task_id, int64_t next_retry_at, uint32_t max_retries);
int qi_move_to_dlq(qi_index_t* idx, const char* task_id, const char* reason);

// Lease operations
int qi_renew_lease(qi_index_t* idx, const char* task_id, const char* worker_id, int64_t lease_expiry);
int qi_release_lease(qi_index_t* idx, const char* task_id, const char* worker_id);

// Index maintenance
int qi_rebuild_index(qi_index_t* idx);
int qi_compact_index(qi_index_t* idx);

// Memory management
void qi_free_task_array(qi_task_t* tasks);
void qi_free_string_array(char** strings, size_t count);

// Error handling
const char* qi_last_error(qi_index_t* idx);
void qi_clear_error(qi_index_t* idx);

// Utility
uint64_t qi_get_index_version(qi_index_t* idx);
int64_t qi_get_index_mtime(qi_index_t* idx);
size_t qi_get_task_count(qi_index_t* idx, const char* status);

#ifdef __cplusplus
}
#endif

#endif // QUEUE_INDEX_H
