import { prisma } from "@/lib/prisma";
import AdminProductsClient from "@/components/AdminProductsClient";
import AdminProductStats from "@/components/AdminProductStats";
import { normalizeMediaUrl } from "@/lib/media-url";

export const dynamic = "force-dynamic";

export default async function AdminProductsPage() {
  const [rows, orderItems] = await Promise.all([
    prisma.product.findMany({
      where: { NOT: { type: "PHYSICAL" } },
      orderBy: { createdAt: "asc" },
    }),
    prisma.orderItem.findMany({
      where: { product: { NOT: { type: "PHYSICAL" } } },
      include: { product: true, order: { include: { user: true } } },
    }),
  ]);

  const products = rows.map((p) => ({
    ...p,
    image: normalizeMediaUrl(p.image) ?? p.image,
  }));

  return (
    <div className="space-y-8">
      <AdminProductStats items={orderItems} catalog="TICKET" />
      <AdminProductsClient catalog="TICKET" initialProducts={products} />
    </div>
  );
}
