import { NextRequest, NextResponse } from 'next/server'; import { prisma } from '@/lib/db'; import { verifyAdminAuth } from '@/lib/admin-auth'; export async function GET( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const adminUser = await verifyAdminAuth(request); if (!adminUser) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const { id } = await params; const socialMediaLink = await prisma.socialMediaLink.findUnique({ where: { id }, include: { creator: { select: { name: true, email: true } }, updater: { select: { name: true, email: true } } } }); if (!socialMediaLink) { return NextResponse.json( { success: false, error: 'Social media link not found' }, { status: 404 } ); } return NextResponse.json({ success: true, data: socialMediaLink }); } catch (error) { console.error('Error fetching social media link:', error); return NextResponse.json( { success: false, error: 'Failed to fetch social media link' }, { status: 500 } ); } } export async function PUT( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const adminUser = await verifyAdminAuth(request); if (!adminUser) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const { id } = await params; const body = await request.json(); const { platform, name, url, icon, isEnabled, order } = body; if (!platform || !name || !url || !icon) { return NextResponse.json( { success: false, error: 'Platform, name, URL, and icon are required' }, { status: 400 } ); } // Check if another link uses the same platform const existingLink = await prisma.socialMediaLink.findFirst({ where: { platform, id: { not: id } } }); if (existingLink) { return NextResponse.json( { success: false, error: 'Another social media link for this platform already exists' }, { status: 400 } ); } const socialMediaLink = await prisma.socialMediaLink.update({ where: { id }, data: { platform, name, url, icon, isEnabled, order, updatedBy: adminUser.id }, include: { creator: { select: { name: true, email: true } }, updater: { select: { name: true, email: true } } } }); return NextResponse.json({ success: true, data: socialMediaLink }); } catch (error) { console.error('Error updating social media link:', error); return NextResponse.json( { success: false, error: 'Failed to update social media link' }, { status: 500 } ); } } export async function DELETE( request: NextRequest, { params }: { params: Promise<{ id: string }> } ) { try { const adminUser = await verifyAdminAuth(request); if (!adminUser) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const { id } = await params; await prisma.socialMediaLink.delete({ where: { id } }); return NextResponse.json({ success: true, message: 'Social media link deleted successfully' }); } catch (error) { console.error('Error deleting social media link:', error); return NextResponse.json( { success: false, error: 'Failed to delete social media link' }, { status: 500 } ); } }