'use client';
import CvGenerator from '../../components/organisms/CvGenerator';
import { useSearchParams } from 'next/navigation';
import { useEffect, useState, Suspense } from 'react';

function ResumeContent() {
  const [isClient, setIsClient] = useState(false);
  const [decodedStudentId, setDecodedStudentId] = useState(null);
  const searchParams = useSearchParams();

  useEffect(() => {
    setIsClient(true);
    const token = searchParams.get('i') || searchParams.get('i');
    
    if (token) {
      try {
        // Decode the base64 encoded ID
        const decodedId = atob(token); // Decode base64
        
        // If you used additional encryption in PHP:
        // const decodedId = decryptToken(token);
        
        setDecodedStudentId(decodedId);
      } catch (error) {
        console.error('Error decoding token:', error);
        alert('Invalid resume link.');
        // Handle error - maybe show an error message to user
      }
    }
  }, [searchParams]);

  if (!isClient || decodedStudentId === null) {
    return <div>Loading...</div>;
  }

  return <CvGenerator studentId={decodedStudentId} />;
}

export default function ResumePage() {
  return (
    <Suspense fallback={<div>Loading...</div>}>
      <ResumeContent />
    </Suspense>
  );
}