export interface ProductResponse {
  id: string;
  name: string;
  price: number;
  description: string;
  imageSrc: string;
}

export const fetchProduct = async (id: string): Promise<ProductResponse> => {
  try {
    const response = await fetch(`/api/products/${id}`);
    if (!response.ok) {
      throw new Error('Failed to fetch product');
    }
    return response.json();
  } catch {
    throw new Error('Failed to fetch product');
  }
};

export const fetchProducts = async (): Promise<ProductResponse[]> => {
  try {
    const response = await fetch('/api/products');
    if (!response.ok) {
      throw new Error('Failed to fetch products');
    }
    return response.json();
  } catch {
    throw new Error('Failed to fetch products');
  }
};

export const fetchProductsByCategory = async (category: string): Promise<ProductResponse[]> => {
  try {
    const response = await fetch(`/api/products?category=${category}`);
    if (!response.ok) {
      throw new Error('Failed to fetch products');
    }
    return response.json();
  } catch {
    throw new Error('Failed to fetch products');
  }
};
