import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { CategoryForm } from "@/components/admin/CategoryForm";
import { requireAdmin } from "@/lib/admin-actions";
import { db } from "@/lib/db";

export const metadata: Metadata = {
  title: "Edit Category",
  description: "Rename a catalog category.",
};

interface EditCategoryPageProps {
  params: Promise<{ id: string }>;
}

export default async function EditCategoryPage({ params }: EditCategoryPageProps) {
  await requireAdmin();
  const { id } = await params;
  const category = await db.category.findUnique({
    where: { id: Number(id) },
  });
  if (!category) notFound();

  return (
    <div>
      <h1 className="text-2xl font-bold text-white sm:text-3xl">
        Edit: {category.name}
      </h1>
      <p className="mt-1 mb-6 text-sm text-slate-400">
        Renaming the slug changes public category URLs — old links will 404.
      </p>
      <div className="max-w-2xl rounded-2xl border border-slate-800 bg-slate-900 p-5">
        <CategoryForm
          categoryId={category.id}
          initial={{
            name: category.name,
            slug: category.slug,
            blurb: category.blurb,
          }}
          submitLabel="Save changes"
        />
      </div>
    </div>
  );
}
