// app/api/verify-code/route.js
import { NextResponse } from 'next/server';

export async function POST(req) {
  try {
    const body = await req.json();
    console.log(body);
    const response = await fetch(`${process.env.NEXT_PUBLIC_BACKEND_URL}/api/auth/verify-email`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(body),
    });

    const data = await response.json();
    console.log("Verify response:", data);
    if (!response.ok) {
      return NextResponse.json({ error: data.error || 'Verification failed' }, { status: response.status });
    }

    // Determine redirect URL based on user role
    let redirectUrl = '/';
    switch (data?.user?.role) {
      case 'patient':
        redirectUrl = '/dashboard/patient/profile';
        break;
      case 'doctor':
        redirectUrl = '/dashboard/doctor/profile';
        break;
      case 'lab':
        redirectUrl = '/dashboard/lab/profile';
        break;
      case 'pharmacy':
        redirectUrl = '/dashboard/pharmacy/profile';
        break;
      default:
        redirectUrl = '/dashboard';
    }

    // Return both the data and redirect URL
    return NextResponse.json({ success: true, redirectUrl, user: data.user }, { status: 200 });

  } catch (error) {
    console.error("Verification error:", error);
    return NextResponse.json({ error: 'Internal server error' }, { status: 500 });
  }
}
