import { prisma } from "@/lib/prisma";
import { notFound } from "next/navigation";
import { VideoPlayer } from "@/components/VideoPlayer";
import { Metadata } from "next";

export const dynamic = "force-dynamic";

export async function generateMetadata({ params }: { params: Promise<{ id: string }> }): Promise<Metadata> {
  const { id } = await params;
  const media = await prisma.media.findUnique({
    where: { id, type: "VIDEO" },
    select: { title: true, thumbnailUrl: true }
  }).catch(() => null);

  if (!media) return {};

  return {
    title: `${media.title} | Lecteur Embed MTBR`,
    robots: { index: false, follow: true }, // Empêcher l'indexation de l'iframe mais laisser Googlebot explorer
  };
}

export default async function EmbedVideoPage({ params }: { params: Promise<{ id: string }> }) {
  const { id } = await params;

  const media = await prisma.media.findUnique({
    where: { id, type: "VIDEO", isPublished: true },
    select: {
      id: true,
      title: true,
      url: true,
      thumbnailUrl: true,
      isPremium: true
    }
  }).catch(() => null);

  if (!media) notFound();

  return (
    <div className="w-full h-full min-h-screen bg-black flex items-center justify-center p-0 m-0 overflow-hidden">
      <div className="w-full h-full relative aspect-video">
        <VideoPlayer mediaUrl={media.url} isLocked={false} />
      </div>
    </div>
  );
}
