// services/fileUploadService.ts
export interface FileUploadResponse {
  url: string;
  filename: string;
}

class FileUploadService {
  private apiBaseUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:8080';

  async uploadFile(file: File): Promise<FileUploadResponse> {
    const formData = new FormData();
    formData.append('file', file);
    
    try {
      const response = await fetch(`${this.apiBaseUrl}/api/files/upload`, {
        method: 'POST',
        body: formData,
      });
      
      if (!response.ok) {
        const errorData = await response.json().catch(() => ({}));
        throw new Error(errorData.error || `HTTP error! status: ${response.status}`);
      }
      
      const result: FileUploadResponse = await response.json();
      return result;
    } catch (error) {
      console.error('File upload error:', error);
      throw error;
    }
  }
}

export const fileUploadService = new FileUploadService();