import { NextRequest, NextResponse } from 'next/server'; import { getSession } from '@/lib/auth'; import { getGHLClientForUser } from '@/lib/ghl/helpers'; export async function GET(request: NextRequest) { const session = await getSession(); if (!session) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const ghl = await getGHLClientForUser(session.user.id); if (!ghl) { return NextResponse.json({ error: 'GHL not configured' }, { status: 400 }); } const { searchParams } = new URL(request.url); const limit = parseInt(searchParams.get('limit') || '20'); const status = searchParams.get('status') as 'all' | 'read' | 'unread' | 'starred' || 'all'; const contactId = searchParams.get('contactId') || undefined; try { const conversations = await ghl.conversations.getAll({ limit, status, contactId, }); return NextResponse.json(conversations); } catch (error) { console.error('Failed to get conversations:', error); return NextResponse.json({ error: 'Failed to fetch conversations' }, { status: 500 }); } }