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

export function createCreateNoteTool(client, options = {}) {
  const defaultWorkspaceSlug = options.defaultWorkspaceSlug || process.env.PRODUCTIER_WORKSPACE_SLUG_DEFAULT || "";

  return {
    name: "productier_create_note",
    description: "Create a note in a Productier workspace.",
    inputSchema: {
      type: "object",
      properties: {
        workspaceSlug: { type: "string", description: "Workspace slug. Defaults to PRODUCTIER_WORKSPACE_SLUG_DEFAULT." },
        title: { type: "string" },
        content: { type: "string" }
      },
      required: ["title", "content"],
      additionalProperties: false
    },
    async invoke(rawInput = {}) {
      const workspaceSlug = resolveWorkspaceSlug(rawInput.workspaceSlug, defaultWorkspaceSlug);
      if (!workspaceSlug) {
        throw new Error("workspaceSlug is required (or set PRODUCTIER_WORKSPACE_SLUG_DEFAULT)");
      }

      const title = requireStringField(rawInput.title, "title");
      const content = requireStringField(rawInput.content, "content");
      const response = await client.createNote({
        workspaceSlug,
        title,
        content
      });

      return {
        created: true,
        note: response?.data ?? null
      };
    }
  };
}
