import type { NextRequest } from 'next/server'
import { getSession } from '@/lib/auth'
import { prisma } from '@/lib/prisma'
import { JOB_INCLUDE, buildPriceMap, mapJob } from '@/lib/jobs-mapper'
import type { PriceRow, RawJob } from '@/lib/jobs-mapper'

export async function GET(
  _request: NextRequest,
  { params }: { params: Promise<{ id: string }> }
) {
  const session = await getSession()
  if (!session.userId) {
    return Response.json({ error: 'Niet ingelogd' }, { status: 401 })
  }

  const { id } = await params

  const job = await prisma.claudeJob.findFirst({
    where: { id, user_id: session.userId },
    include: JOB_INCLUDE,
  })

  if (!job) {
    return Response.json({ error: 'Job niet gevonden' }, { status: 404 })
  }

  const prices = await prisma.modelPrice.findMany()
  const priceMap = buildPriceMap(prices as PriceRow[])
  return Response.json(mapJob(job as RawJob, priceMap))
}
