"use client";

import { useNavStore } from "@/stores/navStore";
import { useRouter, usePathname } from "next/navigation";
import { useEffect } from "react";

export function ExamGuard({ children }: { children: React.ReactNode }) {
  const { examSubmitted, resultsDone } = useNavStore();
  const router = useRouter();
  const pathname = usePathname();

  useEffect(() => {
    // Prevent access to /exam after submission
    if (pathname === "/exam/exam-screen" && examSubmitted) {
      router.replace("/results");
    }

    // Prevent access to /results after done
    if (pathname === "/exam/results" && resultsDone) {
      router.replace("/categories"); // or wherever you want them to go
    }
  }, [pathname, examSubmitted, resultsDone, router]);

  return <>{children}</>;
}
