/**
 * GET /api/jobs/<id>/dispatched-pipelines?limit=20
 *
 * Recent pipelines this Job has dispatched, decorated with live
 * pipeline_runs status. Lets the Jobs UI show "what did this Job
 * fire and what state are they in" without a roundtrip per row.
 */

import { NextResponse } from 'next/server';
import { listJobDispatchedPipelines, getJob } from '@/lib/jobs/store';

export async function GET(req: Request, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const job = getJob(id);
  if (!job) return NextResponse.json({ error: 'job not found' }, { status: 404 });

  const url = new URL(req.url);
  const limitParam = url.searchParams.get('limit');
  const limit = limitParam && Number.isFinite(Number(limitParam))
    ? Math.min(Math.max(Number(limitParam), 1), 100)
    : 20;

  return NextResponse.json({ pipelines: listJobDispatchedPipelines(id, limit) });
}
