import { create } from 'zustand';
import { persist } from 'zustand/middleware';
import { useUiPreferencesStore } from './useUiPreferencesStore';

type State = {
  selectedOrgId: string | null;
  setSelectedOrgId: (orgId: string | null) => void;
  clearSelectedOrgId: () => void;
};

export const useOrganizationStore = create<State>()(
  persist(
    (set) => ({
      selectedOrgId: null,
      setSelectedOrgId: (orgId) => set({ selectedOrgId: orgId }),
      clearSelectedOrgId: () => set({ selectedOrgId: null }),
    }),
    {
      name: 'organization-selection',
      partialize: (state) => ({ selectedOrgId: state.selectedOrgId }),
    }
  )
);

// Sync org store changes into the UI preferences store for server persistence
useOrganizationStore.subscribe((state) => {
  useUiPreferencesStore.getState().setSelectedOrgId(state.selectedOrgId);
});

export const useSelectedOrgId = () =>
  useOrganizationStore((s) => s.selectedOrgId);
export const useSetSelectedOrgId = () =>
  useOrganizationStore((s) => s.setSelectedOrgId);
