import type { NextApiRequest, NextApiResponse } from 'next' import { prisma } from '@/lib/prisma' import { requireAuth } from '@/lib/auth' // Auto-suggests release items tied to a product: recent first-build form // submission counts for that product, plus recently resolved NCR fixes. export default async function handler(req: NextApiRequest, res: NextApiResponse) { const user = await requireAuth(req, res) if (!user) return if (req.method !== 'GET') return res.status(405).end() const { product } = req.query as { product?: string } const items: { label: string; type: string; included: boolean }[] = [] if (product) { const forms = await prisma.buildForm.findMany({ where: { product: { contains: product} }, include: { _count: { select: { submissions: true } } }, }) for (const f of forms) { if (f._count.submissions > 0) { items.push({ label: `${f.name} — ${f._count.submissions} submissions`, type: 'FORM_DATA', included: true }) } } } // Recently resolved NCRs (last 90 days) as candidate fixes to include const since = new Date() since.setDate(since.getDate() - 90) const recentNcrs = await prisma.nCR.findMany({ where: { status: 'RESOLVED', resolvedAt: { gte: since } }, orderBy: { resolvedAt: 'desc' }, take: 5, }) for (const n of recentNcrs) { items.push({ label: `${n.ref} fix — ${n.category || 'resolved'}`, type: 'NCR_FIX', included: false }) } return res.json({ data: items }) }