import { prisma } from '@/lib/db'
import { apiRoute, paginated, pagination } from '@/lib/api/handler'

export const dynamic = 'force-dynamic'

export const GET = apiRoute({ permission: 'recommendations.read' }, async ({ searchParams }) => {
  const { page, pageSize, skip, take } = pagination(searchParams, 50)
  const status = searchParams.get('status')
  const where = status ? { status } : {}

  const [items, total] = await Promise.all([
    prisma.recommendation.findMany({
      where,
      skip,
      take,
      orderBy: [{ riskReduction: 'desc' }],
      include: {
        asset: { select: { code: true } },
        prediction: { select: { code: true, riskScore: true } },
      },
    }),
    prisma.recommendation.count({ where }),
  ])

  return paginated(items, total, page, pageSize)
})
