import { notFound } from 'next/navigation';

// async function getTrainee(id) {
//   try {
//     const res = await fetch(`http://localhost:8055/items/trainees/${id}`);
//     const json = await res.json();
//     return json.data;
//   } catch (error) {
//     console.error('Error fetching trainee:', error);
//     return null;
//   }
// }


async function getTrainee(id) {
  try {
    const res = await fetch(`https://dtsnew.adhcloud.space/items/trainees/${id}`, {
      headers: {
        Authorization: 'Bearer G_2h5o1NlX1_PfomkUe5zk7Xx0dT2juV',
      },
    });

// if we are using directus API 
// const res = await fetch(`http://localhost:8055/items/trainees/${id}`);


    const json = await res.json();
    return json.data;
  } catch (error) {
    console.error('Error fetching trainee:', error);
    return null;
  }
}





// ✅ Destructure 'params' at the top level
export default async function TraineeDetailPage({ params: { id } }) {
  const trainee = await getTrainee(id);

  if (!trainee) return notFound();

  return (
    <main className="p-6">
      <h1 className="text-2xl font-bold mb-4">{trainee.Name}</h1>
      <ul className="space-y-1">
        <li><strong>Feedback:</strong> {trainee.overall_feedback}</li>
        <li><strong>Attendance:</strong> {trainee.overall_attendance}</li>
        <li><strong>Batch:</strong> {trainee.batch_name}</li>
        <li><strong>Speed:</strong> {trainee.speed}</li>
        <li><strong>Accuracy:</strong> {trainee.accuracy}</li>
        <li><strong>Precision:</strong> {trainee.precision}</li>
        <li><strong>DOJ:</strong> {trainee.doj}</li>
      </ul>
    </main>
  );
}
