import { NextRequest, NextResponse } from "next/server";
import { getConversation, listConversations } from "@/lib/agent-client";

export async function GET(request: NextRequest) {
  const conversationId = request.nextUrl.searchParams.get("id");

  try {
    if (conversationId) {
      const conversation = await getConversation(conversationId);
      return NextResponse.json(conversation);
    }
    const result = await listConversations();
    return NextResponse.json(result);
  } catch (err) {
    const message = err instanceof Error ? err.message : "Unknown error";
    return NextResponse.json({ error: message }, { status: 502 });
  }
}
