import { create } from 'zustand';

interface AdminInfo {
  id: number;
  username: string;
  name: string;
  role: string;
  phone?: string;
}

interface AuthState {
  token: string | null;
  userInfo: AdminInfo | null;
  setAuth: (token: string, userInfo: AdminInfo) => void;
  logout: () => void;
}

export const useAuthStore = create<AuthState>((set) => ({
  token: localStorage.getItem('admin_token'),
  userInfo: JSON.parse(localStorage.getItem('admin_userInfo') || 'null'),
  setAuth: (token, userInfo) => {
    localStorage.setItem('admin_token', token);
    localStorage.setItem('admin_userInfo', JSON.stringify(userInfo));
    set({ token, userInfo });
  },
  logout: () => {
    localStorage.removeItem('admin_token');
    localStorage.removeItem('admin_userInfo');
    set({ token: null, userInfo: null });
    window.location.href = '/login';
  },
}));
