import { ensureAtLeastOneField, optionalString, requireStringField } from "./tool-helpers.mjs";

export function createUpdateNoteTool(client) {
  return {
    name: "productier_update_note",
    description: "Update a Productier note.",
    inputSchema: {
      type: "object",
      properties: {
        noteId: { type: "string" },
        title: { type: "string" },
        content: { type: "string" }
      },
      required: ["noteId"],
      additionalProperties: false
    },
    async invoke(rawInput = {}) {
      const noteId = requireStringField(rawInput.noteId, "noteId");
      const body = Object.fromEntries(
        Object.entries({
          title: optionalString(rawInput.title),
          content: optionalString(rawInput.content)
        }).filter(([, value]) => value !== undefined),
      );
      ensureAtLeastOneField(body, "at least one field must be provided to update");

      const response = await client.updateNote(noteId, body);
      return {
        updated: true,
        note: response?.data ?? null
      };
    }
  };
}
