import { create } from "zustand";

export interface CartSeat {
  seatId: number;
  ticketId: number;
  sector: string;
  row: number;
  number: number;
  price: number;
}

interface CartState {
  seats: CartSeat[];
  addSeat: (seat: CartSeat) => void;
  removeSeat: (seatId: number) => void;
  clearCart: () => void;
  totalPrice: () => number;
}

export const useCartStore = create<CartState>()((set, get) => ({
  seats: [],
  addSeat: (seat) =>
    set((state) => ({
      seats: state.seats.some((s) => s.seatId === seat.seatId)
        ? state.seats
        : [...state.seats, seat],
    })),
  removeSeat: (seatId) =>
    set((state) => ({ seats: state.seats.filter((s) => s.seatId !== seatId) })),
  clearCart: () => set({ seats: [] }),
  totalPrice: () => get().seats.reduce((sum, s) => sum + s.price, 0),
}));
