import { fail, guard, ok, readJson } from "@/lib/api";
import { runAction, type ActionName } from "@/lib/agents";

const ACTIONS: ActionName[] = ["install", "update", "status", "start", "stop", "restart", "logs", "uninstall"];

export async function POST(req: Request, ctx: { params: Promise<{ id: string }> }) {
  const g = await guard(); if (g) return g;
  const { id } = await ctx.params;
  const b = await readJson<{ action?: string }>(req);
  if (!b.action || !ACTIONS.includes(b.action as ActionName)) return fail("invalid_action");
  const r = await runAction(id, b.action as ActionName);
  return ok(r);
}
