// import { defineStore } from 'pinia'
// import { ref, watch } from 'vue'

// export interface CartItem {
//   id: number | string
//   name: string,
//   variation_id: number | string
//   product_id: number | string
//   price: number | string
//   quantity: number
//   stock: number
//   image?: string
//   [key: string]: any // Optional extra properties
// }

// export const useCartStore = defineStore('cart', () => {
    
//   const cartItems = ref<CartItem[]>([])

//   // Load from localStorage
//   if (process.client) {
//     const storedCart = localStorage.getItem('cart')
//     if (storedCart) {
//       try {
//         cartItems.value = JSON.parse(storedCart)
//       } catch (e) {
//         console.warn('Invalid cart data in localStorage')
//       }
//     }
//   }

//   // Sync to localStorage on change
//   watch(
//     cartItems,
//     (newCart) => {
//       localStorage.setItem('cart', JSON.stringify(newCart))
//     },
//     { deep: true }
//   )

//   function addToCart(product: CartItem) {
//     const existing = cartItems.value.find(item => item.id === product.id)
//     if (existing) {
//       existing.quantity += 1
//     } else {
//       cartItems.value.push({ ...product})
//     }
//   }


//   function removeFromCart(productId: number | string) {
//     cartItems.value = cartItems.value.filter(item => item.id !== productId)
//   }

//   function updateQuantity(productId: number | string, quantity: number) {
//     const item = cartItems.value.find(item => item.id === productId)
//     if (item && quantity > 0) {
//       item.quantity = quantity
//     }
//   }

//   function clearCart() {
//     cartItems.value = []
//   }

//   function totalItems(): number {
//     return cartItems.value.reduce((sum, item) => sum + item.quantity, 0)
//   }

//   function totalPrice(): number {
//     return cartItems.value.reduce((sum, item) => sum + item.quantity * item.price, 0)
//   }



//   function updateItem(updatedItem: any) {

//     const item = cartItems.value.find(i =>
//       i.id === updatedItem.id &&
//       i.variation_id === updatedItem.variation_id &&
//       i.product_id === updatedItem.product_id
//     )

//     if (item) {
//       item.quantity = updatedItem.quantity
//       item.price = updatedItem.price
//     }
//   }

//   return {
//     cartItems,
//     addToCart,
//     removeFromCart,
//     updateQuantity,
//     clearCart,
//     totalItems,
//     totalPrice,
//     updateItem,
//   }
// })