import { useQuery } from '@tanstack/react-query';

export interface SetupStatus {
  /** True when the runtime has no usable LLM worker → show the wizard. */
  needsSetup: boolean;
  /** True when an OAuth provider or local auth is active. */
  authActive: boolean;
  /** Resolved listen port (for the restart hint). */
  port: number;
  /** How the server was started, for tailored restart instructions. */
  deployHint: 'docker' | 'source';
  /** True when a setup token must be supplied with mutating setup calls. */
  tokenRequired: boolean;
}

/**
 * Polls GET /api/setup/status. Short staleTime so the wizard disappears
 * promptly once the LLM is configured (the server recomputes needsSetup per
 * request). Never throws into the gate — on error we treat it as "no wizard".
 */
export function useSetupState() {
  return useQuery<SetupStatus>({
    queryKey: ['setup', 'status'],
    queryFn: async () => {
      const res = await fetch('/api/setup/status');
      if (!res.ok) throw new Error(`setup status ${res.status}`);
      return res.json();
    },
    retry: false,
    staleTime: 10_000,
  });
}
