/**
 * GET    /api/jobs/<id>     { job, recent_runs: [..20..] }
 * PATCH  /api/jobs/<id>     partial update
 * DELETE /api/jobs/<id>     cascade
 */

import { NextResponse } from 'next/server';
import { getJob, updateJob, deleteJob, listRuns } 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 });
  return NextResponse.json({ job, recent_runs: listRuns(id, 20) });
}

export async function PATCH(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 });
  let body: any = {};
  try { body = await req.json(); } catch {}
  const ok = updateJob(id, body);
  return NextResponse.json({ ok, job: getJob(id) });
}

export async function DELETE(_req: Request, { params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const ok = deleteJob(id);
  return NextResponse.json({ ok });
}
