import { NextRequest, NextResponse } from "next/server";
import { resolveApiKeyUser } from "@/lib/api-auth";

/**
 * GET /api/auth/me
 *
 * Returns the current user's live plan and profile using their API key.
 * Called by the CLI on startup and after each heartbeat cycle to detect
 * plan changes (upgrades or downgrades) without requiring re-authentication.
 *
 * Auth: Bearer token (lh_sk_* API key).
 */
export async function GET(req: NextRequest) {
  const user = await resolveApiKeyUser(req);
  if (!user) {
    return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  }

  return NextResponse.json({
    id: user.id,
    email: user.email,
    name: user.name,
    plan: user.plan,
  });
}
