// apps/admin/server/api/moderation/[id]/dismiss.post.ts
//
// Proxy: leitet Dismiss-Request (flag clear) ans Backend.

export default defineEventHandler(async (event) => {
  const config = useRuntimeConfig();
  const apiBase = config.public.apiBase;
  const adminSecret = config.adminSecret;

  if (!adminSecret) {
    throw createError({
      statusCode: 500,
      statusMessage: "ADMIN_SECRET nicht konfiguriert",
    });
  }

  const id = getRouterParam(event, "id");
  if (!id) throw createError({ statusCode: 400, statusMessage: "ID fehlt" });

  const body = await readBody(event).catch(() => ({}));

  try {
    return await $fetch(
      `${apiBase}/api/admin/moderation/${encodeURIComponent(id)}/dismiss`,
      {
        method: "POST",
        headers: { "x-admin-secret": adminSecret },
        body: body ?? {},
      },
    );
  } catch (err: any) {
    throw createError({
      statusCode: err?.statusCode ?? 502,
      statusMessage:
        err?.statusMessage ?? err?.message ?? "Backend-Request fehlgeschlagen",
    });
  }
});
