/**
 * POST /api/magic/devices/:deviceId/cancel-cooldown
 *
 * Removes an active Magic device cooldown immediately.
 */
export default defineEventHandler(async (event) => {
  const user = await requireUser(event);
  const deviceId = getRouterParam(event, "deviceId");

  if (!deviceId) {
    throw createError({ statusCode: 400, message: "deviceId required" });
  }

  const db = usePrisma();
  const device = await db.userDevice.findUnique({
    where: { userId_deviceId: { userId: user.id, deviceId } },
    select: { id: true, magicEnrolledAt: true, magicRevokedAt: true },
  });

  if (!device || !device.magicEnrolledAt || device.magicRevokedAt) {
    throw createError({ statusCode: 404, message: "Magic-Binding nicht gefunden" });
  }

  await db.userDevice.update({
    where: { id: device.id },
    data: { magicCooldownUntil: null },
  });

  return { success: true, data: { cooldownUntil: null } };
});
