/**
 * GET /api/sdk/agents/[agentId] — Agent-Detail mit SOUL-Content
 */

import { NextRequest, NextResponse } from 'next/server'
import { getAgentById } from '@/lib/sdk/agents/agent-registry'
import { readSoulFile, getSoulFileStats, soulFileExists } from '@/lib/sdk/agents/soul-reader'

export async function GET(
  _request: NextRequest,
  { params }: { params: Promise<{ agentId: string }> }
) {
  try {
    const { agentId } = await params
    const agent = getAgentById(agentId)

    if (!agent) {
      return NextResponse.json({ error: 'Agent not found' }, { status: 404 })
    }

    const exists = await soulFileExists(agentId)
    const soulContent = await readSoulFile(agentId)
    const fileStats = await getSoulFileStats(agentId)

    return NextResponse.json({
      ...agent,
      status: exists ? agent.status : 'error',
      soulContent: soulContent || '',
      createdAt: fileStats?.createdAt || null,
      updatedAt: fileStats?.updatedAt || null,
      fileSize: fileStats?.size || 0,
    })
  } catch (error) {
    console.error('Error fetching agent detail:', error)
    return NextResponse.json({ error: 'Failed to fetch agent' }, { status: 500 })
  }
}
