import { navidromeFetch } from '@/lib/navidrome'

export async function GET(
  _request: Request,
  { params }: { params: Promise<{ id: string }> }
) {
  const { id } = await params

  try {
    const res = await navidromeFetch('stream.view', { id })
    const contentType = res.headers.get('content-type') || 'audio/mpeg'
    const contentLength = res.headers.get('content-length')

    const headers: Record<string, string> = {
      'Content-Type': contentType,
      'Cache-Control': 'public, max-age=86400',
      'Accept-Ranges': 'bytes',
    }
    if (contentLength) headers['Content-Length'] = contentLength

    return new Response(res.body, { headers })
  } catch (error) {
    console.error('Stream error:', error)
    return new Response('Stream failed', { status: 502 })
  }
}
