import { NextResponse } from "next/server";
import prisma from "@/lib/prisma";
import { getUserIdFromRequest } from "@/lib/auth";

export async function GET(req: Request) {
  const userId = getUserIdFromRequest(req);
  if (!userId) return NextResponse.json({ items: [] });
  const logs = await prisma.postViewLog.findMany({
    where: { userId },
    orderBy: { createdAt: "desc" },
    take: 10,
    select: { postId: true, createdAt: true, post: { select: { title: true } } },
  });
  const items = logs.map((l) => ({ id: l.postId, title: l.post?.title ?? "", viewedAt: l.createdAt }));
  return NextResponse.json({ items });
}


