// Zustand-like store (vanilla implementation for simplicity)
const createStore = (initialState) => {
  let state = { ...initialState };
  const listeners = new Set();

  const getState = () => state;

  const setState = (partial) => {
    const nextState = typeof partial === 'function' ? partial(state) : partial;
    state = { ...state, ...nextState };
    listeners.forEach(listener => listener(state));
  };

  const subscribe = (listener) => {
    listeners.add(listener);
    return () => listeners.delete(listener);
  };

  return { getState, setState, subscribe };
};

// Create the store
const store = createStore({
  route: '/',
  team: null,
  theme: 'dark',
  beingMode: 'idle',
  metrics: {
    ui: { components: 0, tokens: 0, synced: false, stories: 0 },
    ux: { consistency: 0, violations: 0, patterns: 0, tokens: 0 },
    qa: { health: 0, quickWins: 0, unused: 0, coverage: 0 }
  }
});

// Helper functions
export const useStore = {
  getState: store.getState,
  subscribe: store.subscribe,

  setRoute: (route) => store.setState({ route }),
  setTeam: (team) => store.setState({ team }),
  setTheme: (theme) => store.setState({ theme }),
  setBeingMode: (beingMode) => store.setState({ beingMode }),
  setMetrics: (team, metrics) => {
    const currentMetrics = store.getState().metrics;
    store.setState({
      metrics: {
        ...currentMetrics,
        [team]: { ...currentMetrics[team], ...metrics }
      }
    });
  }
};

export default store;
