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 artist = await prisma.artist.findUnique({ where: { id }, include: { user: { select: { email: true, username: true, displayName: true, }, }, label: { select: { id: true, name: true, slug: true, }, }, _count: { select: { songs: true, albums: true, }, }, }, }) if (!artist) { return NextResponse.json({ error: 'Artist not found' }, { status: 404 }) } return NextResponse.json(artist) } catch (error) { console.error('Error fetching artist:', error) return NextResponse.json({ error: 'Failed to fetch artist' }, { status: 500 }) } } export async function PUT( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const { user, artist } = await requireArtist() const { id } = await params if (artist.id !== id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 403 }) } const body = await request.json() const { name, bio, website, twitter, instagram, spotify, avatarUrl, bannerUrl } = body const updateData: any = {} if (name !== undefined) { updateData.name = name updateData.slug = slugify(name) } if (bio !== undefined) updateData.bio = bio if (website !== undefined) updateData.website = website if (twitter !== undefined) updateData.twitter = twitter if (instagram !== undefined) updateData.instagram = instagram if (spotify !== undefined) updateData.spotify = spotify if (avatarUrl !== undefined) updateData.avatarUrl = avatarUrl if (bannerUrl !== undefined) updateData.bannerUrl = bannerUrl const updatedArtist = await prisma.artist.update({ where: { id }, data: updateData, include: { user: { select: { email: true, username: true, displayName: true, }, }, label: { select: { id: true, name: true, slug: true, }, }, }, }) return NextResponse.json(updatedArtist) } 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 artist:', error) return NextResponse.json({ error: 'Failed to update artist' }, { status: 500 }) } }