/**
 * POST /api/pipelines/gc
 *
 * On-demand pipeline scratch-dir cleanup. Same logic as the background
 * sweep in lib/init.ts; this endpoint exists so the user (or CI) can
 * trigger it on demand via `forge pipeline gc`.
 *
 * Body (optional): { dry_run?: boolean }
 *
 * Response: { scanned, removed: [{path, reason}], kept: [{path, reason}] }
 */

import { NextResponse } from 'next/server';
import { gcPipelineTmp } from '@/lib/pipeline-gc';

export async function POST(req: Request) {
  let body: any = {};
  try { body = await req.json(); } catch {}
  const dryRun = body?.dry_run === true || body?.dryRun === true;

  try {
    const result = gcPipelineTmp({ dryRun });
    return NextResponse.json({ ok: true, dryRun, ...result });
  } catch (e) {
    return NextResponse.json({ ok: false, error: (e as Error).message }, { status: 500 });
  }
}
