import Link from "next/link";
import { ArrowLeft } from "lucide-react";
import WhatsappTemplateForm from "../../WhatsappTemplateForm";
import { prisma } from "@/lib/prisma";
import { notFound } from "next/navigation";

export const metadata = {
  title: "Edit Template WhatsApp | Admin",
};

export default async function EditWhatsappTemplatePage({
  params,
}: {
  params: Promise<{ id: string }>;
}) {
  const { id } = await params;
  const templateId = Number(id);

  if (isNaN(templateId)) {
    notFound();
  }

  const template = await prisma.whatsappTemplate.findUnique({
    where: { id: templateId },
  });

  if (!template) {
    notFound();
  }

  return (
    <div className="space-y-6">
      <div className="flex items-center gap-4">
        <Link
          href="/admin/whatsapp-templates"
          className="p-2 bg-white rounded-lg border border-slate-200 text-slate-500 hover:bg-slate-50 transition"
        >
          <ArrowLeft className="w-5 h-5" />
        </Link>
        <div>
          <h1 className="text-2xl font-bold text-slate-900">Edit Template</h1>
          <p className="text-sm text-slate-500 mt-1">
            Ubah template pesan otomatis WhatsApp.
          </p>
        </div>
      </div>

      <WhatsappTemplateForm initialData={template} />
    </div>
  );
}
