import { delay } from './api';
import { mockProducts } from '@/data/mockProducts';
import type { Product } from '@/types';

let products = [...mockProducts];

export const productsService = {
  // GET /api/products
  list: (storeId: string) =>
    delay(products.filter((p) => p.storeId === storeId)),

  // GET /api/products/:id
  getById: (storeId: string, id: string) =>
    delay(products.find((p) => p.storeId === storeId && p.id === id) ?? null),

  // GET /api/stores/:slug/products/:productSlug
  getBySlug: (storeId: string, slug: string) =>
    delay(products.find((p) => p.storeId === storeId && p.slug === slug) ?? null),

  // POST /api/products
  create: (product: Product) => {
    products = [product, ...products];
    return delay(product);
  },

  // PUT /api/products/:id
  update: (id: string, patch: Partial<Product>) => {
    products = products.map((p) => (p.id === id ? { ...p, ...patch } : p));
    return delay(products.find((p) => p.id === id) ?? null);
  },

  // DELETE /api/products/:id
  remove: (id: string) => {
    products = products.filter((p) => p.id !== id);
    return delay({ success: true });
  },
};
