import { NextResponse } from 'next/server'
import { getDefaultAgentId } from '@/app/api/nova-config'

export async function GET() {
  const baseUrl = process.env.NOVA_BASE_URL || ''
  const agentId = getDefaultAgentId()
  const tenantId = process.env.NOVA_TENANT_ID || ''

  let stats = {
    activeConnections: 0,
    totalMessages: 0,
    averageResponseTime: 0,
  }

  try {
    const { getStats } = await import('@/remote-control/shared/nova-bridge')
    stats = getStats()
  } catch {
    // nova-bridge 未加载
  }

  let status: 'connected' | 'disconnected' = 'disconnected'
  try {
    const response = await fetch(`${baseUrl}/health`, {
      signal: AbortSignal.timeout(5000),
    })
    if (response.ok) {
      status = 'connected'
    }
  } catch {
    // 连接失败
  }

  return NextResponse.json({
    baseUrl,
    agentId,
    tenantId,
    status,
    ...stats,
  })
}
