import { NextRequest, NextResponse } from 'next/server' import { prisma } from '@/lib/prisma' import { requireAuth } from '@/lib/auth' export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const user = await requireAuth() const { id } = await params // Check if user owns this label const label = await prisma.label.findUnique({ where: { id }, select: { userId: true }, }) if (!label) { return NextResponse.json({ error: 'Label not found' }, { status: 404 }) } if (label.userId !== user.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 403 }) } const invitations = await prisma.labelInvitation.findMany({ where: { labelId: id }, include: { artist: { select: { id: true, name: true, slug: true, avatarUrl: true, }, }, }, orderBy: { createdAt: 'desc', }, }) return NextResponse.json(invitations) } catch (error) { if (error instanceof Error && error.message === 'Unauthorized') { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } console.error('Error fetching label invitations:', error) return NextResponse.json({ error: 'Failed to fetch label invitations' }, { status: 500 }) } } export async function POST( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const user = await requireAuth() const { id } = await params // Check if user owns this label const label = await prisma.label.findUnique({ where: { id }, select: { userId: true }, }) if (!label) { return NextResponse.json({ error: 'Label not found' }, { status: 404 }) } if (label.userId !== user.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 403 }) } const body = await request.json() const { artistId, message } = body if (!artistId) { return NextResponse.json({ error: 'Artist ID is required' }, { status: 400 }) } // Check if artist exists const artist = await prisma.artist.findUnique({ where: { id: artistId }, select: { id: true, labelId: true }, }) if (!artist) { return NextResponse.json({ error: 'Artist not found' }, { status: 404 }) } // Check if artist already has a label if (artist.labelId) { return NextResponse.json({ error: 'Artist already has a label' }, { status: 400 }) } // Check if pending invitation already exists const existingInvitation = await prisma.labelInvitation.findUnique({ where: { labelId_artistId: { labelId: id, artistId: artistId, }, }, }) if (existingInvitation && existingInvitation.status === 'pending') { return NextResponse.json({ error: 'Pending invitation already exists' }, { status: 400 }) } // Create invitation with 30 days expiration const expiresAt = new Date() expiresAt.setDate(expiresAt.getDate() + 30) const invitation = await prisma.labelInvitation.create({ data: { labelId: id, artistId: artistId, message, expiresAt, }, include: { artist: { select: { id: true, name: true, slug: true, avatarUrl: true, }, }, }, }) return NextResponse.json(invitation, { status: 201 }) } catch (error) { if (error instanceof Error && error.message === 'Unauthorized') { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } console.error('Error creating invitation:', error) return NextResponse.json({ error: 'Failed to create invitation' }, { status: 500 }) } }