// app/admin/videos/[videoId]/edit/page.tsx
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
import { prisma } from '@/lib/prisma';
import { redirect } from 'next/navigation';
import EditVideoClient from './edit-video-client';

export default async function EditVideoPage({
  params,
}: {
  params: Promise<{ videoId: string }>;
}) {
  const { videoId } = await params;
  
  const session = await getServerSession(authOptions);
  const role = (session as any)?.user?.role ?? null;

  if (!session?.user) {
    redirect('/login');
  }

  if (!(role === 'admin' || role === 'superadmin')) {
    redirect('/dashboard');
  }

  const video = await prisma.video.findUnique({
    where: { id: videoId },
    include: {
      playlist: {
        include: {
          course: true,
        },
      },
      videoCourses: true,
    },
  });

  if (!video) {
    redirect('/admin/videos');
  }

  return (
    <EditVideoClient
      video={{
        id: video.id,
        title: video.title,
        description: (video as any).description || '',
        thumbnail: video.thumbnail ?? undefined,
        playlistTitle: video.playlist.title,
        courseTitle: video.playlist.course.title,
        restrictedCourseIds: video.videoCourses
          .filter((vc) => vc.exclusive)
          .map((vc) => vc.courseId),
      }}
    />
  );
}
