import { NextRequest, NextResponse } from 'next/server';
import { getConversations } from '@/lib/db';

export async function GET(req: NextRequest) {
  try {
    const url = req.nextUrl;
    const search = url.searchParams.get('search') || undefined;
    const status = url.searchParams.get('status') || undefined;
    const limit = parseInt(url.searchParams.get('limit') || '50', 10);
    const offset = parseInt(url.searchParams.get('offset') || '0', 10);

    const conversations = getConversations({ search, status, limit, offset });

    return NextResponse.json({ conversations });
  } catch (err) {
    console.error('GET /api/conversations error:', err);
    return NextResponse.json({ error: 'Failed to load conversations' }, { status: 500 });
  }
}
