/**
 * Shared utilities for synchronous bulk endpoints.
 * See `/api/v1/{entity}/bulk` route handlers for usage.
 */

export interface BulkRowResult {
  id: string;
  ok: boolean;
  error?: string;
}

export interface BulkSummary {
  total: number;
  succeeded: number;
  failed: number;
}

export async function runBulk(
  ids: string[],
  perRow: (id: string) => Promise<void>,
): Promise<{ results: BulkRowResult[]; summary: BulkSummary }> {
  const results: BulkRowResult[] = [];
  for (const id of ids) {
    try {
      await perRow(id);
      results.push({ id, ok: true });
    } catch (err) {
      results.push({
        id,
        ok: false,
        error: err instanceof Error ? err.message : 'unknown error',
      });
    }
  }
  return {
    results,
    summary: {
      total: results.length,
      succeeded: results.filter((r) => r.ok).length,
      failed: results.filter((r) => !r.ok).length,
    },
  };
}
