import { notFound } from "next/navigation";
import { auth } from "@/auth";
import { db } from "@/lib/db";
import { ReviewPageClient } from "./ReviewPageClient";

export async function generateMetadata({ params }: { params: Promise<{ versionId: string }> }) {
  const { versionId } = await params;
  const version = await db.version.findUnique({
    where: { id: versionId },
    include: {
      shot: { select: { shotCode: true } },
      task: { select: { title: true } },
    },
  });
  if (!version) return { title: "Not Found" };
  const label = version.shot?.shotCode ?? version.task?.title ?? "Version";
  return { title: `Review — ${label} v${version.versionNumber}` };
}

async function getReviewData(versionId: string) {
  const version = await db.version.findUnique({
    where: { id: versionId },
    include: {
      shot: {
        include: {
          project: {
            select: { id: true, name: true, code: true },
          },
          versions: {
            orderBy: { versionNumber: "desc" },
            select: {
              id: true,
              versionNumber: true,
              approvalStatus: true,
              isLatest: true,
              fps: true,
              duration: true,
              notes: true,
              fileSize: true,
              createdAt: true,
            },
          },
        },
      },
      task: {
        include: {
          project: { select: { id: true, name: true, code: true } },
          shot: { select: { id: true, shotCode: true } },
          asset: { select: { id: true, assetCode: true, name: true } },
          versions: {
            orderBy: { versionNumber: "desc" },
            select: {
              id: true,
              versionNumber: true,
              approvalStatus: true,
              isLatest: true,
              fps: true,
              duration: true,
              notes: true,
              fileSize: true,
              createdAt: true,
            },
          },
        },
      },
      artist: {
        select: { id: true, name: true, image: true, email: true },
      },
    },
  });

  if (!version) return null;

  const comments = await db.comment.findMany({
    where: { versionId },
    orderBy: { frameNumber: "asc" },
    include: {
      author: { select: { id: true, name: true, image: true, email: true } },
      replies: {
        orderBy: { createdAt: "asc" },
        include: {
          author: { select: { id: true, name: true, image: true, email: true } },
        },
      },
    },
  });

  const annotations = await db.annotation.findMany({
    where: { versionId },
    orderBy: { frameNumber: "asc" },
  });

  return { version, comments, annotations };
}

export default async function ReviewPage({
  params,
}: {
  params: Promise<{ versionId: string }>;
}) {
  const { versionId } = await params;
  const session = await auth();
  if (!session?.user) {
    // Already handled by middleware but be safe
    return null;
  }

  const data = await getReviewData(versionId);
  if (!data) notFound();

  const canApprove = ["ADMIN", "PRODUCER", "SUPERVISOR", "CLIENT"].includes(
    session.user.role
  );
  const canShare = ["ADMIN", "PRODUCER", "SUPERVISOR"].includes(session.user.role);

  return (
    <ReviewPageClient
      version={data.version as any}
      comments={data.comments as any}
      annotations={data.annotations as any}
      canApprove={canApprove}
      canShare={canShare}
      currentUserId={session.user.id}
    />
  );
}
