import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";
import { getActiveAccountId } from "@/lib/account-context";
import { apiRouteErrorResponse } from "@/lib/auth/api";
import { requireUserAccountScope } from "@/lib/auth/user-scope";

export async function GET() {
  try {
    const accountId = await getActiveAccountId();
    const { where } = await requireUserAccountScope(accountId);
    const published = await prisma.published.findMany({
      where,
      orderBy: { publishedAt: "desc" },
      select: {
        id: true,
        accountId: true,
        text: true,
        angle: true,
        hook: true,
        rationale: true,
        draftType: true,
        permalink: true,
        publishedAt: true,
        views: true,
        likes: true,
        replies: true,
      },
    });

    return NextResponse.json({ published });
  } catch (error) {
    return apiRouteErrorResponse(error, "published");
  }
}