import { NextRequest, NextResponse } from 'next/server';
import { requireAdmin } from '@/lib/auth-server';
import { listDevices, getNotificationPrefs } from '@/lib/platform-client';

export async function GET(req: NextRequest) {
  const admin = await requireAdmin(req);
  if (!admin) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });

  const userId = req.nextUrl.searchParams.get('userId');
  if (!userId) return NextResponse.json({ error: 'userId is required' }, { status: 400 });

  try {
    const [devicesResult, prefs] = await Promise.all([
      listDevices(userId),
      getNotificationPrefs(userId),
    ]);
    return NextResponse.json({
      devices: devicesResult.devices ?? [],
      prefs: prefs ?? { pushEnabled: true, emailEnabled: true, categories: {} },
    });
  } catch (err) {
    return NextResponse.json({ error: String(err) }, { status: 500 });
  }
}
