import { NextRequest, NextResponse } from 'next/server'
import { cookies } from 'next/headers'

const API_BASE_URL = process.env.API_BASE_URL || process.env.NEXT_PUBLIC_API_BASE_URL

export async function GET(req: NextRequest) {
  const cookieStore = await cookies()
  const token = cookieStore.get('session_token')?.value

  const headers: Record<string, string> = { Accept: 'application/json' }
  if (token) headers['Authorization'] = `Bearer ${token}`

  const ids = req.nextUrl.searchParams.get('ids') ?? ''

  try {
    const res = await fetch(
      `${API_BASE_URL}/api/v1/roadmap/votes?ids=${encodeURIComponent(ids)}`,
      { headers, cache: 'no-store' }
    )
    if (!res.ok) {
      // Degrade gracefully — return empty map so UI shows 0 votes
      return NextResponse.json({})
    }
    return NextResponse.json(await res.json())
  } catch {
    return NextResponse.json({})
  }
}
