import { NextRequest, NextResponse } from 'next/server';
import { forgotPasswordViaService } from '@/lib/platform-client';
import { getRequestProductId } from '@/lib/product-config';
import { logError } from '@/lib/logger';

export async function POST(req: NextRequest) {
  try {
    const { email } = await req.json();
    if (!email) {
      return NextResponse.json({ error: 'Email required' }, { status: 400 });
    }

    const result = await forgotPasswordViaService(email, getRequestProductId(req));
    return NextResponse.json(result);
  } catch (error) {
    logError('Forgot password error', error);
    // Always return success to prevent email enumeration
    return NextResponse.json({ message: 'If that email exists, a reset link has been sent.' });
  }
}
