import { apiClient } from './apiClient'
import { ProductSearchFilters, ProductSearchPage } from '../types/product'

export interface ImportReport {
    total_processed: number
    success_count: number
    failed_count: number
    errors: string[]
}

export const productService = {
    search: async (filters: ProductSearchFilters): Promise<ProductSearchPage> => {
        // Build query params
        const params = new URLSearchParams()

        // Required
        params.append('lat', filters.lat.toString())
        params.append('lng', filters.lng.toString())

        if (filters.search) params.append('search', filters.search)
        if (filters.min_price) params.append('min_price', filters.min_price.toString())
        if (filters.max_price) params.append('max_price', filters.max_price.toString())
        if (filters.max_distance) params.append('max_distance', filters.max_distance.toString())
        if (filters.expires_before) params.append('expires_before', filters.expires_before.toString())
        if (filters.page) params.append('page', filters.page.toString())
        if (filters.page_size) params.append('page_size', filters.page_size.toString())

        return apiClient.get<ProductSearchPage>(`/v1/products/search?${params.toString()}`)
    },

    importProducts: async (file: File): Promise<ImportReport> => {
        const formData = new FormData()
        formData.append('file', file)
        return apiClient.postMultiPart<ImportReport>('/v1/products/import', formData)
    }
}

