/**
 * GET /api/writing-assistant/documents?brandId=X — List all writing documents.
 */
import { NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { prisma } from "@/lib/prisma";

export const dynamic = "force-dynamic";

export async function GET(req: Request) {
  const user = await getCurrentUser();
  if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });

  const brandId = new URL(req.url).searchParams.get("brandId");
  if (!brandId) return NextResponse.json({ error: "brandId required" }, { status: 400 });

  const documents = await prisma.writingDocument.findMany({
    where: { brandId },
    orderBy: { updatedAt: "desc" },
    select: { id: true, title: true, targetKeyword: true, seoScore: true, llmoScore: true, humanScore: true, grade: true, wordCount: true, status: true, updatedAt: true },
  });

  return NextResponse.json({ documents });
}
