import api from './api'; export interface Product { id: string; _id?: string; source: 'cima' | 'promofarma' | 'pharmarket' | 'docmorris' | '1001farma' | 'primor' | 'mifarma'; name: string; brand: string; category: string; subcategory?: string; image_url: string | null; source_url?: string; price?: number; original_price?: number; available?: boolean; rating?: number; review_count?: number; active_ingredient?: string; dosage?: string; form?: string; prescription?: string; commercialized?: boolean; photos?: { tipo: string; url: string }[]; docs?: { tipo: number; url: string }[]; } export interface ProductSearchResponse { results: Product[]; total: number; page?: number; pages?: number; sources?: { cima: number; }; } export async function searchProducts(query: string): Promise { if (!query || query.trim().length < 2) return []; try { const { data } = await api.get('/products/search', { params: { q: query } }); return data.results || []; } catch (error) { console.error('[Products] Search error:', error); return []; } } export async function searchParapharmacy(query: string, options?: { category?: string; brand?: string; page?: number; limit?: number; }): Promise { if (!query || query.trim().length < 2) { return { results: [], total: 0 }; } try { const params: Record = { q: query }; if (options?.category) params.category = options.category; if (options?.brand) params.brand = options.brand; if (options?.page) params.page = options.page; if (options?.limit) params.limit = options.limit; const { data } = await api.get('/products/parapharmacy/search', { params }); return data; } catch (error) { console.error('[Parapharmacy] Search error:', error); return { results: [], total: 0 }; } } export async function getParapharmacyProduct(id: string): Promise { try { const { data } = await api.get(`/products/parapharmacy/${id}`); return data; } catch (error) { console.error('[Parapharmacy] Detail error:', error); return null; } } export async function getParapharmacyCategories(): Promise { try { const { data } = await api.get('/products/parapharmacy/categories'); return data || []; } catch (error) { console.error('[Parapharmacy] Categories error:', error); return []; } } export async function getParapharmacyBrands(): Promise { try { const { data } = await api.get('/products/parapharmacy/brands'); return data || []; } catch (error) { console.error('[Parapharmacy] Brands error:', error); return []; } } export async function getProduct(source: string, id: string): Promise { try { // Use parapharmacy endpoint for all non-CIMA sources const isCima = source === 'cima'; const endpoint = isCima ? `/products/${source}/${id}` : `/products/parapharmacy/${id}`; const { data } = await api.get(endpoint); return data; } catch (error) { console.error('[Products] Detail error:', error); return null; } }