import { NextResponse } from "next/server";
import { db } from "@/lib/db";

// GET /api/quiz - get today's active quiz
export async function GET() {
  const now = new Date();
  const todayStart = new Date(now);
  todayStart.setHours(0, 0, 0, 0);
  const todayEnd = new Date(now);
  todayEnd.setHours(23, 59, 59, 999);

  const quiz = await db.dailyQuiz.findFirst({
    where: { date: { gte: todayStart, lte: todayEnd } },
    include: {
      questions: {
        orderBy: { order: "asc" },
        select: {
          id: true,
          questionText: true,
          options: true,
          order: true,
        },
      },
    },
  });

  if (!quiz) return NextResponse.json(null);

  const isActive = !quiz.isProcessed && now >= quiz.windowStart && now <= quiz.windowEnd;
  return NextResponse.json({ ...quiz, isActive });
}
