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<{ id: string }>(
  { permission: 'simulations.read' },
  async ({ params }) => {
    const simulation = await prisma.simulation.findFirst({
      where: { OR: [{ id: params.id }, { code: params.id }] },
      include: {
        result: true,
        impacts: {
          include: {
            region: { select: { code: true } },
            asset: { select: { code: true } },
          },
        },
        scenario: true,
        createdBy: { select: { name: true, nameAr: true } },
      },
    })
    if (!simulation) throw new ApiError(ERROR_CODES.notFound)
    return simulation
  },
)
