import { NextRequest, NextResponse } from 'next/server';
import { postgresClient } from '@/lib/services/postgres-client';

export async function GET(request: NextRequest, { params }: { params: Promise<{ id: string }> }) {
  try {
    const { id } = await params;

    const result = await postgresClient.query(`
      SELECT u.user_id, u.username, u.email, u.realname, u.status,
             u.is_enrolled, u.last_login, u.last_directory_sync, u.created,
             u.phones_count, u.groups, u.aliases, u.enable_auto_prompt, u.notes,
             u.synced_at
      FROM duo_users u
      WHERE u.duo_account_id = $1
      ORDER BY u.username ASC
    `, [id]);

    return NextResponse.json({ users: result.rows, total: result.rowCount });
  } catch (error: any) {
    console.error('[DuoAPI] Error fetching users:', error);
    return NextResponse.json({ error: error.message }, { status: 500 });
  }
}
