import { create } from 'zustand';
import { prafrotService } from '../services/prafrotService';
import { toast } from 'sonner';
import { extractFriendlyMessage } from '../utils/errorMessages';

export const useDrivers = create((set, get) => ({
  drivers: [],
  loading: false,
  error: null,

  fetchDrivers: async () => {
    set({ loading: true, error: null });
    try {
      const data = await prafrotService.getDrivers();
      set({ drivers: Array.isArray(data) ? data : (data.data || []) });
    } catch (error) {
      const friendlyMsg = extractFriendlyMessage(error);
      set({ error: friendlyMsg });
      toast.error(friendlyMsg);
    } finally {
      set({ loading: false });
    }
  }
}));



