import { NextRequest, NextResponse } from 'next/server';
import { authLimiter } from '@/lib/middleware/rateLimiter';

/**
 * Registration endpoint with rate limiting
 * Limited to 5 attempts per 15 minutes per IP
 */
export async function POST(request: NextRequest) {
  // Apply rate limiting
  const rateLimitResult = await authLimiter(request);
  if (rateLimitResult) return rateLimitResult;

  try {
    const body = await request.json();
    const { email, password, name } = body;

    // TODO: Implement actual registration logic
    // This is a placeholder - actual registration will be handled by backend

    // For now, forward to backend API
    const backendUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3020';
    const response = await fetch(`${backendUrl}/api/v1/auth/register`, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ email, password, name }),
    });

    const data = await response.json();

    if (!response.ok) {
      return NextResponse.json(data, { status: response.status });
    }

    return NextResponse.json(data, { status: 201 });
  } catch (error) {
    console.error('[Auth] Registration error:', error);
    return NextResponse.json(
      {
        error: 'AUTH_REGISTRATION_FAILED',
        message: 'Registration failed. Please try again.',
      },
      { status: 500 }
    );
  }
}
