import { d as defineEventHandler, g as getCookie, b as getHeader, c as createError, r as readBody } from '../../nitro/nitro.mjs';
import { b as verifyToken, e as getUserById } from '../../_/auth.mjs';
import { s as saveMember } from '../../_/members.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 members_post = defineEventHandler(async (event) => {
  try {
    let token = getCookie(event, "auth_token");
    if (!token) {
      const authHeader = getHeader(event, "authorization");
      if (authHeader && authHeader.startsWith("Bearer ")) {
        token = authHeader.substring(7);
      }
    }
    if (!token) {
      throw createError({
        statusCode: 401,
        message: "Nicht authentifiziert. Bitte Token im Cookie oder Authorization-Header bereitstellen."
      });
    }
    const decoded = verifyToken(token);
    if (!decoded) {
      throw createError({
        statusCode: 401,
        message: "Ung\xFCltiges Token."
      });
    }
    const user = await getUserById(decoded.id);
    if (!user) {
      throw createError({
        statusCode: 401,
        message: "Benutzer nicht gefunden."
      });
    }
    if (user.role !== "admin" && user.role !== "vorstand") {
      throw createError({
        statusCode: 403,
        message: "Keine Berechtigung zum Hinzuf\xFCgen/Bearbeiten von Mitgliedern. Erforderlich: admin oder vorstand Rolle."
      });
    }
    const body = await readBody(event);
    const { id, firstName, lastName, geburtsdatum, email, phone, address, notes } = body;
    if (!firstName || !lastName) {
      throw createError({
        statusCode: 400,
        message: "Vorname und Nachname sind erforderlich."
      });
    }
    if (!geburtsdatum) {
      throw createError({
        statusCode: 400,
        message: "Geburtsdatum ist erforderlich, um Duplikate zu vermeiden."
      });
    }
    try {
      await saveMember({
        id: id || void 0,
        firstName,
        lastName,
        geburtsdatum: geburtsdatum || "",
        email: email || "",
        phone: phone || "",
        address: address || "",
        notes: notes || ""
      });
      return {
        success: true,
        message: "Mitglied erfolgreich gespeichert."
      };
    } catch (memberError) {
      if (memberError.message && memberError.message.includes("existiert bereits")) {
        throw createError({
          statusCode: 409,
          message: memberError.message
        });
      }
      throw memberError;
    }
  } catch (error) {
    console.error("Fehler beim Speichern des Mitglieds:", error);
    if (error.statusCode) {
      throw error;
    }
    throw createError({
      statusCode: error.statusCode || 500,
      message: error.message || "Fehler beim Speichern des Mitglieds."
    });
  }
});

export { members_post as default };
//# sourceMappingURL=members.post.mjs.map
