import { db } from "@/lib/db";
import { requireAdmin } from "@/lib/session";
import { notFound } from "next/navigation";
import QuizForm from "../../QuizForm";

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

  const quiz = await db.dailyQuiz.findUnique({
    where: { id },
    include: {
      questions: {
        orderBy: { order: "asc" },
      },
      _count: {
        select: { submissions: true },
      },
    },
  });

  if (!quiz) notFound();
  if (quiz.isProcessed) notFound();

  return (
    <div className="max-w-2xl mx-auto">
      <h1 className="text-2xl font-bold mb-6">ویرایش کوییز</h1>
      <QuizForm
        quizId={quiz.id}
        submissionsCount={quiz._count.submissions}
        initial={{
          date: quiz.date,
          windowStart: quiz.windowStart,
          windowEnd: quiz.windowEnd,
          goldWinnersCount: quiz.goldWinnersCount,
          silverWinnersCount: quiz.silverWinnersCount,
          bronzeWinnersCount: quiz.bronzeWinnersCount,
          goldMinCorrect: quiz.goldMinCorrect,
          silverMinCorrect: quiz.silverMinCorrect,
          bronzeMinCorrect: quiz.bronzeMinCorrect,
          questions: quiz.questions.map((question) => ({
            questionText: question.questionText,
            options: question.options,
            correctAnswer: question.correctAnswer,
          })),
        }}
      />
    </div>
  );
}
