// app/api/watch-history/route.ts import { NextResponse } from 'next/server'; import { getServerSession } from 'next-auth'; import { authOptions } from '@/lib/auth-options'; import { prisma } from '@/lib/prisma'; export async function GET(req: Request) { try { const session = await getServerSession(authOptions); if (!session?.user?.email) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); const userEmail = session.user.email; const user = await prisma.user.findUnique({ where: { email: userEmail } }); if (!user) return NextResponse.json({ error: 'user not found' }, { status: 404 }); // Fetch all videos watched by user, sorted by most recently updated const watchHistory = await prisma.videoProgress.findMany({ where: { userId: user.id }, include: { video: { select: { id: true, title: true, thumbnail: true, durationSec: true, url: true, }, }, }, orderBy: { updatedAt: 'desc' }, }); return NextResponse.json(watchHistory); } catch (err: any) { console.error('watch history GET error', err); return NextResponse.json( { error: err?.message ?? 'server error' }, { status: 500 } ); } }