import { create } from 'zustand';

type UIState = {
  accountId: string | null;
  selectedThreadId: string | null;
  isComposeOpen: boolean;
  searchQuery: string;
  checkedThreadIds: Set<string>;

  setAccountId: (id: string) => void;
  selectThread: (id: string | null) => void;
  openCompose: () => void;
  closeCompose: () => void;
  setSearchQuery: (q: string) => void;
  toggleCheck: (id: string) => void;
  checkAll: (ids: string[]) => void;
  clearChecks: () => void;
};

export const useUI = create<UIState>((set) => ({
  accountId: null,
  selectedThreadId: null,
  isComposeOpen: false,
  searchQuery: '',
  checkedThreadIds: new Set(),

  setAccountId: (id) => set({ accountId: id }),
  selectThread: (id) => set({ selectedThreadId: id }),
  openCompose: () => set({ isComposeOpen: true }),
  closeCompose: () => set({ isComposeOpen: false }),
  setSearchQuery: (q) => set({ searchQuery: q }),
  toggleCheck: (id) =>
    set((s) => {
      const next = new Set(s.checkedThreadIds);
      next.has(id) ? next.delete(id) : next.add(id);
      return { checkedThreadIds: next };
    }),
  checkAll: (ids) => set({ checkedThreadIds: new Set(ids) }),
  clearChecks: () => set({ checkedThreadIds: new Set() }),
}));
