import { notFound } from "next/navigation";
import db from "@/lib/db";
import PublicProfileContent from "./public-profile-content";

export async function generateMetadata({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const user = await db.user.findUnique({
    where: { id },
    select: { name: true },
  });

  return {
    title: user ? `${user.name || "User"}'s Profile | ZuriGuide` : "Profile Not Found",
  };
}

export default async function PublicProfilePage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;
  const response = await fetch(`${process.env.NEXT_PUBLIC_APP_URL || 'http://localhost:3000'}/api/users/${id}`, {
    cache: "no-store",
  });

  if (!response.ok) {
    notFound();
  }

  const data = await response.json();

  return <PublicProfileContent user={data.user} stats={data.stats} />;
}
