import { NextRequest, NextResponse } from "next/server";
import { getAgentDetails } from "@/lib/agent-registry";
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 details = await getAgentDetails(agentName);
  if (!details) {
    return NextResponse.json({ error: "Agent not found" }, { status: 404 });
  }

  // Check online status
  let online = false;
  const agent = await getAgent(agentName);
  if (agent) {
    try {
      const client = createChannelClient(agent.channelUrl, agent.secret);
      await client.getHealth();
      online = true;
    } catch {
      // offline
    }
  }

  return NextResponse.json({ ...details, online });
}
