import { NextRequest, NextResponse } from 'next/server' import { requireAuth } from '@/lib/auth' import { getUserQueue, addToQueue, updateUserQueue } from '@/lib/player' import { prisma } from '@/lib/prisma' export async function GET(request: NextRequest) { try { const user = await requireAuth() const queue = await getUserQueue(user.id) // Get song details for the queue const songIds = queue.songIds as string[] || [] if (songIds.length === 0) { return NextResponse.json( { ...queue, songIds: [], songs: [], }, { status: 200 } ) } const songs = await prisma.song.findMany({ where: { id: { in: songIds }, isPublic: true, }, select: { id: true, title: true, slug: true, duration: true, coverUrl: true, artist: { select: { id: true, name: true, slug: true, }, }, album: { select: { id: true, title: true, slug: true, }, }, }, }) // Order songs according to queue order const orderedSongs = songIds.map(id => songs.find(song => song.id === id)).filter(Boolean) return NextResponse.json( { ...queue, songIds, songs: orderedSongs, }, { status: 200 } ) } catch (error) { console.error('Get queue error:', error) if (error instanceof Error && error.message === 'Unauthorized') { return NextResponse.json( { error: 'Unauthorized' }, { status: 401 } ) } return NextResponse.json( { error: 'Failed to fetch queue' }, { status: 500 } ) } } export async function POST(request: NextRequest) { try { const user = await requireAuth() const body = await request.json() const { songIds, currentIndex, isShuffled, repeatMode } = body // Validate song IDs if (songIds && Array.isArray(songIds)) { const songs = await prisma.song.findMany({ where: { id: { in: songIds }, OR: [ { isPublic: true }, { artistId: user.id }, ], }, select: { id: true }, }) const validSongIds = songs.map(song => song.id) const invalidSongIds = songIds.filter(id => !validSongIds.includes(id)) if (invalidSongIds.length > 0) { return NextResponse.json( { error: 'Invalid song IDs', invalidSongs: invalidSongIds }, { status: 400 } ) } const queue = await addToQueue(user.id, songIds) return NextResponse.json(queue, { status: 200 }) } // Update queue properties const currentQueue = await getUserQueue(user.id) const updatedQueue = await updateUserQueue( user.id, currentQueue.songIds as string[] || [], currentIndex, isShuffled, repeatMode ) return NextResponse.json(updatedQueue, { status: 200 }) } catch (error) { console.error('Update queue error:', error) if (error instanceof Error && error.message === 'Unauthorized') { return NextResponse.json( { error: 'Unauthorized' }, { status: 401 } ) } return NextResponse.json( { error: 'Failed to update queue' }, { status: 500 } ) } } export async function DELETE(request: NextRequest) { try { const user = await requireAuth() // Clear the queue const queue = await updateUserQueue(user.id, [], 0, false, 'none') return NextResponse.json( { message: 'Queue cleared', queue }, { status: 200 } ) } catch (error) { console.error('Clear queue error:', error) if (error instanceof Error && error.message === 'Unauthorized') { return NextResponse.json( { error: 'Unauthorized' }, { status: 401 } ) } return NextResponse.json( { error: 'Failed to clear queue' }, { status: 500 } ) } }