/**
 * POST /api/gbp/locations/[id]/reviews/[reviewId]/draft-reply — AI reply draft.
 */
import { NextResponse } from "next/server";
import { getCurrentUser } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
import { generateReplyDraft } from "@/lib/services/gbp-multi-location";

export const dynamic = "force-dynamic";

export async function POST(_req: Request, { params }: { params: { id: string; reviewId: string } }) {
  const user = await getCurrentUser();
  if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  const loc = await prisma.gbpLocation.findUnique({ where: { id: params.id }, include: { brand: { include: { organization: { include: { memberships: true } } } } } });
  if (!loc) return NextResponse.json({ error: "Not found" }, { status: 404 });
  if (!loc.brand.organization.memberships.some((m: { userId: string }) => m.userId === user.id)) return NextResponse.json({ error: "Access denied" }, { status: 403 });

  const draft = await generateReplyDraft(params.reviewId, loc.brand.name);
  return NextResponse.json({ draft });
}
