import type { NextApiRequest, NextApiResponse } from 'next' import { db, schema } from '../../../../lib/db/connection' import { eq, desc } from 'drizzle-orm' export default async function handler( req: NextApiRequest, res: NextApiResponse ) { if (req.method !== 'GET') { return res.status(405).json({ error: 'Method not allowed' }) } const { status = 'pending', page = '1', limit = '20' } = req.query try { const offset = (parseInt(page as string) - 1) * parseInt(limit as string) const limitInt = parseInt(limit as string) const reports = await db .select() .from(schema.reports) .where(eq(schema.reports.status, status as any)) .orderBy(desc(schema.reports.createdAt)) .limit(limitInt) .offset(offset) // Process the reports to parse JSON fields const processedReports = reports.map(report => ({ ...report, categorySuggestions: report.categorySuggestions ? JSON.parse(report.categorySuggestions) : [], evidenceUrls: report.evidenceUrls ? JSON.parse(report.evidenceUrls) : [] })) return res.status(200).json(processedReports) } catch (error) { console.error('Database error:', error) return res.status(500).json({ error: 'Internal server error' }) } }