import { NextResponse } from 'next/server';
import { listAgents, getDefaultAgentId, resolveTerminalLaunch } from '@/lib/agents';

export async function GET(req: Request) {
  const url = new URL(req.url);
  const resolve = url.searchParams.get('resolve');

  // GET /api/agents?resolve=claude → resolve terminal launch info for an agent
  if (resolve) {
    const info = resolveTerminalLaunch(resolve);
    return NextResponse.json(info);
  }

  // ?include=cli|api|all (default: cli) — API profiles aren't useful in
  // Terminal / Task / Pipeline pickers (no CLI binary to spawn), so the
  // default hides them. WorkspaceView's API mode passes ?include=all
  // (or ?include=api) to surface them when needed.
  const include = (url.searchParams.get('include') || 'cli').toLowerCase();
  const all = listAgents();
  const agents = include === 'all'
    ? all
    : include === 'api'
      ? all.filter((a: any) => a.backendType === 'api')
      : all.filter((a: any) => a.backendType !== 'api');
  const defaultAgent = getDefaultAgentId();
  return NextResponse.json({ agents, defaultAgent });
}
