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

/**
 * GET /api/stripe/connect/status
 * Returns whether the current user has a Stripe Connect Express account linked
 * and optionally the account id (for dashboard link).
 */
export async function GET() {
  try {
    const session = await auth();

    if (!session?.user?.id) {
      return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
    }

    const user = await db.user.findUnique({
      where: { id: session.user.id },
      select: { stripeConnectAccountId: true },
    });

    const accountId = user?.stripeConnectAccountId ?? null;
    const connected = Boolean(accountId);

    return NextResponse.json({
      connected,
      ...(accountId && { accountId }),
    });
  } catch {
    return NextResponse.json(
      { error: "Failed to get Connect status" },
      { status: 500 },
    );
  }
}
