import { Hono } from 'hono'
import { resolveRuntimeForPurpose, rowToAgent, rowToRuntime } from '../runtime/runtime-registry.js'

const app = new Hono()

// 兼容旧前端的只读配置接口。真实评测/创建/运行统一走 /api/skill-run。
app.get('/', async (c) => {
  const identity = c.get('identity')
  if (!identity?.phone && !identity?.email) return c.json({ error: 'Unauthorized' }, 401)

  const { agent, runtime } = await resolveRuntimeForPurpose('evaluate')
  return c.json({
    hasProvider: !!runtime,
    hasEngine: !!runtime,
    engineKind: runtime ? runtime.protocol : null,
    runtime: runtime ? rowToRuntime(runtime) : null,
    agent: agent ? rowToAgent(agent) : null,
    agentRun: runtime?.protocol === 'agentrun'
      ? {
          code: runtime.code,
          name: runtime.name,
          protocol: runtime.protocol,
          runtimeName: runtime.runtime_name,
          endpointName: runtime.endpoint_name,
        }
      : null,
    provider: runtime
      ? { code: runtime.code, name: runtime.name, icon: runtime.icon, model: runtime.endpoint_name || runtime.local_command || 'default', protocol: runtime.protocol }
      : null,
  })
})

// 旧直连评测流已下线，避免绕过统一 AgentRun 执行链路。
app.post('/stream', async (c) => {
  const identity = c.get('identity')
  if (!identity?.phone && !identity?.email) return c.json({ error: 'Unauthorized' }, 401)
  return c.json({
    error: 'eval_stream_migrated',
    message: '评测执行已统一迁移到 /api/skill-run/stream（AgentRun）',
  }, 410)
})

export default app
