import { notFound } from "next/navigation";
import { GalleryForm } from "@/components/admin/gallery/GalleryForm";
import { prisma } from "@/lib/prisma";
import { updateGallery } from "../../actions";

export default async function EditGalleryPage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const galleryId = Number(id);

  if (Number.isNaN(galleryId)) {
    notFound();
  }

  const gallery = await prisma.gallery.findUnique({
    where: { id: galleryId },
  });

  if (!gallery) {
    notFound();
  }

  return (
    <div>
      <h1 className="text-2xl font-semibold text-heading">Edit Foto</h1>
      <p className="mt-1 text-sm text-body">{gallery.title ?? "Tanpa judul"}</p>

      <div className="mt-6 rounded-2xl bg-white p-8 shadow-sm">
        <GalleryForm
          action={updateGallery.bind(null, galleryId)}
          gallery={gallery}
          submitLabel="Simpan Perubahan"
        />
      </div>
    </div>
  );
}
