// KBDB Base — atomic universal table worker (arcrun self-hosted data layer + official core).
// SDD: .agents/specs/arcrun/kbdb-base/design.md
//
// Base = D1 only (free, no credit card): entries / templates / records + LIKE search + recipe-stats.
// Optional modules (NOT in this base): embed (Vectorize+AI binding, semantic search), triplet (separate repo).
import { Hono } from 'hono';
import type { Bindings } from './types';
import { entryRoutes } from './routes/entries';
import { templateRoutes } from './routes/templates';
import { recordRoutes } from './routes/records';
import { recipeStatRoutes } from './routes/recipe-stats';
import { embedRoutes } from './routes/embed';
import { mapRoutes } from './routes/map';

const app = new Hono<{ Bindings: Bindings }>();

app.get('/', (c) => c.json({ service: 'arcrun-kbdb', tier: 'base', status: 'ok' }));
app.get('/health', (c) => c.json({ ok: true }));

app.route('/entries', entryRoutes);
app.route('/templates', templateRoutes);
app.route('/records', recordRoutes);
app.route('/recipe-stats', recipeStatRoutes);
// Optional embed module admin (backfill). Route mounts unconditionally; the handler
// honestly 409s when the embed binding is off (base 對內容語意無知，只認通用 embed 旗標)。
app.route('/embed', embedRoutes);
// 藏書地圖（library-map SDD M2 / Arcrun#39）：聚合 SQL 只准住基本盤（D6 推論），
// recompute＋讀端都在這裡；ingest workflow 只透過 HTTP 呼叫（A 類接 B 類 API，牆不破）。
app.route('/map', mapRoutes);

export default app;
