import apiClient from './client'
import type { FileUploadResponse } from '@/types/upload'

export const uploadsApi = {
  async upload(file: File, sessionId?: string): Promise<FileUploadResponse> {
    const formData = new FormData()
    formData.append('file', file)
    if (sessionId) formData.append('session_id', sessionId)
    const response = await apiClient.post<FileUploadResponse>('/uploads', formData, {
      headers: { 'Content-Type': 'multipart/form-data' },
    })
    return response.data
  },

  async getUrl(id: string): Promise<string> {
    const response = await apiClient.get<{ url: string }>(`/uploads/${id}/url`)
    return response.data.url
  },

  async list(sessionId: string): Promise<FileUploadResponse[]> {
    const response = await apiClient.get<FileUploadResponse[]>('/uploads', {
      params: { session_id: sessionId },
    })
    return response.data
  },

  async remove(id: string): Promise<void> {
    await apiClient.delete(`/uploads/${id}`)
  },
}

export default uploadsApi
