#include "queue_index.h"
#include "index/priority_queue.h"
#include <cstring>

// C API Implementation - delegates to PriorityQueueIndex

qi_index_t* qi_open(const char* queue_dir) {
    auto* idx = new PriorityQueueIndex(queue_dir);
    if (!idx->open()) {
        delete idx;
        return nullptr;
    }
    return reinterpret_cast<qi_index_t*>(idx);
}

void qi_close(qi_index_t* idx) {
    if (idx) {
        delete reinterpret_cast<PriorityQueueIndex*>(idx);
    }
}

int qi_add_tasks(qi_index_t* idx, const qi_task_t* tasks, uint32_t count) {
    if (!idx || !tasks || count == 0) return -1;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->add_tasks(tasks, count);
}

int qi_get_next_batch(qi_index_t* idx, qi_task_t* out_tasks, uint32_t max_count, uint32_t* out_count) {
    if (!idx || !out_tasks || max_count == 0) return -1;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->get_next_batch(out_tasks, max_count, out_count);
}

int qi_save(qi_index_t* idx) {
    if (!idx) return -1;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->save();
}

const char* qi_last_error(qi_index_t* idx) {
    if (!idx) return nullptr;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->last_error();
}

// Stub implementations for functions referenced by Go bindings
// These would delegate to PriorityQueueIndex methods when fully implemented

int qi_update_tasks(qi_index_t* idx, const qi_task_t* tasks, uint32_t count) {
    if (!idx || !tasks || count == 0) return -1;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->update_tasks(tasks, count);
}

int qi_remove_tasks(qi_index_t* idx, const char** task_ids, uint32_t count) {
    if (!idx || !task_ids || count == 0) return -1;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->remove_tasks(task_ids, count);
}

int qi_peek_next(qi_index_t* idx, qi_task_t* out_task) {
    if (!idx || !out_task) return -1;
    uint32_t count = 0;
    return qi_get_next_batch(idx, out_task, 1, &count);
}

int qi_get_task_by_id(qi_index_t* idx, const char* task_id, qi_task_t* out_task) {
    if (!idx || !task_id || !out_task) return -1;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->get_task_by_id(task_id, out_task);
}

int qi_get_all_tasks(qi_index_t* idx, qi_task_t** out_tasks, size_t* count) {
    if (!idx || !out_tasks || !count) return -1;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->get_all_tasks(out_tasks, count);
}

int qi_get_tasks_by_status(qi_index_t* idx, const char* status, qi_task_t** out_tasks, size_t* count) {
    (void)idx; (void)status; (void)out_tasks; (void)count;
    return -1; // Not yet implemented
}

// Task lifecycle operations
int qi_retry_task(qi_index_t* idx, const char* task_id, int64_t next_retry_at, uint32_t max_retries) {
    (void)idx; (void)task_id; (void)next_retry_at; (void)max_retries;
    return -1; // Not yet implemented
}

int qi_move_to_dlq(qi_index_t* idx, const char* task_id, const char* reason) {
    (void)idx; (void)task_id; (void)reason;
    return -1; // Not yet implemented
}

// Lease operations
int qi_renew_lease(qi_index_t* idx, const char* task_id, const char* worker_id, int64_t lease_expiry) {
    (void)idx; (void)task_id; (void)worker_id; (void)lease_expiry;
    return -1; // Not yet implemented
}

int qi_release_lease(qi_index_t* idx, const char* task_id, const char* worker_id) {
    (void)idx; (void)task_id; (void)worker_id;
    return -1; // Not yet implemented
}

// Index maintenance
int qi_rebuild_index(qi_index_t* idx) {
    if (!idx) return -1;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->rebuild();
}

int qi_compact_index(qi_index_t* idx) {
    if (!idx) return -1;
    return reinterpret_cast<PriorityQueueIndex*>(idx)->compact_index();
}

// Memory management
void qi_free_task_array(qi_task_t* tasks) {
    if (tasks) {
        delete[] tasks;
    }
}

void qi_free_string_array(char** strings, size_t count) {
    if (strings) {
        for (size_t i = 0; i < count; i++) {
            delete[] strings[i];
        }
        delete[] strings;
    }
}

void qi_clear_error(qi_index_t* idx) {
    if (idx) {
        reinterpret_cast<PriorityQueueIndex*>(idx)->clear_error();
    }
}

// Utility
uint64_t qi_get_index_version(qi_index_t* idx) {
    (void)idx;
    return 0; // Not yet implemented
}

int64_t qi_get_index_mtime(qi_index_t* idx) {
    (void)idx;
    return 0; // Not yet implemented
}

size_t qi_get_task_count(qi_index_t* idx, const char* status) {
    (void)idx; (void)status;
    return 0; // Not yet implemented
}
