import { NextRequest, NextResponse } from 'next/server' import pool from '@/lib/db' // GET /api/buyers - Get all buyers with referral counts export async function GET(request: NextRequest) { try { const [rows] = await pool.execute( `SELECT b.id, b.username, b.email, b.created_at, COALESCE(ref_counts.referral_count, 0) as referral_count FROM buyers b LEFT JOIN ( SELECT referrer, COUNT(*) as referral_count FROM referrals GROUP BY referrer ) ref_counts ON b.id = ref_counts.referrer ORDER BY b.created_at DESC` ) // Add access status for each buyer const buyersWithAccess = (rows as any[]).map((buyer: any) => { const referralCount = parseInt(buyer.referral_count) || 0 return { ...buyer, referral_count: referralCount, hasWholesaleAccess: referralCount >= 3, hasInnerCircleAccess: referralCount >= 10 } }) return NextResponse.json(buyersWithAccess) } catch (error) { console.error('Error fetching buyers:', error) return NextResponse.json( { error: 'Failed to fetch buyers' }, { status: 500 } ) } }