/**
 * Route guard component.
 *
 * Protects all routes by ensuring the initial setup wizard has been
 * completed.  If setup is not done yet, the user is redirected to `/setup`.
 * While the status is loading a full-screen spinner is shown.
 */

import { Navigate } from "react-router-dom";
import { Spinner } from "@fluentui/react-components";
import { useSetup } from "../hooks/useSetup";

/**
 * Component is intentionally simple; status load is handled by the hook.
 */

interface SetupGuardProps {
  /** The protected content to render when setup is complete. */
  children: React.JSX.Element;
}

/**
 * Render `children` only when setup has been completed.
 *
 * Redirects to `/setup` if setup is still pending.
 */
export function SetupGuard({ children }: SetupGuardProps): React.JSX.Element {
  const { status, loading } = useSetup();

  if (loading) {
    return (
      <div
        style={{
          display: "flex",
          justifyContent: "center",
          alignItems: "center",
          minHeight: "100vh",
        }}
      >
        <Spinner size="large" label="Loading…" />
      </div>
    );
  }

  if (!status?.completed) {
    return <Navigate to="/setup" replace />;
  }

  return children;
}
