import type { Category, Product } from '@/entities/product/model/types'
import { apiClient } from '@/shared/api/client'

export type PublicProductsResponse = {
  items: Product[]
  total: number
  page: number
  pageSize: number
}

export async function fetchPublicProducts(params?: {
  categorySlug?: string
  q?: string
  sort?: 'price_asc' | 'price_desc' | ''
  page?: number
  pageSize?: number
  priceMinCents?: number
  priceMaxCents?: number
}): Promise<PublicProductsResponse> {
  const { data } = await apiClient.get<PublicProductsResponse>('products', {
    params: {
      categorySlug: params?.categorySlug || undefined,
      q: params?.q || undefined,
      sort: params?.sort || undefined,
      page: params?.page || undefined,
      pageSize: params?.pageSize || undefined,
      priceMin: params?.priceMinCents ?? undefined,
      priceMax: params?.priceMaxCents ?? undefined,
    },
  })
  return data
}

export async function fetchPublicProduct(id: string): Promise<Product> {
  const { data } = await apiClient.get<Product>(`products/${id}`)
  return data
}

export async function fetchCategories(): Promise<Category[]> {
  const { data } = await apiClient.get<Category[]>('categories')
  return data
}
