import { create } from "zustand";

/* ── Product Type ────────────────────────────────────────────────── */
export interface Product {
  id: number;
  name: string;
  namebn: string;
  category: string;
  price: number;
  originalPrice: number;
  discount: number;
  colors: string[];
  sizes: string[];
  badge: string;
  image: string;
  hoverImage: string;
  rating: number;
  reviews: number;
  inStock: boolean;
}

/* ── Cart Item Type ─────────────────────────────────────────────── */
export interface CartItem extends Product {
  quantity: number;
  selectedSize: string;
  selectedColor: string;
}

/* ── Cart Store (Zustand) ────────────────────────────────────────── */
interface CartStore {
  items: CartItem[];
  isOpen: boolean;
  wishlist: Set<number>;

  // Cart actions
  addItem: (product: CartItem) => void;
  removeItem: (id: number) => void;
  updateQuantity: (id: number, delta: number) => void;
  clearCart: () => void;
  toggleCart: () => void;
  openCart: () => void;
  closeCart: () => void;

  // Wishlist actions
  toggleWishlist: (id: number) => void;
  isInWishlist: (id: number) => boolean;

  // Computed
  totalItems: () => number;
  subtotal: () => number;
}

export const useCartStore = create<CartStore>((set, get) => ({
  items: [],
  isOpen: false,
  wishlist: new Set<number>(),

  addItem: (product) =>
    set((state) => {
      const existing = state.items.find(
        (item) =>
          item.id === product.id &&
          item.selectedSize === product.selectedSize &&
          item.selectedColor === product.selectedColor
      );
      if (existing) {
        return {
          items: state.items.map((item) =>
            item.id === product.id &&
            item.selectedSize === product.selectedSize &&
            item.selectedColor === product.selectedColor
              ? { ...item, quantity: item.quantity + 1 }
              : item
          ),
        };
      }
      return { items: [...state.items, { ...product, quantity: 1 }] };
    }),

  removeItem: (id) =>
    set((state) => ({
      items: state.items.filter((item) => item.id !== id),
    })),

  updateQuantity: (id, delta) =>
    set((state) => {
      const item = state.items.find((i) => i.id === id);
      if (!item) return state;
      if (item.quantity + delta <= 0) {
        return { items: state.items.filter((i) => i.id !== id) };
      }
      return {
        items: state.items.map((i) =>
          i.id === id ? { ...i, quantity: i.quantity + delta } : i
        ),
      };
    }),

  clearCart: () => set({ items: [] }),
  toggleCart: () => set((state) => ({ isOpen: !state.isOpen })),
  openCart: () => set({ isOpen: true }),
  closeCart: () => set({ isOpen: false }),

  toggleWishlist: (id) =>
    set((state) => {
      const newWishlist = new Set(state.wishlist);
      if (newWishlist.has(id)) {
        newWishlist.delete(id);
      } else {
        newWishlist.add(id);
      }
      return { wishlist: newWishlist };
    }),

  isInWishlist: (id) => get().wishlist.has(id),

  totalItems: () => get().items.reduce((sum, item) => sum + item.quantity, 0),
  subtotal: () =>
    get().items.reduce((sum, item) => sum + item.price * item.quantity, 0),
}));
