// src/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";

// Force dynamic rendering (OK to keep)
export const dynamic = "force-dynamic";
export const dynamicParams = true;

type PageProps = {
  params: Promise<{        // ← Now a Promise in Next.js 15
    id: string;
  }>;
};

export default async function FacultyPage({ params }: PageProps) {
  const { id } = await params;

  if (!id) {
    notFound();
  }

  let memberData;

  try {
    memberData = await FacultyService.getFacultyById(id); // pass string, not Number
  } catch (err) {
    console.error("Error loading faculty member:", err);
    notFound();
  }

  if (!memberData) {
    notFound();
  }

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

// --------------------
// Metadata generation
// --------------------
export async function generateMetadata({ params }: PageProps): Promise<Metadata> {
  const { id } = await params;

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

  try {
    const memberData = await FacultyService.getFacultyById(id); // ← pass id directly, no Number()

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

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