import { create } from 'zustand';
import { QueryClient } from '@tanstack/react-query';

interface Tenant {
  id: string;
  name: string;
  plan: 'free' | 'pro' | 'enterprise';
}

interface TenantState {
  currentTenant: Tenant | null;
  availableTenants: Tenant[];
  setCurrentTenant: (tenant: Tenant) => void;
  setAvailableTenants: (tenants: Tenant[]) => void;
  switchTenant: (tenantId: string, queryClient: QueryClient) => void;
}

export const useTenantStore = create<TenantState>((set, get) => ({
  currentTenant: null,
  availableTenants: [],
  setCurrentTenant: (tenant) => set({ currentTenant: tenant }),
  setAvailableTenants: (tenants) => set({ availableTenants: tenants }),
  switchTenant: (tenantId, queryClient) => {
    const tenant = get().availableTenants.find((t) => t.id === tenantId);
    if (tenant) {
      set({ currentTenant: tenant });
      queryClient.removeQueries();
      queryClient.invalidateQueries();
    }
  },
}));
