import { apiPostForm } from '@/lib/http'
import type { UploadedFile } from '@/types/worker-platform'
import { optimizeImageForUpload } from '@/utils/image-upload'

type UploadScope = 'admin' | 'worker' | 'collect'

export async function uploadFile(file: File, scene: string, scope: UploadScope) {
  const uploadTarget = await optimizeImageForUpload(file, scene)
  const form = new FormData()
  form.append('file', uploadTarget)
  form.append('scene', scene)
  const endpoint =
    scope === 'admin'
      ? '/api/v1/admin/files/upload'
      : scope === 'collect'
        ? '/api/v1/collect/upload'
        : '/api/v1/worker/files/upload'
  const response = await apiPostForm<{ file: UploadedFile }>(endpoint, form)
  return response.data.file
}
