import { NextRequest } from 'next/server' import { prisma } from '@/src/lib/prisma' export async function GET(req: NextRequest) { try { const { searchParams } = new URL(req.url) const page = Math.max(1, parseInt(searchParams.get('page') || '1', 10) || 1) const pageSizeRaw = parseInt(searchParams.get('pageSize') || '20', 10) || 20 const pageSize = Math.min(100, Math.max(1, pageSizeRaw)) const startDate = searchParams.get('startDate') const endDate = searchParams.get('endDate') const hasFit = ((): boolean | undefined => { const v = searchParams.get('hasFit') return v === null ? undefined : v === 'true' })() const sortBy = searchParams.get('sortBy') || 'timestamp' // timestamp | duration | maxValue const sortOrder = (searchParams.get('sortOrder') === 'asc' ? 'asc' : 'desc') as 'asc' | 'desc' const where: any = {} if (startDate || endDate) { where.timestamp = {} if (startDate) where.timestamp.gte = new Date(startDate) if (endDate) where.timestamp.lte = new Date(endDate) } if (hasFit === true) where.NOT = { fit_a: null } if (hasFit === false) where.fit_a = null let orderBy: any if (sortBy === 'duration') orderBy = { duration: sortOrder } else if (sortBy === 'maxValue') { if (hasFit === true) orderBy = { force_max: sortOrder } else orderBy = { code_max: sortOrder } } else { orderBy = { timestamp: sortOrder } } const [total, rows] = await Promise.all([ prisma.recording.count({ where }), prisma.recording.findMany({ where, orderBy, skip: (page - 1) * pageSize, take: pageSize, select: { id: true, timestamp: true, sample_count: true, duration: true, fit_a: true, fit_b: true, code_min: true, code_max: true, code_avg: true, force_min: true, force_max: true, force_avg: true, note: true, }, }), ]) const data = rows.map((r) => ({ id: r.id, timestamp: r.timestamp.toISOString(), sampleCount: r.sample_count, duration: r.duration, hasFit: r.fit_a !== null && r.fit_a !== undefined, fit: r.fit_a !== null && r.fit_a !== undefined ? { a: r.fit_a!, b: r.fit_b! } : undefined, note: r.note ?? undefined, stats: { codeMin: r.code_min, codeMax: r.code_max, codeAvg: r.code_avg, forceMin: r.force_min ?? undefined, forceMax: r.force_max ?? undefined, forceAvg: r.force_avg ?? undefined, }, })) return Response.json({ data, pagination: { total, page, pageSize, totalPages: Math.max(1, Math.ceil(total / pageSize)), }, }) } catch (err) { console.error(err) return Response.json({ error: 'internal error' }, { status: 500 }) } }