import { prisma } from '@/lib/db'
import { apiRoute } from '@/lib/api/handler'
import { ApiError, ERROR_CODES } from '@/lib/api/errors'

export const dynamic = 'force-dynamic'

export const GET = apiRoute<{ code: string }>({ permission: 'dna.read' }, async ({ params }) => {
  const code = decodeURIComponent(params.code)
  const dna = await prisma.failureDna.findFirst({
    where: { OR: [{ code }, { pattern: { code } }] },
    include: {
      pattern: { include: { region: { select: { code: true, name: true, nameAr: true } } } },
    },
  })
  if (!dna) throw new ApiError(ERROR_CODES.notFound)
  return dna
})
