import { d as defineEventHandler, g as getCookie, c as createError, r as readBody } from '../../../../nitro/nitro.mjs';
import { a as getUserFromToken, r as readUsers, w as writeUsers } from '../../../../_/auth.mjs';
import nodemailer from 'nodemailer';
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 approve_post = defineEventHandler(async (event) => {
  try {
    const token = getCookie(event, "auth_token");
    const currentUser = await getUserFromToken(token);
    if (!currentUser || currentUser.role !== "admin" && currentUser.role !== "vorstand") {
      throw createError({
        statusCode: 403,
        message: "Zugriff verweigert"
      });
    }
    const body = await readBody(event);
    const { userId, role } = body;
    const users = await readUsers();
    const user = users.find((u) => u.id === userId);
    if (!user) {
      throw createError({
        statusCode: 404,
        message: "Benutzer nicht gefunden"
      });
    }
    user.active = true;
    user.role = role || "mitglied";
    const updatedUsers = users.map((u) => u.id === userId ? user : u);
    await writeUsers(updatedUsers);
    try {
      const transporter = nodemailer.createTransporter({
        host: process.env.SMTP_HOST || "smtp.gmail.com",
        port: process.env.SMTP_PORT || 587,
        secure: false,
        auth: {
          user: process.env.SMTP_USER,
          pass: process.env.SMTP_PASS
        }
      });
      await transporter.sendMail({
        from: process.env.SMTP_FROM || "noreply@harheimertc.de",
        to: user.email,
        subject: "Zugang freigeschaltet - Harheimer TC",
        html: `
          <h2>Zugang freigeschaltet</h2>
          <p>Hallo ${user.name},</p>
          <p>Ihr Zugang zum Mitgliederbereich wurde freigeschaltet!</p>
          <p>Sie k\xF6nnen sich jetzt mit Ihrer E-Mail-Adresse und Ihrem Passwort anmelden.</p>
          <p><a href="https://harheimertc.tsschulz.de/login">Zum Login</a></p>
          <br>
          <p>Mit sportlichen Gr\xFC\xDFen,<br>Ihr Harheimer TC</p>
        `
      });
    } catch (emailError) {
      console.error("E-Mail-Versand fehlgeschlagen:", emailError);
    }
    return {
      success: true,
      message: "Benutzer wurde freigeschaltet"
    };
  } catch (error) {
    console.error("Fehler beim Freischalten:", error);
    throw error;
  }
});

export { approve_post as default };
//# sourceMappingURL=approve.post.mjs.map
