import { defineHandler } from '@kb-labs/sdk';
import { SessionManager } from '@kb-labs/agent-core';
import { promises } from 'fs';

// src/rest/handlers/approve-session-plan-handler.ts
async function loadPlan(planPath) {
  const content = await promises.readFile(planPath, "utf-8");
  return JSON.parse(content);
}
var approve_session_plan_handler_default = defineHandler({
  async execute(ctx, input) {
    const params = input.params;
    const sessionId = params?.sessionId;
    const body = input.body ?? {};
    if (!sessionId) {
      throw new Error("Session ID is required");
    }
    const baseManager = new SessionManager(ctx.cwd);
    const session = await baseManager.loadSession(sessionId);
    if (!session) {
      throw new Error(`Session not found: ${sessionId}`);
    }
    const workingDir = session.workingDir || ctx.cwd;
    const sessionManager = new SessionManager(workingDir);
    const planPath = sessionManager.getSessionPlanPath(sessionId);
    let plan;
    try {
      plan = await loadPlan(planPath);
    } catch {
      throw new Error(`Plan not found for session ${sessionId}`);
    }
    const approvedAt = (/* @__PURE__ */ new Date()).toISOString();
    const approvedPlan = {
      ...plan,
      status: "approved",
      updatedAt: approvedAt,
      approvalComment: body.comment?.trim() || void 0,
      approvedAt
    };
    await promises.writeFile(planPath, JSON.stringify(approvedPlan, null, 2), "utf-8");
    ctx.platform.logger.info(`[approve-session-plan] Session ${sessionId}: plan approved`);
    return {
      sessionId,
      plan: approvedPlan,
      approvedAt
    };
  }
});

export { approve_session_plan_handler_default as default };
//# sourceMappingURL=approve-session-plan-handler.js.map
//# sourceMappingURL=approve-session-plan-handler.js.map