"use client";

import { useEffect, type ReactNode } from "react";
import { useRouter } from "next/navigation";
import { useAuthContext } from "@/context/auth-context";

export default function DashboardLayout({ children }: { children: ReactNode }) {
  const { token, isLoaded } = useAuthContext();
  const router = useRouter();

  useEffect(() => {
    if (isLoaded && !token) {
      router.push("/login");
    }
  }, [isLoaded, token, router]);

  if (!isLoaded) {
    return (
      <div className="flex flex-1 items-center justify-center">
        <div className="h-5 w-5 animate-spin rounded-full border-2 border-zinc-700 border-t-zinc-300" />
      </div>
    );
  }

  if (!token) return null;

  return <>{children}</>;
}
