import { authenticateApiRequest } from '@/lib/api-auth'
import { prisma } from '@/lib/prisma'
import { productAccessFilter } from '@/lib/product-access'

export async function GET(request: Request) {
  const auth = await authenticateApiRequest(request)
  if ('error' in auth) {
    return Response.json({ error: auth.error }, { status: auth.status })
  }

  const products = await prisma.product.findMany({
    where: { archived: false, ...productAccessFilter(auth.userId) },
    orderBy: { created_at: 'desc' },
    select: { id: true, code: true, name: true, description: true, repo_url: true, definition_of_done: true },
  })

  return Response.json(products)
}
