import { NextResponse } from "next/server";
import { getSessionCustomer, resendVerificationEmail } from "../../../lib/customerAuth";
import { checkRateLimit, getClientIp } from "../../../lib/rateLimit";

export async function POST(request: Request) {
  if (!checkRateLimit(`resend-verification:${getClientIp(request)}`, { limit: 3, windowMs: 15 * 60 * 1000 })) {
    return NextResponse.json({ ok: false, reason: "Zu viele Versuche. Bitte in ein paar Minuten erneut probieren." }, { status: 429 });
  }

  const session = await getSessionCustomer();
  if (!session) return NextResponse.json({ ok: false, reason: "Bitte zuerst einloggen." }, { status: 401 });
  if (session.customer.emailVerified) return NextResponse.json({ ok: true });

  const ok = await resendVerificationEmail(session);
  return NextResponse.json({ ok }, { status: ok ? 200 : 500 });
}
