import { NextRequest, NextResponse } from 'next/server' import { prisma } from '@/lib/prisma' import { auth } from '@/lib/auth' import { v4 as uuidv4 } from 'uuid' // GET /api/levels - Get all levels export async function GET(request: NextRequest) { try { const session = await auth.api.getSession({ headers: request.headers, }) if (!session) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const levels = await prisma.level.findMany({ orderBy: { sortOrder: 'asc' }, }) return NextResponse.json(levels) } catch (error) { console.error('Error fetching levels:', error) return NextResponse.json( { error: 'Failed to fetch levels' }, { status: 500 } ) } } // POST /api/levels - Create a new level export async function POST(request: NextRequest) { try { const session = await auth.api.getSession({ headers: request.headers, }) if (!session) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const body = await request.json() const { imageUrl, answer, hint1, hint2, hint3 } = body if (!imageUrl || !answer) { return NextResponse.json( { error: 'imageUrl and answer are required' }, { status: 400 } ) } // Get max sort order const maxSortOrder = await prisma.level.aggregate({ _max: { sortOrder: true }, }) const sortOrder = (maxSortOrder._max.sortOrder || 0) + 1 const level = await prisma.level.create({ data: { id: uuidv4(), imageUrl, answer, hint1: hint1 || null, hint2: hint2 || null, hint3: hint3 || null, sortOrder, }, }) return NextResponse.json(level, { status: 201 }) } catch (error) { console.error('Error creating level:', error) return NextResponse.json( { error: 'Failed to create level' }, { status: 500 } ) } } // PUT /api/levels - Update a level export async function PUT(request: NextRequest) { try { const session = await auth.api.getSession({ headers: request.headers, }) if (!session) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const body = await request.json() const { id, imageUrl, answer, hint1, hint2, hint3 } = body if (!id) { return NextResponse.json({ error: 'id is required' }, { status: 400 }) } const level = await prisma.level.update({ where: { id }, data: { imageUrl, answer, hint1: hint1 || null, hint2: hint2 || null, hint3: hint3 || null, }, }) return NextResponse.json(level) } catch (error) { console.error('Error updating level:', error) return NextResponse.json( { error: 'Failed to update level' }, { status: 500 } ) } } // DELETE /api/levels - Delete a level export async function DELETE(request: NextRequest) { try { const session = await auth.api.getSession({ headers: request.headers, }) if (!session) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) } const { searchParams } = new URL(request.url) const id = searchParams.get('id') if (!id) { return NextResponse.json({ error: 'id is required' }, { status: 400 }) } await prisma.level.delete({ where: { id }, }) return NextResponse.json({ success: true }) } catch (error) { console.error('Error deleting level:', error) return NextResponse.json( { error: 'Failed to delete level' }, { status: 500 } ) } }