import { create } from 'zustand';

// Types
export interface Taller {
  id: string;
  name: string;
  legal_name: string;
  rfc: string;
  address: string;
  phone: string;
  email: string;
  logo_url?: string;
}

export interface WorkBay {
  id: string;
  name: string;
  bay_type: 'general' | 'diesel' | 'heavy_duty';
  status: 'available' | 'occupied' | 'maintenance';
  current_order_id?: string;
}

export interface TallerState {
  currentTaller: Taller | null;
  selectedBay: WorkBay | null;
  workBays: WorkBay[];
  isLoading: boolean;
  error: string | null;
}

interface TallerStore extends TallerState {
  setTaller: (taller: Taller) => void;
  setSelectedBay: (bay: WorkBay | null) => void;
  setWorkBays: (bays: WorkBay[]) => void;
  updateBayStatus: (bayId: string, status: WorkBay['status']) => void;
  setLoading: (loading: boolean) => void;
  setError: (error: string | null) => void;
  reset: () => void;
}

const initialState: TallerState = {
  currentTaller: null,
  selectedBay: null,
  workBays: [],
  isLoading: false,
  error: null,
};

export const useTallerStore = create<TallerStore>((set) => ({
  ...initialState,

  setTaller: (taller) => set({ currentTaller: taller }),

  setSelectedBay: (bay) => set({ selectedBay: bay }),

  setWorkBays: (bays) => set({ workBays: bays }),

  updateBayStatus: (bayId, status) =>
    set((state) => ({
      workBays: state.workBays.map((bay) =>
        bay.id === bayId ? { ...bay, status } : bay
      ),
    })),

  setLoading: (loading) => set({ isLoading: loading }),

  setError: (error) => set({ error }),

  reset: () => set(initialState),
}));
