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

export function createCreateBoardGroupTool(client, options = {}) {
  const defaultWorkspaceSlug = options.defaultWorkspaceSlug || process.env.PRODUCTIER_WORKSPACE_SLUG_DEFAULT || "";
  const defaultColor = options.defaultBoardGroupColor || process.env.PRODUCTIER_BOARD_GROUP_COLOR_DEFAULT || "slate";

  return {
    name: "productier_create_board_group",
    description: "Create a board group in a Productier workspace.",
    inputSchema: {
      type: "object",
      properties: {
        workspaceSlug: { type: "string", description: "Workspace slug. Defaults to PRODUCTIER_WORKSPACE_SLUG_DEFAULT." },
        name: { type: "string" },
        color: { type: "string", description: "Color token. Defaults to PRODUCTIER_BOARD_GROUP_COLOR_DEFAULT or slate." }
      },
      required: ["name"],
      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 name = requireStringField(rawInput.name, "name");
      const color = rawInput.color ? String(rawInput.color) : defaultColor;
      const response = await client.createBoardGroup({
        workspaceSlug,
        name,
        color
      });

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