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

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())

        const response = await apiClient.get(`/v1/products/search?${params.toString()}`)
        return response.data
    }
}
