import { NextRequest, NextResponse } from 'next/server';
import { logError } from '@/lib/logger';
import { getMeViaService } from '@/lib/platform-client';

export async function GET(req: NextRequest) {
  try {
    const token = req.headers.get('authorization')?.replace('Bearer ', '') ?? '';
    if (!token) {
      return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
    }
    const user = await getMeViaService(token);
    return NextResponse.json({
      id: user.id,
      email: user.email,
      name: user.displayName,
      role: user.role,
      plan: user.plan,
    });
  } catch (error) {
    logError('Auth/me error', error);
    return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
  }
}
