import { d as defineEventHandler, g as getCookie, c as createError, r as readBody } from '../../nitro/nitro.mjs';
import { b as verifyToken, e as getUserById } from '../../_/auth.mjs';
import { s as saveTermin } from '../../_/termine.mjs';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'node:fs';
import 'node:path';
import 'node:crypto';
import 'node:url';
import 'bcryptjs';
import 'jsonwebtoken';
import 'fs';
import 'path';
import '../../_/encryption.mjs';
import 'crypto';

const termineManage_post = defineEventHandler(async (event) => {
  try {
    const token = getCookie(event, "auth_token");
    if (!token) {
      throw createError({
        statusCode: 401,
        message: "Nicht authentifiziert."
      });
    }
    const decoded = verifyToken(token);
    if (!decoded) {
      throw createError({
        statusCode: 401,
        message: "Ung\xFCltiges Token."
      });
    }
    const user = await getUserById(decoded.id);
    if (!user || user.role !== "admin" && user.role !== "vorstand") {
      throw createError({
        statusCode: 403,
        message: "Keine Berechtigung zum Erstellen von Terminen."
      });
    }
    const body = await readBody(event);
    const { datum, uhrzeit, titel, beschreibung, kategorie } = body;
    if (!datum || !titel) {
      throw createError({
        statusCode: 400,
        message: "Datum und Titel sind erforderlich."
      });
    }
    await saveTermin({
      datum,
      uhrzeit: uhrzeit || "",
      titel,
      beschreibung: beschreibung || "",
      kategorie: kategorie || "Sonstiges"
    });
    return {
      success: true,
      message: "Termin erfolgreich gespeichert."
    };
  } catch (error) {
    console.error("Fehler beim Speichern des Termins:", error);
    throw error;
  }
});

export { termineManage_post as default };
//# sourceMappingURL=termine-manage.post.mjs.map
