/**
 * GET /api/schedules/:id/runs?limit=20
 *
 * Recent schedule_runs for this schedule, newest first.
 */

import { NextResponse } from 'next/server';
import { getSchedule, listScheduleRuns } from '@/lib/schedules/store';

export async function GET(req: Request, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const s = getSchedule(id);
  if (!s) return NextResponse.json({ error: 'schedule 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), 200)
    : 20;

  return NextResponse.json({ runs: listScheduleRuns(id, limit) });
}
