/**
 * Skill management REST routes.
 *
 * GET    /api/skills              — list all discovered skills
 * PATCH  /api/skills/:id          — update skill (enable/disable)
 * POST   /api/skills/:id/install-tool — install a tool a skill requires
 */
import { checkSkillTools } from "../skills/index.js";
import { installSkillTool } from "../skills/install.js";
function serializeSkill(s) {
    const tools = checkSkillTools(s);
    return {
        id: s.id,
        name: s.name,
        description: s.description,
        filePath: s.filePath,
        source: s.source,
        enabled: s.enabled,
        ...(s.requires ? { requires: s.requires } : {}),
        ...(s.install ? { install: s.install } : {}),
        toolsSatisfied: tools.satisfied,
        missingTools: tools.missing,
    };
}
export function registerSkillRoutes(app, skillRegistry) {
    /** List all discovered skills. */
    app.get("/api/skills", async () => {
        return skillRegistry.list().map(serializeSkill);
    });
    /** Toggle a skill's enabled state. */
    app.patch("/api/skills/:id", async (req, reply) => {
        const skill = skillRegistry.get(req.params.id);
        if (!skill)
            return reply.status(404).send({ error: "Skill not found" });
        const body = req.body;
        if (body && typeof body.enabled === "boolean") {
            skillRegistry.setEnabled(req.params.id, body.enabled);
        }
        return serializeSkill(skill);
    });
    /**
     * Install a tool that a skill declares it needs.
     * Currently supports `kind: "node"` specs via `npm install -g <package>`.
     */
    app.post("/api/skills/:id/install-tool", async (req, reply) => {
        const skill = skillRegistry.get(req.params.id);
        if (!skill)
            return reply.status(404).send({ error: "Skill not found" });
        const body = req.body ?? {};
        try {
            const result = await installSkillTool({ skill, installId: body.installId });
            return { ...result, skill: serializeSkill(skill) };
        }
        catch (err) {
            const msg = err instanceof Error ? err.message : String(err);
            // Unsupported install kind / no matching option → 400; runtime failure → 500.
            const status = /Unsupported install kind|No matching install option/.test(msg) ? 400 : 500;
            return reply.status(status).send({ error: msg });
        }
    });
}
//# sourceMappingURL=skills.js.map