export async function GET() {
  const apiKey = process.env.SERPAPI_KEY || "6f49f63fbe238dc99a431723418b46861c1ad9fd2662cefd9f747b2d16196d3a";
  const placeId = process.env.SERPAPI_PLACE_ID || "ChIJ9b7ftlX3K4gRb7-4SkJNGCE";

  try {
    const url = `https://serpapi.com/search.json?engine=google_maps_reviews&hl=en&api_key=${apiKey}&place_id=${placeId}`;
    const response = await fetch(url);

    if (!response.ok) {
      throw new Error(`SerpAPI request failed with status ${response.status}`);
    }

    const data = await response.json();
    console.log('SerpAPI reviews response', data);

    if (data.error) {
      throw new Error(data.error.message || data.error);
    }

    const reviews = Array.isArray(data.reviews) ? data.reviews : [];
    return new Response(JSON.stringify({ reviews, total: reviews.length }), {
      status: 200,
      headers: { "Content-Type": "application/json" },
    });
  } catch (error) {
    console.error('Error fetching reviews:', error);
    return new Response(JSON.stringify({ reviews: [], error: error.message || "Failed to fetch reviews" }), {
      status: 200,
      headers: { "Content-Type": "application/json" },
    });
  }
}
