/**
 * Planning tools — create and manage task plans via local SQLite.
 */
import { getDb, genId } from "../db.js";
export const planningTools = [
    {
        name: "createPlan",
        description: "Create an explicit task plan with steps marked as pending/in_progress/completed.",
        inputSchema: {
            type: "object",
            properties: {
                goal: {
                    type: "string",
                    description: "The high-level goal to accomplish",
                },
                steps: {
                    type: "array",
                    description: "List of steps to accomplish the goal",
                    items: {
                        type: "object",
                        properties: {
                            step: { type: "string", description: "Description of the step" },
                            status: {
                                type: "string",
                                enum: ["pending", "in_progress", "completed"],
                                description: "Current status of the step",
                            },
                            assignedAgent: {
                                type: "string",
                                description: "Which agent is responsible (optional)",
                            },
                            notes: {
                                type: "string",
                                description: "Additional notes (optional)",
                            },
                        },
                        required: ["step", "status"],
                    },
                },
            },
            required: ["goal", "steps"],
        },
        handler: async (args) => {
            const { goal, steps } = args;
            if (!goal || !steps || steps.length === 0) {
                throw new Error("Goal and steps are required");
            }
            const db = getDb();
            const planId = genId("plan");
            const now = new Date().toISOString();
            db.prepare("INSERT INTO plans (id, goal, steps, created_at, updated_at) VALUES (?, ?, ?, ?, ?)").run(planId, goal, JSON.stringify(steps), now, now);
            const markdown = `# Task Plan: ${goal}\n\nCreated: ${now}\n\n## Steps\n\n` +
                steps
                    .map((s, i) => {
                    const cb = s.status === "completed"
                        ? "x"
                        : s.status === "in_progress"
                            ? "/"
                            : " ";
                    const agent = s.assignedAgent ? ` (${s.assignedAgent})` : "";
                    const notes = s.notes ? `\n   - ${s.notes}` : "";
                    return `${i + 1}. [${cb}] ${s.step}${agent}${notes}`;
                })
                    .join("\n");
            return {
                success: true,
                planId,
                message: `Created task plan with ${steps.length} steps`,
                markdown,
            };
        },
    },
    {
        name: "updatePlanStep",
        description: "Update the status or notes of a specific step in a task plan.",
        inputSchema: {
            type: "object",
            properties: {
                planId: { type: "string", description: "ID of the plan to update" },
                stepIndex: {
                    type: "number",
                    description: "Index of the step to update (0-based)",
                },
                status: {
                    type: "string",
                    enum: ["pending", "in_progress", "completed"],
                    description: "New status for the step (optional)",
                },
                notes: {
                    type: "string",
                    description: "New notes for the step (optional)",
                },
            },
            required: ["planId", "stepIndex"],
        },
        handler: async (args) => {
            const { planId, stepIndex, status, notes } = args;
            const db = getDb();
            const row = db.prepare("SELECT * FROM plans WHERE id = ?").get(planId);
            if (!row)
                throw new Error(`Plan not found: ${planId}`);
            const steps = JSON.parse(row.steps);
            if (stepIndex < 0 || stepIndex >= steps.length) {
                throw new Error(`Invalid step index: ${stepIndex}`);
            }
            if (status)
                steps[stepIndex].status = status;
            if (notes !== undefined)
                steps[stepIndex].notes = notes;
            const now = new Date().toISOString();
            db.prepare("UPDATE plans SET steps = ?, updated_at = ? WHERE id = ?").run(JSON.stringify(steps), now, planId);
            return {
                success: true,
                message: `Updated step ${stepIndex + 1} in plan ${planId}`,
                step: steps[stepIndex],
            };
        },
    },
    {
        name: "getPlan",
        description: "Retrieve a task plan by ID.",
        inputSchema: {
            type: "object",
            properties: {
                planId: { type: "string", description: "ID of the plan to retrieve" },
            },
            required: ["planId"],
        },
        handler: async (args) => {
            const db = getDb();
            const row = db.prepare("SELECT * FROM plans WHERE id = ?").get(args.planId);
            if (!row)
                throw new Error(`Plan not found: ${args.planId}`);
            return {
                success: true,
                plan: {
                    id: row.id,
                    goal: row.goal,
                    steps: JSON.parse(row.steps),
                    createdAt: row.created_at,
                    updatedAt: row.updated_at,
                },
            };
        },
    },
];
//# sourceMappingURL=planningTools.js.map