import { ReactNode } from "react";
import { Navigate } from "react-router-dom";
import { useAuthStore } from "../../store/authStore";

interface AdminGuardProps {
  children: ReactNode;
}

/**
 * AdminGuard - restricts access to system account (aws-admin/default) or developer
 */
export default function AdminGuard({ children }: AdminGuardProps) {
  const { user } = useAuthStore();
  const role = user?.role;
  const accountSlug = user?.account?.slug;
  const isSystemAccount = accountSlug === 'aws-admin' || accountSlug === 'default-account' || accountSlug === 'default';
  const allowed = role === 'developer' || isSystemAccount;

  if (!allowed) {
    return <Navigate to="/" replace />;
  }

  return <>{children}</>;
}

