import { NextRequest, NextResponse } from 'next/server' import prisma from '@/lib/prisma' import { getMobileUserId } from '@/lib/mobile-auth' export async function GET(req: NextRequest) { const userId = getMobileUserId(req) if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) const now = new Date() const decks = await prisma.flashcardDeck.findMany({ where: { userId }, include: { notebook: { select: { name: true } }, flashcards: { select: { id: true, interval: true, nextReviewAt: true, front: true, }, }, }, orderBy: { updatedAt: 'desc' }, }) const result = decks.map((deck) => ({ id: deck.id, name: deck.name, notebookId: deck.notebookId, notebookName: deck.notebook?.name ?? null, totalCards: deck.flashcards.length, dueCount: deck.flashcards.filter((c) => c.nextReviewAt <= now).length, masteredCount: deck.flashcards.filter((c) => c.interval >= 7).length, })) return NextResponse.json({ decks: result }) }