-- get_category_results.lua
-- Get top candidates for a category from leaderboard
--
-- KEYS[1] = leaderboard:{category}
-- ARGV[1] = limit (top N results)

local leaderboard_key = KEYS[1]
local limit = tonumber(ARGV[1]) or 10

-- Get top candidates with scores (descending order)
local results = redis.call('ZREVRANGE', leaderboard_key, 0, limit - 1, 'WITHSCORES')

local formatted = {}
for i = 1, #results, 2 do
    table.insert(formatted, {
        candidate_id = results[i],
        vote_count = tonumber(results[i + 1])
    })
end

return cjson.encode(formatted)
