import { clientFetch } from '@/lib/api/api-client';

// Product data types
export interface Product {
  _id: string;
  name: string;
  description: string;
  price: number;
  imageUrl?: string;
  category?: string;
  stock?: number;
  status: 'active' | 'inactive';
}

export interface ProductsResponse {
  products: Product[];
  total: number;
  success?: boolean;
}

export interface StockData {
  currentStock: number;
  stockTracking?: boolean;
  lowStockThreshold?: number;
}

/**
 * Get all products with pagination
 */
export const getProducts = async (page: number = 1, limit: number = 25): Promise<ProductsResponse> => {
  return clientFetch(`/products?page=${page}&limit=${limit}`);
};

/**
 * Get a specific product by ID
 */
export const getProductDetails = async (productId: string): Promise<Product> => {
  return clientFetch(`/products/${productId}`);
};

/**
 * Create a new product
 */
export const createProduct = async (productData: Omit<Product, '_id'>): Promise<Product> => {
  return clientFetch('/products', {
    method: 'POST',
    body: JSON.stringify(productData),
  });
};

/**
 * Update a product
 */
export const updateProduct = async (productId: string, productData: Partial<Product>): Promise<Product> => {
  return clientFetch(`/products/${productId}`, {
    method: 'PUT',
    body: JSON.stringify(productData),
  });
};

/**
 * Delete a product
 */
export const deleteProduct = async (productId: string): Promise<{ success: boolean }> => {
  return clientFetch(`/products/${productId}`, {
    method: 'DELETE',
  });
};

/**
 * Upload a product image
 */
export const uploadProductImage = async (productId: string, file: File): Promise<{ imageUrl: string }> => {
  const formData = new FormData();
  formData.append('file', file);

  return clientFetch(`/products/${productId}/image`, {
    method: 'PUT',
    body: formData,
    headers: {
      // Don't set Content-Type when sending FormData, the browser will set it with the boundary
    }
  });
};

/**
 * Get product stock information
 */
export const getProductStock = async (productId: string): Promise<StockData> => {
  return clientFetch(`/stock/${productId}`);
};

/**
 * Update product stock
 */
export const updateProductStock = async (productId: string, stockData: StockData): Promise<StockData> => {
  return clientFetch(`/stock/${productId}`, {
    method: 'PUT',
    body: JSON.stringify(stockData),
  });
}; 