import { d as defineEventHandler, $ as getAuthRequestContext, q as readBody, c as createError, aC as isValidEmail, G as createContentManager, dv as generatePasswordResetToken, dw as emitAuthPasswordReset } from '../../../nitro/nitro.mjs';
import 'node:crypto';
import 'jsonwebtoken';
import 'node:fs';
import 'node:module';
import 'node:path';
import 'node:url';
import 'node:http';
import 'node:https';
import 'node:events';
import 'node:buffer';
import 'pino';
import 'drizzle-orm/better-sqlite3';
import 'drizzle-orm/node-postgres';
import 'drizzle-orm/mysql2';
import 'better-sqlite3';
import 'pg';
import 'mysql2/promise';
import 'drizzle-orm';
import 'fs';
import 'path';
import 'prom-client';
import 'bcryptjs';
import '@node-rs/argon2';
import 'node:async_hooks';
import 'crypto';
import 'node:dns';
import 'node:util';
import 'node:net';
import 'fs/promises';
import 'url';
import 'semver';

const forgotPassword_post = defineEventHandler(async (event) => {
  const reqCtx = getAuthRequestContext(event);
  try {
    const body = await readBody(event);
    if (!body || typeof body !== "object") {
      throw createError({
        statusCode: 400,
        statusMessage: "Bad Request",
        message: "Request body must be a valid JSON object"
      });
    }
    const email = typeof body.email === "string" ? body.email.trim().toLowerCase() : "";
    if (!email || !isValidEmail(email)) {
      throw createError({
        statusCode: 400,
        statusMessage: "Validation Error",
        message: "Valid email is required"
      });
    }
    const userManager = createContentManager("user");
    const existingUsers = await userManager.findMany({
      filters: { email },
      limit: 1
    });
    let resetToken;
    if (existingUsers.data.length > 0) {
      const user = existingUsers.data[0];
      const userId = Number(user.id);
      const userEmail = String(user.email || email);
      resetToken = generatePasswordResetToken(userId, userEmail);
      await emitAuthPasswordReset(userId, userEmail, "requested", reqCtx);
    }
    const response = {
      success: true,
      message: "If the account exists, password reset instructions have been sent"
    };
    if (resetToken && false) ;
    return response;
  } catch (error) {
    if (error && typeof error === "object" && "statusCode" in error) {
      throw error;
    }
    if (error instanceof Error && error.message.includes("Schema") && error.message.includes("not found")) {
      throw createError({
        statusCode: 500,
        statusMessage: "Configuration Error",
        message: "User schema is not configured. Please run database sync."
      });
    }
    console.error("[POST /api/auth/forgot-password] Error:", error);
    throw createError({
      statusCode: 500,
      statusMessage: "Password Reset Request Failed",
      message: error instanceof Error ? error.message : "Unknown error occurred"
    });
  }
});

export { forgotPassword_post as default };
//# sourceMappingURL=forgot-password.post.mjs.map
