import { create } from 'zustand';

interface SellerInfo {
  id: number;
  phone: string;
  contactName: string;
  email?: string;
  status: string;
  merchantId?: number;
  merchantStatus?: string;
}

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

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