import { NextRequest, NextResponse } from 'next/server' import { prisma } from '@/lib/prisma' import { requireArtist, slugify } from '@/lib/auth' export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const { id } = await params const album = await prisma.album.findUnique({ where: { id }, include: { artist: { select: { id: true, name: true, slug: true, avatarUrl: true, verified: true, }, }, songs: { include: { genres: { include: { genre: true, }, }, }, orderBy: { createdAt: 'asc', }, }, }, }) if (!album) { return NextResponse.json({ error: 'Album not found' }, { status: 404 }) } return NextResponse.json(album) } catch (error) { console.error('Error fetching album:', error) return NextResponse.json({ error: 'Failed to fetch album' }, { status: 500 }) } } export async function PUT( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const { artist } = await requireArtist() const { id } = await params const existingAlbum = await prisma.album.findUnique({ where: { id }, }) if (!existingAlbum) { return NextResponse.json({ error: 'Album not found' }, { status: 404 }) } if (existingAlbum.artistId !== artist.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 403 }) } const body = await request.json() const { title, description, coverUrl, releaseDate, albumType } = body const updateData: any = {} if (title !== undefined) { updateData.title = title updateData.slug = slugify(title) } if (description !== undefined) updateData.description = description if (coverUrl !== undefined) updateData.coverUrl = coverUrl if (releaseDate !== undefined) updateData.releaseDate = releaseDate ? new Date(releaseDate) : null if (albumType !== undefined) updateData.albumType = albumType const updatedAlbum = await prisma.album.update({ where: { id }, data: updateData, include: { artist: { select: { id: true, name: true, slug: true, avatarUrl: true, verified: true, }, }, _count: { select: { songs: true, }, }, }, }) return NextResponse.json(updatedAlbum) } catch (error) { if (error instanceof Error && error.message === 'Unauthorized') { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } if (error instanceof Error && error.message === 'Artist profile required') { return NextResponse.json({ error: 'Artist profile required' }, { status: 403 }) } console.error('Error updating album:', error) return NextResponse.json({ error: 'Failed to update album' }, { status: 500 }) } } export async function DELETE( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const { artist } = await requireArtist() const { id } = await params const existingAlbum = await prisma.album.findUnique({ where: { id }, }) if (!existingAlbum) { return NextResponse.json({ error: 'Album not found' }, { status: 404 }) } if (existingAlbum.artistId !== artist.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 403 }) } await prisma.album.delete({ where: { id }, }) return NextResponse.json({ message: 'Album deleted successfully' }) } catch (error) { if (error instanceof Error && error.message === 'Unauthorized') { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } if (error instanceof Error && error.message === 'Artist profile required') { return NextResponse.json({ error: 'Artist profile required' }, { status: 403 }) } console.error('Error deleting album:', error) return NextResponse.json({ error: 'Failed to delete album' }, { status: 500 }) } }