// app/faculty/[id]/page.tsx
import Header from "../../../components/Layouts/Header";
import { Footer } from "../../../components/Layouts/Footer";
import TeamMemberDetail from "../../../components/faculty/TeamMemberDetail";
import { notFound } from 'next/navigation';
import { FacultyService } from '../../../lib/facultyData';
import type { Metadata } from 'next';

export default async function FacultyPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const memberId = parseInt(id);

  if (isNaN(memberId)) {
    notFound();
  }

  const memberData = await FacultyService.getFacultyById(memberId);

  if (!memberData) {
    notFound();
  }

  return (
    <>
      <Header />
      <TeamMemberDetail memberId={memberId} memberData={memberData} />
      <Footer />
    </>
  );
}

// Generate static params
export async function generateStaticParams() {
  try {
    const teamMembers = await FacultyService.getAllFaculty();

    return teamMembers.map((member) => ({
      id: member.id.toString(),
    }));
  } catch (error) {
    console.error('Error generating static params:', error);
    return [];
  }
}

// Generate metadata
export async function generateMetadata({
  params,
}: {
  params: Promise<{ id: string }>;
}): Promise<Metadata> {
  const { id } = await params;
  const memberId = parseInt(id);

  if (isNaN(memberId)) {
    return { title: 'Faculty Member Not Found - CMC Vellore' };
  }

  try {
    const memberData = await FacultyService.getFacultyById(memberId);

    if (!memberData) {
      return { title: 'Faculty Member Not Found - CMC Vellore' };
    }

    return {
      title: `${memberData.name} - ${memberData.designation} | CMC Vellore`,
      description: memberData.description,
      openGraph: {
        title: `${memberData.name} - ${memberData.designation}`,
        description: memberData.description,
        images: [memberData.image],
      },
    };
  } catch (error) {
    console.error('Error generating metadata:', error);
    return { title: 'Faculty Member - CMC Vellore' };
  }
}