import multipart from '@fastify/multipart';
import { IngestPipeline, KnowledgeQuery, createEmbedder } from 'expxagents-knowledge';
export async function knowledgeRoutes(app, opts) {
    const { db } = opts;
    const embedder = createEmbedder('local');
    const pipeline = new IngestPipeline(db, embedder, { max_tokens: 512, overlap: 0.2 });
    const query = new KnowledgeQuery(db, embedder);
    await app.register(multipart, { limits: { fileSize: 10 * 1024 * 1024 } });
    // POST /api/knowledge/ingest — multipart file upload
    app.post('/api/knowledge/ingest', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const data = await request.file();
        if (!data)
            return reply.status(400).send({ error: 'File required' });
        const content = (await data.toBuffer()).toString('utf-8');
        const title = data.filename;
        const fields = data.fields;
        const squad_code = fields?.squad_code?.value;
        const tags = fields?.tags?.value?.split(',').map((t) => t.trim());
        const result = await pipeline.ingestText(content, title, { squad_code, tags });
        return reply.status(201).send(result);
    });
    // POST /api/knowledge/ingest-text
    app.post('/api/knowledge/ingest-text', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const { content, title, squad_code, tags } = request.body;
        if (!content?.trim() || !title?.trim()) {
            return reply.status(400).send({ error: 'content and title are required' });
        }
        const result = await pipeline.ingestText(content, title, { squad_code, tags });
        return reply.status(201).send(result);
    });
    // GET /api/knowledge/query
    app.get('/api/knowledge/query', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const { q, mode, squad, source_type, limit } = request.query;
        if (!q?.trim())
            return reply.status(400).send({ error: 'q parameter is required' });
        const result = await query.query(q, {
            mode: mode ?? 'auto',
            scope: { squad, source_type: source_type },
            limit: limit ? parseInt(limit, 10) : 10,
        });
        return reply.send(result);
    });
    // GET /api/knowledge/documents
    app.get('/api/knowledge/documents', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const limit = request.query.limit ? parseInt(request.query.limit, 10) : 50;
        const offset = request.query.offset ? parseInt(request.query.offset, 10) : 0;
        return reply.send({ documents: query.listDocuments(limit, offset) });
    });
    // GET /api/knowledge/documents/:id
    app.get('/api/knowledge/documents/:id', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const doc = query.getDocument(request.params.id);
        if (!doc)
            return reply.status(404).send({ error: 'Document not found' });
        return reply.send(doc);
    });
    // DELETE /api/knowledge/documents/:id — admin only
    app.delete('/api/knowledge/documents/:id', { preHandler: [app.requireAuth] }, async (request, reply) => {
        if (request.user?.role !== 'admin') {
            return reply.status(403).send({ error: 'Admin access required' });
        }
        const deleted = query.deleteDocument(request.params.id);
        if (!deleted)
            return reply.status(404).send({ error: 'Document not found' });
        return reply.send({ ok: true });
    });
    // GET /api/knowledge/entities
    app.get('/api/knowledge/entities', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const limit = request.query.limit ? parseInt(request.query.limit, 10) : 50;
        const entities = query.listEntities(request.query.type, limit);
        return reply.send({ entities });
    });
    // GET /api/knowledge/entities/:id/relations
    app.get('/api/knowledge/entities/:id/relations', { preHandler: [app.requireAuth] }, async (request, reply) => {
        const relations = query.getEntityRelations(request.params.id);
        return reply.send({ relations });
    });
    // GET /api/knowledge/stats
    app.get('/api/knowledge/stats', { preHandler: [app.requireAuth] }, async (_request, reply) => {
        return reply.send(query.getStats());
    });
}
//# sourceMappingURL=knowledge-routes.js.map