import axios from '../utils/api';
import { ImageResolution, UploadedImage } from '../types/image';

export async function uploadImage(
  file: File,
  resolution: ImageResolution,
  onProgress?: (progress: number) => void
): Promise<UploadedImage> {
  // Get pre-signed URL for S3 upload
  const { data: { uploadUrl, imageId } } = await axios.post('/images/upload-url', {
    fileName: file.name,
    fileType: file.type,
    resolution: resolution.id
  });

  // Upload to S3
  await axios.put(uploadUrl, file, {
    headers: {
      'Content-Type': file.type
    },
    onUploadProgress: (progressEvent) => {
      if (progressEvent.total) {
        const progress = Math.round((progressEvent.loaded * 100) / progressEvent.total);
        onProgress?.(progress);
      }
    }
  });

  // Get the processed image details
  const { data: image } = await axios.get(`/images/${imageId}`);
  return image;
}