import { NextRequest, NextResponse } from "next/server";
import { getAgent } from "@/lib/agent-registry";
import { createChannelClient } from "@/lib/channel-client";

export async function GET(
  request: NextRequest,
  { params }: { params: Promise<{ agent: string }> }
) {
  const { agent: agentName } = await params;
  const agent = await getAgent(agentName);
  if (!agent) {
    return NextResponse.json({ error: "Agent not found" }, { status: 404 });
  }

  const conversationId = request.nextUrl.searchParams.get("conversation_id");
  if (!conversationId) {
    return NextResponse.json(
      { error: "Missing conversation_id" },
      { status: 400 }
    );
  }

  try {
    const client = createChannelClient(agent.channelUrl, agent.secret);
    const conversation = await client.getConversation(conversationId);
    return NextResponse.json(conversation);
  } catch {
    return NextResponse.json(
      { error: "Conversation not found" },
      { status: 404 }
    );
  }
}
