import { notFound } from "next/navigation";
import { VideoForm } from "@/admin/components/VideoForm";
import { AdminAutoLayout } from "@/admin/layout/AdminAutoLayout";
import { requirePermission } from "@/lib/auth";
import { prisma } from "@/lib/prisma";

type PageProps = {
  params: Promise<{ id: string }>;
  searchParams: Promise<{ error?: string }>;
};

const errorMessages: Record<string, string> = {
  required: "Please enter a title and valid YouTube URL. Description can be short or empty.",
  youtube: "Please enter a valid YouTube video or shorts URL."
};

export default async function EditVideoPage({ params, searchParams }: PageProps) {
  await requirePermission("videos");
  const [{ id }, query] = await Promise.all([params, searchParams]);
  const [video, categories] = await Promise.all([
    prisma.video.findUnique({ where: { id } }),
    prisma.category.findMany({ orderBy: { name: "asc" } })
  ]);

  if (!video) {
    notFound();
  }

  return (
    <AdminAutoLayout
      description="Update video details, description, and publishing options."
      error={query.error}
      errorMessages={errorMessages}
      eyebrow="Video"
      title="Edit video"
      width="wide"
    >
      <VideoForm categories={categories} video={video} />
    </AdminAutoLayout>
  );
}
