import React, { useState, useEffect } from "react";
import { Box, CircularProgress } from "@mui/material";
import { useNavigate, useSearchParams, Navigate } from "react-router-dom";
import Intro from "../components/Intro";
import BenchmarkGenerator from "../components/BenchmarkGenerator";

function BenchmarkGenerationPage() {
  const navigate = useNavigate();
  const [searchParams] = useSearchParams();
  const sessionId = searchParams.get("session");
  const [isValidSession, setIsValidSession] = useState(true);

  useEffect(() => {
    if (!sessionId) {
      setIsValidSession(false);
    }
  }, [sessionId]);

  const handleGenerationComplete = (result) => {
    console.log("Benchmark generation completed:", result);
    if (result && result.success) {
      navigate(`/benchmark-display?session=${sessionId}`);
    }
  };

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

  return (
    <>
      <Intro />
      <BenchmarkGenerator
        sessionId={sessionId}
        onComplete={handleGenerationComplete}
      />
    </>
  );
}

export default BenchmarkGenerationPage;
