import type { Metadata } from "next";
import { notFound } from "next/navigation";
import { GameForm } from "@/components/admin/GameForm";
import { requireAdmin, updateGameAction } from "@/lib/admin-actions";
import { db } from "@/lib/db";
import { getGameAdmin } from "@/lib/games";

export const metadata: Metadata = {
  title: "Edit Game",
  description: "Edit a catalog game.",
};

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

export default async function EditGamePage({ params }: EditGamePageProps) {
  await requireAdmin();
  const { id } = await params;
  const game = await getGameAdmin(Number(id));
  if (!game) notFound();

  const categories = await db.category.findMany({
    orderBy: { name: "asc" },
    select: { id: true, name: true },
  });

  return (
    <div>
      <h1 className="text-2xl font-bold text-white sm:text-3xl">
        Edit: {game.title}
      </h1>
      <p className="mt-1 mb-6 text-sm text-slate-400">
        Slug: /{game.slug} · Status: {game.status} · Last updated{" "}
        {game.updatedAt.toLocaleDateString()}
      </p>
      <GameForm
        categories={categories}
        initial={{
          title: game.title,
          slug: game.slug,
          description: game.description,
          categoryId: game.categoryId,
          developer: game.developer,
          publisher: game.publisher,
          officialUrl: game.officialUrl,
          thumbnail: game.thumbnail,
          embedAllowed: game.embedAllowed,
          embedUrl: game.embedUrl,
          status: game.status,
          featured: game.featured,
          trending: game.trending,
          instructions: game.instructions,
          controls: game.controls,
        }}
        action={updateGameAction.bind(null, game.numericId)}
        submitLabel="Save changes"
      />
    </div>
  );
}
