import { useAuth } from '../lib/auth';
import { mindSpaceLoginUrl } from '../lib/site';

export function RequireAdmin({ children }: { children: React.ReactNode }) {
  const { loading, user } = useAuth();

  if (loading) return <p>检查权限…</p>;

  if (!user) {
    return (
      <div className="card">
        <h2>需要登录</h2>
        <p>请先在 MindSpace 登录后再访问管理后台。</p>
        <a className="btn" href={mindSpaceLoginUrl()}>
          前往登录
        </a>
      </div>
    );
  }

  if (user.role !== 'admin') {
    return (
      <div className="card">
        <h2>权限不足</h2>
        <p>超级管理后台需要 role = admin，当前账号 ({user.username}) 无此权限。</p>
      </div>
    );
  }

  return children;
}
