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

interface SubsonicPlaylist {
  id: string
  name: string
  songCount: number
  duration: number
  coverArt: string
}

interface PlaylistsResult {
  playlists?: {
    playlist?: SubsonicPlaylist[]
  }
}

export async function GET() {
  try {
    const data = await navidromeGet<PlaylistsResult>('getPlaylists.view')
    const playlists = (data.playlists?.playlist || [])
      .filter((p) => p.name !== '__soulsync_offline__')
      .map((p) => ({
        id: p.id,
        name: p.name,
        songCount: p.songCount,
        duration: p.duration,
        coverArt: p.coverArt,
      }))
    return NextResponse.json({ playlists })
  } catch (error) {
    console.error('Playlists error:', error)
    return NextResponse.json({ error: 'Failed to load playlists' }, { status: 502 })
  }
}
