export interface CartItem {
  id: string;          // unique key: productId-color-size-length
  productId: string;
  slug: string;
  title: string;
  thumbnail: string;
  color: string;
  size: string;
  length: string;
  price: number;
  quantity: number;
}

const CART_KEY = "ag_cart";

export function getCart(): CartItem[] {
  if (typeof window === "undefined") return [];
  try {
    return JSON.parse(localStorage.getItem(CART_KEY) || "[]");
  } catch {
    return [];
  }
}

function saveCart(items: CartItem[]) {
  localStorage.setItem(CART_KEY, JSON.stringify(items));
  if (typeof window !== "undefined") {
    window.dispatchEvent(new Event("cart-updated"));
  }
}

export function addToCart(item: Omit<CartItem, "id" | "quantity">, quantity = 1): CartItem[] {
  const cart = getCart();
  const id = `${item.productId}-${item.color}-${item.size}-${item.length}`;
  const existing = cart.find((c) => c.id === id);
  if (existing) {
    existing.quantity += quantity;
  } else {
    cart.push({ ...item, id, quantity });
  }
  saveCart(cart);
  return cart;
}

export function removeFromCart(id: string): CartItem[] {
  const cart = getCart().filter((c) => c.id !== id);
  saveCart(cart);
  return cart;
}

export function updateQty(id: string, quantity: number): CartItem[] {
  const cart = getCart().map((c) => (c.id === id ? { ...c, quantity: Math.max(1, quantity) } : c));
  saveCart(cart);
  return cart;
}

export function clearCart(): CartItem[] {
  saveCart([]);
  return [];
}

export function cartTotal(items: CartItem[]): number {
  return items.reduce((sum, i) => sum + i.price * i.quantity, 0);
}

export function cartCount(items: CartItem[]): number {
  return items.reduce((sum, i) => sum + i.quantity, 0);
}
