import type { NextRequest } from 'next/server'
import { getSession } from '@/lib/auth'
import { prisma } from '@/lib/prisma'

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 userId = session.userId
  const { id } = await params

  const job = await prisma.claudeJob.findFirst({
    where: { id, user_id: userId },
    select: { kind: true },
  })

  if (!job || job.kind !== 'SPRINT_IMPLEMENTATION') {
    return Response.json([], { status: 200 })
  }

  const executions = await prisma.sprintTaskExecution.findMany({
    where: { sprint_job_id: id },
    include: { task: { select: { code: true, title: true } } },
    orderBy: { order: 'asc' },
  })

  return Response.json(
    executions.map(e => ({
      id: e.id,
      taskCode: e.task.code,
      taskTitle: e.task.title,
      status: e.status,
    }))
  )
}
