// Data-import INFRASTRUCTURE — the only layer that touches @ump/shared/apiClient. Implements the
// DirectUploadGateway port by CONVERGING the spec's Direct Upload onto the existing content-addressed
// dataset upload (be0 `POST /api/v1/datasets/{id}/files` via shared `uploadDatasetFiles`). A second
// pipeline would split-brain the single dataset-file list; see the design notes.

import { uploadDatasetFiles } from '@ump/shared';
import type { DirectUploadGateway } from '../application/ports';

export const httpDirectUploadGateway: DirectUploadGateway = {
  async upload({ datasetId, files, onProgress }) {
    // The shared apiClient doesn't thread axios onUploadProgress yet, so report coarse begin/end.
    onProgress?.(5);
    const res = await uploadDatasetFiles(datasetId, files);
    onProgress?.(100);
    const dedupedCount = res.files.filter((f) => f.deduped).length;
    return {
      uploadedCount: res.files.length,
      dedupedCount,
      paths: res.files.map((f) => f.path),
    };
  },
};
