import { NextResponse } from 'next/server'
import { navidromeGet } from '@/lib/navidrome'

interface LyricsResult {
  lyrics?: {
    artist?: string
    title?: string
    value?: string
  }
}

interface LrcLibResult {
  syncedLyrics?: string | null
  plainLyrics?: string | null
}

export async function GET(request: Request) {
  const { searchParams } = new URL(request.url)
  const artist = searchParams.get('artist') || ''
  const title = searchParams.get('title') || ''

  if (!artist || !title) {
    return NextResponse.json({ lyrics: null, synced: null })
  }

  // Try LRCLIB first for synced (timed) lyrics
  try {
    const controller = new AbortController()
    const timeout = setTimeout(() => controller.abort(), 4000)

    const lrcRes = await fetch(
      `https://lrclib.net/api/get?artist_name=${encodeURIComponent(artist)}&track_name=${encodeURIComponent(title)}`,
      {
        headers: {
          'User-Agent': 'SoulSync/1.0 (jeffemmett@gmail.com)',
        },
        signal: controller.signal,
        cache: 'no-store',
      }
    )
    clearTimeout(timeout)

    if (lrcRes.ok) {
      const lrc: LrcLibResult = await lrcRes.json()
      if (lrc.syncedLyrics) {
        return NextResponse.json({
          lyrics: lrc.plainLyrics || lrc.syncedLyrics.replace(/\[\d{2}:\d{2}\.\d{2,3}\]\s?/g, ''),
          synced: lrc.syncedLyrics,
        })
      }
      if (lrc.plainLyrics) {
        return NextResponse.json({ lyrics: lrc.plainLyrics, synced: null })
      }
    }
  } catch {
    // LRCLIB unavailable, fall through to Navidrome
  }

  // Fallback to Navidrome plain lyrics
  try {
    const data = await navidromeGet<LyricsResult>('getLyrics.view', { artist, title })
    return NextResponse.json({
      lyrics: data.lyrics?.value || null,
      synced: null,
    })
  } catch (error) {
    console.error('Lyrics error:', error)
    return NextResponse.json({ lyrics: null, synced: null })
  }
}
