import { NextRequest, NextResponse } from "next/server";

const API_URL = "https://api.astrology.togwe.com/api/user/astrologers/top";

// Confirmed live: needs no Authorization header (powers the logged-out
// homepage teaser), so this proxy forwards one only if present.
export async function GET(request: NextRequest) {
  try {
    const authorization = request.headers.get("authorization");
    const limit = request.nextUrl.searchParams.get("limit") || "3";

    const response = await fetch(`${API_URL}?limit=${encodeURIComponent(limit)}`, {
      method: "GET",
      headers: {
        Accept: "application/json",
        ...(authorization ? { Authorization: authorization } : {}),
      },
      next: { revalidate: 60 },
    });

    const data = await response.json();

    return NextResponse.json(data, { status: response.status });
  } catch (error) {
    console.error("Top astrologers proxy error:", error);

    return NextResponse.json(
      { message: "Unable to connect to the astrologer API.", recordList: [], status: 500 },
      { status: 500 },
    );
  }
}
