import { create } from 'zustand';
import { Vehicle } from '../../features/vehicles/types/vehicles.types';

interface AppState {
  // UI state
  sidebarOpen: boolean;
  sidebarCollapsed: boolean;
  selectedVehicle: Vehicle | null;

  // Actions
  toggleSidebar: () => void;
  setSidebarOpen: (open: boolean) => void;
  toggleSidebarCollapse: () => void;
  setSelectedVehicle: (vehicle: Vehicle | null) => void;
}

const savedCollapsed = localStorage.getItem('sidebarCollapsed') === 'true';

export const useAppStore = create<AppState>((set) => ({
  // Initial state
  sidebarOpen: false,
  sidebarCollapsed: savedCollapsed,
  selectedVehicle: null,

  // Actions
  toggleSidebar: () => set((state) => ({ sidebarOpen: !state.sidebarOpen })),
  setSidebarOpen: (open: boolean) => set({ sidebarOpen: open }),
  toggleSidebarCollapse: () => set((state) => {
    const next = !state.sidebarCollapsed;
    localStorage.setItem('sidebarCollapsed', String(next));
    return { sidebarCollapsed: next };
  }),
  setSelectedVehicle: (vehicle: Vehicle | null) => set({ selectedVehicle: vehicle }),
}));