import { create } from "zustand";
import { ExpandedEvent } from "@calchat/shared";

interface EventsState {
  events: ExpandedEvent[];
  setEvents: (events: ExpandedEvent[]) => void;
  addEvent: (event: ExpandedEvent) => void;
  updateEvent: (id: string, event: Partial<ExpandedEvent>) => void;
  deleteEvent: (id: string) => void;
}

export const useEventsStore = create<EventsState>((set) => ({
  events: [],
  setEvents: (events: ExpandedEvent[]) => {
    set({ events });
  },
  addEvent: (event: ExpandedEvent) => {
    set((state) => ({ events: [...state.events, event] }));
  },
  updateEvent: (id: string, updates: Partial<ExpandedEvent>) => {
    set((state) => ({
      events: state.events.map((e) => (e.id === id ? { ...e, ...updates } : e)),
    }));
  },
  deleteEvent: (id: string) => {
    set((state) => ({
      events: state.events.filter((e) => e.id !== id),
    }));
  },
}));
