import { sql } from '@vercel/postgres'; import { verifyToken } from '../auth-utils.js'; // GET /api/collections - Get user collections export default async function handler(req, res) { // Set CORS headers res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization'); // Handle preflight requests if (req.method === 'OPTIONS') { res.status(200).end(); return; } if (req.method !== 'GET' && req.method !== 'POST' && req.method !== 'PUT' && req.method !== 'DELETE') { return res.status(405).json({ error: 'Method not allowed' }); } try { // Temporarily bypass auth for testing // const token = req.headers.authorization?.replace('Bearer ', ''); // const user = await verifyToken(token); // if (!user) { // return res.status(401).json({ error: 'Unauthorized' }); // } const userId = 1; // Temporarily hardcoded for testing if (req.method === 'GET') { const { id } = req.query; if (id) { // Get specific collection const result = await sql.query(` SELECT c.id, c.name, c.description, c.is_public, c.created_at, c.updated_at, COUNT(cc.user_card_id) as card_count FROM collections c LEFT JOIN collection_cards cc ON c.id = cc.collection_id WHERE c.id = $1 AND c.user_id = $2 GROUP BY c.id `, [id, userId]); if (result.rows.length === 0) { return res.status(404).json({ error: 'Collection not found' }); } return res.status(200).json({ success: true, collection: result.rows[0] }); } else { // Get all collections const result = await sql.query(` SELECT c.id, c.name, c.description, c.is_public, c.created_at, c.updated_at, COUNT(cc.user_card_id) as card_count FROM collections c LEFT JOIN collection_cards cc ON c.id = cc.collection_id WHERE c.user_id = $1 GROUP BY c.id ORDER BY c.created_at DESC `, [userId]); return res.status(200).json({ success: true, collections: result.rows }); } } if (req.method === 'POST') { const { name, description = '', isPublic = false } = req.body; if (!name) { return res.status(400).json({ error: 'Collection name is required' }); } const result = await sql.query(` INSERT INTO collections (user_id, name, description, is_public) VALUES ($1, $2, $3, $4) RETURNING * `, [userId, name, description, isPublic]); return res.status(201).json({ success: true, message: 'Collection created', collection: result.rows[0] }); } if (req.method === 'PUT') { const { id, name, description, isPublic } = req.body; if (!id) { return res.status(400).json({ error: 'Collection ID is required' }); } const result = await sql.query(` UPDATE collections SET name = COALESCE($1, name), description = COALESCE($2, description), is_public = COALESCE($3, is_public), updated_at = NOW() WHERE id = $4 AND user_id = $5 RETURNING * `, [name, description, isPublic, id, userId]); if (result.rows.length === 0) { return res.status(404).json({ error: 'Collection not found' }); } return res.status(200).json({ success: true, message: 'Collection updated', collection: result.rows[0] }); } if (req.method === 'DELETE') { const { id } = req.query; if (!id) { return res.status(400).json({ error: 'Collection ID is required' }); } // Delete collection cards first await sql.query(` DELETE FROM collection_cards WHERE collection_id = $1 `, [id]); // Delete the collection const result = await sql.query(` DELETE FROM collections WHERE id = $1 AND user_id = $2 RETURNING * `, [id, userId]); if (result.rows.length === 0) { return res.status(404).json({ error: 'Collection not found' }); } return res.status(200).json({ success: true, message: 'Collection deleted' }); } } catch (error) { console.error('❌ Error in collections API:', error); return res.status(500).json({ error: 'Failed to process collections request', details: error.message }); } }