/**
 * Proxy MFA verification to platform-service.
 * POST /api/auth/mfa/verify { challengeToken, code, method }
 * Returns { accessToken, refreshToken, user }.
 */

import { NextRequest, NextResponse } from 'next/server';

const PLATFORM_API = process.env.PLATFORM_SERVICE_URL || 'http://localhost:4003';

export async function POST(req: NextRequest) {
  try {
    const body = await req.json();
    const { challengeToken, code, method } = body;

    if (!challengeToken || !code) {
      return NextResponse.json({ error: 'challengeToken and code required' }, { status: 400 });
    }

    const res = await fetch(`${PLATFORM_API}/api/auth/mfa/verify`, {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ challengeToken, code, method: method || 'totp' }),
    });

    const data = await res.json();
    if (!res.ok) {
      return NextResponse.json(
        { error: data.error || 'MFA verification failed' },
        { status: res.status }
      );
    }

    return NextResponse.json(data);
  } catch {
    return NextResponse.json({ error: 'Platform service unavailable' }, { status: 502 });
  }
}
