import { guard, ok, fail } from "@/lib/api";
import { ping } from "@/lib/ssh";
import { prisma } from "@/lib/db";

export async function POST(_req: Request, ctx: { params: Promise<{ id: string }> }) {
  const g = await guard(); if (g) return g;
  const { id } = await ctx.params;
  try {
    const r = await ping(id);
    if (r.ok) {
      await prisma.host.update({ where: { id }, data: { lastSeenAt: new Date() } });
    }
    return ok({ ok: r.ok, stdout: r.stdout, stderr: r.stderr, code: r.code });
  } catch (e) {
    return fail(e instanceof Error ? e.message : String(e), 500);
  }
}
