"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
    var ownKeys = function(o) {
        ownKeys = Object.getOwnPropertyNames || function (o) {
            var ar = [];
            for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
            return ar;
        };
        return ownKeys(o);
    };
    return function (mod) {
        if (mod && mod.__esModule) return mod;
        var result = {};
        if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
        __setModuleDefault(result, mod);
        return result;
    };
})();
Object.defineProperty(exports, "__esModule", { value: true });
const express_1 = require("express");
const api = __importStar(require("../api/knowledge"));
const router = (0, express_1.Router)();
/**
 * GET /api/knowledge/domains
 * List all knowledge domains with entry counts.
 */
router.get('/domains', async (_req, res) => {
    const result = await api.listDomains();
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/knowledge/entries
 * List entries within a domain.
 * Query: ?domain=...&tags=a,b&limit=50&offset=0
 */
router.get('/entries', async (req, res) => {
    const domain = req.query.domain;
    if (!domain) {
        res.status(400).json({ error: 'domain is required' });
        return;
    }
    const tags = req.query.tags
        ? req.query.tags.split(',').map((t) => t.trim()).filter(Boolean)
        : undefined;
    const search = req.query.search || undefined;
    const limit = req.query.limit ? parseInt(req.query.limit, 10) : undefined;
    const offset = req.query.offset ? parseInt(req.query.offset, 10) : undefined;
    const result = await api.listEntries({ domain, tags, search, limit, offset });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * GET /api/knowledge/entry
 * Get a single knowledge entry.
 * Query: ?domain=...&key=...
 */
router.get('/entry', async (req, res) => {
    const domain = req.query.domain;
    const key = req.query.key;
    if (!domain || !key) {
        res.status(400).json({ error: 'domain and key are required' });
        return;
    }
    const result = await api.getEntry({ domain, key });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * POST /api/knowledge/entry
 * Create or update a knowledge entry.
 * Body: { domain, key, data, tags? }
 */
router.post('/entry', async (req, res) => {
    const { domain, key, data, tags, replace } = req.body;
    if (!domain || !key || !data) {
        res.status(400).json({ error: 'domain, key, and data are required' });
        return;
    }
    const result = await api.storeEntry({ domain, key, data, tags, replace: !!replace });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * DELETE /api/knowledge/entry
 * Delete a knowledge entry.
 * Query: ?domain=...&key=...
 */
router.delete('/entry', async (req, res) => {
    const domain = req.query.domain;
    const key = req.query.key;
    if (!domain || !key) {
        res.status(400).json({ error: 'domain and key are required' });
        return;
    }
    const result = await api.deleteEntry({ domain, key });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * PUT /api/knowledge/field
 * Set a value at a specific JSONB path without clobbering siblings.
 * Body: { domain, key, path, value, tags? }
 */
router.put('/field', async (req, res) => {
    const { domain, key, path, value, tags } = req.body;
    if (!domain || !key || !path || value === undefined) {
        res.status(400).json({ error: 'domain, key, path, and value are required' });
        return;
    }
    const result = await api.setField({ domain, key, path, value, tags });
    res.status(result.status).json(result.data ?? { error: result.error });
});
/**
 * DELETE /api/knowledge/field
 * Remove a specific field at a JSONB path.
 * Query: ?domain=...&key=...&path=...
 */
router.delete('/field', async (req, res) => {
    const domain = req.query.domain;
    const key = req.query.key;
    const path = req.query.path;
    if (!domain || !key || !path) {
        res.status(400).json({ error: 'domain, key, and path are required' });
        return;
    }
    const result = await api.removeField({ domain, key, path });
    res.status(result.status).json(result.data ?? { error: result.error });
});
exports.default = router;
