import { NextRequest, NextResponse } from 'next/server'
import type { Agent, AgentsResponse } from '@/lib/types'
import { getAllAvailableAgents } from '@/lib/agents/factory'

/**
 * GET /api/agents
 * Returns list of available agents from code-based definitions
 *
 * Agents are now defined in src/lib/agents/definitions.ts
 * instead of environment variables
 */
export async function GET(request: NextRequest): Promise<NextResponse<AgentsResponse>> {
  try {
    // Get standard agents from definitions
    const agentDefs = getAllAvailableAgents()

    // Convert to Agent format (client-side response)
    const agents: Agent[] = agentDefs.map((agent) => ({
      id: agent.id,
      name: agent.name,
      description: agent.description,
      // Note: webhookUrl is no longer used with Vercel AI SDK
      // All requests go through the unified /api/chat endpoint
    }))

    if (agents.length === 0) {
      console.warn('[agents] No agents configured')
    }

    console.log(`[agents] Loaded ${agents.length} agents`)

    return NextResponse.json({ agents })
  } catch (error) {
    console.error('[agents] Error loading agents:', error)
    return NextResponse.json(
      { agents: [], error: 'Failed to load agents' },
      { status: 500 }
    )
  }
}
