"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDocsRouter = createDocsRouter;
const express_1 = require("express");
const validation_1 = require("../../api/rest/validation");
/** Express 5 wildcard params are arrays — join them back into a path string. */
function joinParam(value) {
    return Array.isArray(value) ? value.join('/') : String(value);
}
function createDocsRouter() {
    const router = (0, express_1.Router)({ mergeParams: true });
    function getProject(req) {
        return req.project;
    }
    // List topics (files)
    router.get('/topics', (0, validation_1.validateQuery)(validation_1.listQuerySchema), (req, res, next) => {
        try {
            const p = getProject(req);
            if (!p.docManager)
                return res.json({ results: [] });
            const q = req.validatedQuery;
            const { results: topics, total } = p.docManager.listFiles(q.filter, q.limit, q.offset);
            res.json({ results: topics, total });
        }
        catch (err) {
            next(err);
        }
    });
    // Get TOC for a file
    router.get('/toc/*fileId', (req, res, next) => {
        try {
            const p = getProject(req);
            if (!p.docManager)
                return res.status(404).json({ error: 'No doc graph' });
            const fileId = joinParam(req.params.fileId);
            const chunks = p.docManager.getFileChunks(fileId);
            if (!chunks.length)
                return res.status(404).json({ error: 'File not found' });
            res.json({ results: chunks });
        }
        catch (err) {
            next(err);
        }
    });
    // Get node by ID
    router.get('/nodes/*nodeId', (req, res, next) => {
        try {
            const p = getProject(req);
            if (!p.docManager)
                return res.status(404).json({ error: 'No doc graph' });
            const nodeId = joinParam(req.params.nodeId);
            const node = p.docManager.getNode(nodeId);
            if (!node)
                return res.status(404).json({ error: 'Node not found' });
            const { embedding: _, ...rest } = node;
            res.json(rest);
        }
        catch (err) {
            next(err);
        }
    });
    // Search docs
    router.get('/search', (0, validation_1.validateQuery)(validation_1.searchQuerySchema), async (req, res, next) => {
        try {
            const p = getProject(req);
            if (!p.docManager)
                return res.json({ results: [] });
            const q = req.validatedQuery;
            const results = await p.docManager.search(q.q, {
                topK: q.topK,
                minScore: q.minScore,
                searchMode: q.searchMode,
                bfsDepth: q.bfsDepth,
                maxResults: q.maxResults,
                bfsDecay: q.bfsDecay,
            });
            res.json({ results });
        }
        catch (err) {
            next(err);
        }
    });
    return router;
}
