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

export async function GET() {
  try {
    const result = await postgresClient.query(`
      SELECT u.user_id, u.username, u.email, u.realname, u.status,
             u.is_enrolled, u.last_login, u.notes, u.synced_at,
             da.name as account_name, da.account_id as duo_account_id,
             da.is_parent as is_parent_account
      FROM duo_users u
      JOIN duo_accounts da ON da.account_id = u.duo_account_id
      WHERE u.status IN ('bypass', 'disabled')
      ORDER BY da.name ASC, u.username ASC
    `);

    const bypass = result.rows.filter((u: any) => u.status === 'bypass');
    const disabled = result.rows.filter((u: any) => u.status === 'disabled');

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