import { sql } from '../../../../lib/sql.js'; /** * Public listing of community-shared custom games. No auth — read-only. */ export default async function handler(req, res) { try { if (req.method !== 'GET') { return res.status(405).json({ error: 'Method not allowed' }); } const result = await sql` SELECT g.id, g.name, g.description, g.updated_at, COUNT(c.id) AS card_count, COALESCE(NULLIF(u.username, ''), split_part(u.email, '@', 1)) AS author FROM custom_games g JOIN users u ON u.id = g.user_id LEFT JOIN custom_cards c ON c.custom_game_id = g.id WHERE g.is_public = true GROUP BY g.id, u.username, u.email ORDER BY card_count DESC, g.updated_at DESC LIMIT 100 `; return res.status(200).json({ games: result.rows }); } catch (error) { console.error('Public games API error:', error); return res.status(500).json({ error: 'Internal server error' }); } }