import { NextResponse } from "next/server";
import { z } from "zod";
import { passwordResetRequestSchema } from "@open-mercato/core/modules/customer_accounts/data/validators";
import {
  resolveTenantContext,
  TenantResolutionError
} from "@open-mercato/core/modules/customer_accounts/lib/resolveTenantContext";
import { createRequestContainer } from "@open-mercato/shared/lib/di/container";
import { rateLimitErrorSchema } from "@open-mercato/shared/lib/ratelimit/helpers";
import {
  checkAuthRateLimit,
  customerPasswordResetRateLimitConfig,
  customerPasswordResetIpRateLimitConfig
} from "@open-mercato/core/modules/customer_accounts/lib/rateLimiter";
import { readNormalizedEmailFromJsonRequest } from "@open-mercato/core/modules/customer_accounts/lib/rateLimitIdentifier";
const metadata = { requireAuth: false };
async function POST(req) {
  const rateLimitEmail = await readNormalizedEmailFromJsonRequest(req);
  const { error: rateLimitError } = await checkAuthRateLimit({
    req,
    ipConfig: customerPasswordResetIpRateLimitConfig,
    compoundConfig: customerPasswordResetRateLimitConfig,
    compoundIdentifier: rateLimitEmail
  });
  if (rateLimitError) return rateLimitError;
  let body;
  try {
    body = await req.json();
  } catch {
    return NextResponse.json({ ok: true });
  }
  const parsed = passwordResetRequestSchema.safeParse(body);
  if (!parsed.success) {
    return NextResponse.json({ ok: true });
  }
  const { email } = parsed.data;
  let tenantId;
  try {
    const context = await resolveTenantContext(req, parsed.data.tenantId);
    tenantId = context.tenantId;
  } catch (err) {
    if (err instanceof TenantResolutionError) {
      return NextResponse.json({ ok: true });
    }
    throw err;
  }
  const container = await createRequestContainer();
  const customerUserService = container.resolve("customerUserService");
  const customerTokenService = container.resolve("customerTokenService");
  const user = await customerUserService.findByEmail(email, tenantId);
  if (user) {
    await customerTokenService.createPasswordReset(user.id, tenantId);
    void import("@open-mercato/core/modules/customer_accounts/events").then(
      ({ emitCustomerAccountsEvent }) => emitCustomerAccountsEvent("customer_accounts.password_reset.requested", {
        userId: user.id,
        tenantId
      })
    ).catch(() => void 0);
  }
  return NextResponse.json({ ok: true });
}
const successSchema = z.object({ ok: z.literal(true) });
const methodDoc = {
  summary: "Request customer password reset",
  description: "Initiates a password reset flow. Always returns 200 to prevent email enumeration.",
  tags: ["Customer Authentication"],
  requestBody: {
    schema: passwordResetRequestSchema,
    description: "Password reset request with email."
  },
  responses: [
    { status: 200, description: "Request accepted", schema: successSchema }
  ],
  errors: [
    { status: 429, description: "Too many requests", schema: rateLimitErrorSchema }
  ]
};
const openApi = {
  summary: "Customer password reset request",
  description: "Handles password reset initiation for customer accounts.",
  methods: { POST: methodDoc }
};
export {
  POST,
  metadata,
  openApi
};
//# sourceMappingURL=reset-request.js.map
