import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth";
import { CARD_TIER_LABELS, resolveQuizRewardTier } from "@/lib/cardTier";

// POST /api/quiz/submit
export async function POST(req: NextRequest) {
  const session = await getServerSession(authOptions);
  if (!session) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const userId = (session.user as any).id;
  const { quizId, answers } = await req.json();

  if (!quizId || !Array.isArray(answers)) {
    return NextResponse.json({ error: "Invalid payload" }, { status: 400 });
  }

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

  if (!quiz) return NextResponse.json({ error: "Quiz not found" }, { status: 404 });
  if (quiz.isProcessed) {
    return NextResponse.json({ error: "این کوییز بعد از قرعه‌کشی بسته شده است" }, { status: 400 });
  }

  const now = new Date();
  if (now < quiz.windowStart || now > quiz.windowEnd) {
    return NextResponse.json({ error: "خارج از بازه زمانی مجاز" }, { status: 400 });
  }

  const existing = await db.quizSubmission.findUnique({
    where: { userId_quizId: { userId, quizId } },
  });
  if (existing) return NextResponse.json({ error: "قبلاً شرکت کرده‌اید" }, { status: 400 });

  let correct = 0;
  quiz.questions.forEach((q, i) => {
    if (answers[i] === q.correctAnswer) correct++;
  });

  const score = quiz.questions.length > 0
    ? Math.round((correct / quiz.questions.length) * 100)
    : 0;
  const rewardTier = resolveQuizRewardTier(quiz, correct);

  const submission = await db.quizSubmission.create({
    data: { userId, quizId, answers, correctAnswers: correct, score },
  });

  return NextResponse.json({
    score,
    correct,
    total: quiz.questions.length,
    rewardTier,
    rewardTierLabel: rewardTier ? CARD_TIER_LABELS[rewardTier] : null,
    submission,
  });
}
