#include "../src/firefly.h"
#include <iostream>
#include <string>
#include <vector>

void demonstrateStringOperations(void* client) {
    std::cout << "\n=== String Operations ===" << std::endl;
    
    // Set a string value
    if (StringSet(client, "greeting", "Hello, Firefly!")) {
        std::cout << "String set successfully" << std::endl;
    }
    
    // Get the string value
    if (char* value = StringGet(client, "greeting")) {
        std::cout << "Retrieved string: " << value << std::endl;
        FreeString(value);
    }
    
    // Clean up
    Delete(client, "greeting");
}

void demonstrateListOperations(void* client) {
    std::cout << "\n=== List Operations ===" << std::endl;
    const char* listKey = "todo_list";
    
    // Add items to list
    ListRightPush(client, listKey, "Buy groceries");
    ListRightPush(client, listKey, "Write code");
    ListRightPush(client, listKey, "Exercise");
    
    // Get all items
    std::cout << "Todo list items:" << std::endl;
    if (char* items = ListRange(client, listKey, 0, -1)) {
        std::cout << items;
        FreeString(items);
    }
    
    // Remove and get first item
    if (char* completed = ListLeftPop(client, listKey)) {
        std::cout << "Completed task: " << completed << std::endl;
        FreeString(completed);
    }
    
    // Clean up
    Delete(client, listKey);
}

void demonstrateHashOperations(void* client) {
    std::cout << "\n=== Hash Operations ===" << std::endl;
    const char* userKey = "user:123";
    
    // Store user profile
    HashSet(client, userKey, "name", "Alice");
    HashSet(client, userKey, "email", "alice@example.com");
    HashMultiSet(client, userKey, "city SanFrancisco country USA");
    
    // Get all user data
    std::cout << "User profile:" << std::endl;
    if (char* userData = HashGetAll(client, userKey)) {
        std::cout << userData;
        FreeString(userData);
    }
    
    // Get specific field
    if (char* name = HashGet(client, userKey, "name")) {
        std::cout << "Name: " << name << std::endl;
        FreeString(name);
    }
    
    // Clean up
    Delete(client, userKey);
}

void demonstratePipelineOperations(void* client) {
    std::cout << "\n=== Pipeline Operations ===" << std::endl;
    
    // Enable pipeline mode
    if (SetPipelineMode(client, true)) {
        std::cout << "Pipeline mode enabled" << std::endl;
        SetBatchSize(client, 3);
        
        // Queue multiple commands
        StringSet(client, "counter:1", "100");
        StringSet(client, "counter:2", "200");
        StringSet(client, "counter:3", "300");
        
        // Show queue status and execute
        std::cout << "Queued commands: " << GetQueuedCommandCount(client) << std::endl;
        if (char* results = FlushPipeline(client)) {
            std::cout << "Pipeline results:\n" << results << std::endl;
            FreeString(results);
        }
        
        SetPipelineMode(client, false);
        
        // Clean up
        for (int i = 1; i <= 3; i++) {
            char key[20];
            sprintf(key, "counter:%d", i);
            Delete(client, key);
        }
    }
}

int main() {
    // Create client
    void* client = CreateClient("localhost", 6379);
    if (!client) {
        std::cerr << "Failed to create client" << std::endl;
        return 1;
    }
    std::cout << "Connected to Firefly database" << std::endl;
    
    // Run demonstrations
    demonstrateStringOperations(client);
    demonstrateListOperations(client);
    demonstrateHashOperations(client);
    demonstratePipelineOperations(client);
    
    // Cleanup
    DestroyClient(client);
    std::cout << "\nExample completed successfully" << std::endl;
    
    return 0;
}