import { deleteOldNotifications } from "../../db/notifications";

/**
 * POST /api/cron/notifications-cleanup
 *
 * Löscht Notifications die älter als 3 Tage sind.
 * Einrichten auf Hetzner via crontab:
 *
 *   crontab -e
 *   0 2 * * * curl -s -X POST https://rebreak.org/api/cron/notifications-cleanup \
 *     -H "x-cron-secret: $NUXT_CRON_SECRET" >> /var/log/rebreak-cron.log 2>&1
 */
export default defineEventHandler(async (event) => {
  const secret = getHeader(event, "x-cron-secret");
  if (!secret || secret !== process.env.NUXT_CRON_SECRET) {
    throw createError({ statusCode: 401, message: "Unauthorized" });
  }

  const result = await deleteOldNotifications();
  return { deleted: result.count };
});
