import { useAuthStore } from '@/store/authStore';
import { useSwitchTenant } from '@/api/hooks';

export function useTenant() {
  const { user, currentTenant, setTenant } = useAuthStore();
  const switchTenantMutation = useSwitchTenant();

  const availableTenants = user?.tenants ?? [];

  const switchTenant = async (tenantId: string) => {
    try {
      await switchTenantMutation.mutateAsync(tenantId);
    } catch (error) {
      console.error('Failed to switch tenant:', error);
    }
  };

  return {
    currentTenant,
    availableTenants,
    switchTenant,
    isSwitching: switchTenantMutation.isPending,
  };
}
