import type { ReactNode } from 'react';
import { Navigate } from 'react-router-dom';

import { useAuth } from '@/lib/auth';

/** Server still arbitrates — this is a UI gate so non-admins don't see admin routes. */
export function RequireAdmin({ children }: { children: ReactNode }) {
  const { state } = useAuth();
  if (state.loading) {
    return <p className="font-mono text-xs text-text-dim p-8">Loading session…</p>;
  }
  if (!state.user) {
    return <Navigate to="/login" replace />;
  }
  if (!state.user.is_admin) {
    return <Navigate to="/" replace />;
  }
  return <>{children}</>;
}
