import { NextRequest, NextResponse } from "next/server";
import { runDueTasks, runTask } from "@/lib/task-runner";

export async function POST(req: NextRequest) {
  const body = await req.json().catch(() => ({}));
  if (body.taskId) {
    const result = await runTask(String(body.taskId));
    return NextResponse.json(result, { status: result.ok ? 200 : 400 });
  }
  const results = await runDueTasks(Number(body.limit || 5));
  return NextResponse.json({ ok: true, results });
}
