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

/**
 * GET /api/dashboard/marketplace/credits
 *
 * Returns the current user's marketplace prepaid credits balance (session auth).
 */
export async function GET() {
  const session = await auth();
  if (!session?.user?.id) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  try {
    const user = await db.user.findUnique({
      where: { id: session.user.id },
      select: { marketplaceCreditsCents: true },
    });
    if (!user) {
      return NextResponse.json({ error: "User not found" }, { status: 404 });
    }
    return NextResponse.json({
      balance_cents: user.marketplaceCreditsCents,
    });
  } catch {
    return NextResponse.json(
      { error: "Failed to load credits balance" },
      { status: 500 },
    );
  }
}
