import apiClient from './authApi';

export type DocumentCategory = 'SUPPORTING' | 'APPROVAL' | 'REFERENCE' | 'FINAL' | 'OTHER';

export interface UploadResponse {
  documentId: string;
  storageUrl?: string;
  fileName: string;
  originalFileName: string;
}

export async function uploadDocument(
  file: File,
  requestId: string,
  category: DocumentCategory = 'SUPPORTING'
): Promise<UploadResponse> {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('requestId', requestId);
  formData.append('category', category);

  const res = await apiClient.post('/documents', formData, {
    headers: { 'Content-Type': 'multipart/form-data' },
  });
  const data = res.data?.data || res.data;
  return {
    documentId: data?.documentId || data?.document_id || '',
    storageUrl: data?.storageUrl || data?.storage_url,
    fileName: data?.fileName || data?.file_name || file.name,
    originalFileName: data?.originalFileName || data?.original_file_name || file.name,
  };
}

export async function uploadMany(
  files: File[],
  requestId: string,
  category: DocumentCategory = 'SUPPORTING'
): Promise<UploadResponse[]> {
  const tasks = files.map(f => uploadDocument(f, requestId, category));
  return Promise.all(tasks);
}

export default { uploadDocument, uploadMany };


