import { NextRequest, NextResponse } from "next/server";

const CHANNEL_URL = process.env.CHANNEL_URL || "http://127.0.0.1:7100";
const CHANNEL_SECRET = process.env.CHANNEL_SECRET || "";

function channelHeaders() {
  return { Authorization: `Bearer ${CHANNEL_SECRET}` };
}

export async function GET(
  _req: NextRequest,
  { params }: { params: Promise<{ name: string }> }
) {
  const { name } = await params;
  try {
    const res = await fetch(`${CHANNEL_URL}/tasks/agent/${encodeURIComponent(name)}`, {
      headers: channelHeaders(),
    });
    const data = await res.json();
    return NextResponse.json(data, { status: res.status });
  } catch (err) {
    const message = err instanceof Error ? err.message : "Unknown error";
    return NextResponse.json({ error: message }, { status: 502 });
  }
}
