async function createKanbanTask(adapter, input) {
  const existingTasks = await adapter.findMany({
    model: "kanbanTask",
    where: [
      {
        field: "columnId",
        value: input.columnId,
        operator: "eq"
      }
    ]
  });
  const nextOrder = existingTasks.length > 0 ? Math.max(...existingTasks.map((t) => t.order)) + 1 : 0;
  return adapter.create({
    model: "kanbanTask",
    data: {
      title: input.title,
      columnId: input.columnId,
      description: input.description,
      priority: input.priority ?? "MEDIUM",
      order: nextOrder,
      assigneeId: input.assigneeId,
      isArchived: false,
      createdAt: /* @__PURE__ */ new Date(),
      updatedAt: /* @__PURE__ */ new Date()
    }
  });
}
const _pendingBoardCreations = /* @__PURE__ */ new Map();
async function findOrCreateKanbanBoard(adapter, slug, name, columnTitles) {
  const existing = await adapter.findOne({
    model: "kanbanBoard",
    where: [{ field: "slug", value: slug, operator: "eq" }]
  });
  if (existing) return existing;
  const inflight = _pendingBoardCreations.get(slug);
  if (inflight) return inflight;
  const creation = (async () => {
    try {
      const board = await adapter.create({
        model: "kanbanBoard",
        data: {
          name,
          slug,
          createdAt: /* @__PURE__ */ new Date(),
          updatedAt: /* @__PURE__ */ new Date()
        }
      });
      await Promise.all(
        columnTitles.map(
          (title, index) => adapter.create({
            model: "kanbanColumn",
            data: {
              title,
              boardId: board.id,
              order: index,
              createdAt: /* @__PURE__ */ new Date(),
              updatedAt: /* @__PURE__ */ new Date()
            }
          })
        )
      );
      return board;
    } catch (err) {
      const winner = await adapter.findOne({
        model: "kanbanBoard",
        where: [{ field: "slug", value: slug, operator: "eq" }]
      });
      if (winner) return winner;
      throw err;
    }
  })();
  _pendingBoardCreations.set(slug, creation);
  try {
    return await creation;
  } finally {
    _pendingBoardCreations.delete(slug);
  }
}
async function getKanbanColumnsByBoardId(adapter, boardId) {
  return adapter.findMany({
    model: "kanbanColumn",
    where: [{ field: "boardId", value: boardId, operator: "eq" }],
    sortBy: { field: "order", direction: "asc" }
  });
}

export { createKanbanTask, findOrCreateKanbanBoard, getKanbanColumnsByBoardId };
