import { NextRequest, NextResponse } from 'next/server' import prisma from '@/lib/prisma' import { auth } from '@/auth' const COLORS = ['red', 'orange', 'yellow', 'green', 'teal', 'blue', 'purple', 'pink', 'gray']; // GET /api/labels - Get all labels export async function GET(request: NextRequest) { const session = await auth(); if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } try { const labels = await prisma.label.findMany({ where: { userId: session.user.id }, orderBy: { name: 'asc' } }) return NextResponse.json({ success: true, data: labels }) } catch (error) { console.error('GET /api/labels error:', error) return NextResponse.json( { success: false, error: 'Failed to fetch labels' }, { status: 500 } ) } } // POST /api/labels - Create a new label export async function POST(request: NextRequest) { const session = await auth(); if (!session?.user?.id) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } try { const body = await request.json() const { name, color } = body if (!name || typeof name !== 'string') { return NextResponse.json( { success: false, error: 'Label name is required' }, { status: 400 } ) } // Check if label already exists for this user const existing = await prisma.label.findUnique({ where: { name_userId: { name: name.trim(), userId: session.user.id } } }) if (existing) { return NextResponse.json( { success: false, error: 'Label already exists' }, { status: 409 } ) } const label = await prisma.label.create({ data: { name: name.trim(), color: color || COLORS[Math.floor(Math.random() * COLORS.length)], userId: session.user.id } }) return NextResponse.json({ success: true, data: label }) } catch (error) { console.error('POST /api/labels error:', error) return NextResponse.json( { success: false, error: 'Failed to create label' }, { status: 500 } ) } }