import { NextResponse } from 'next/server' import { getUserFromToken } from '@/lib/auth' import { prisma } from '@/lib/db' export const runtime = 'nodejs' function getErrorMessages(locale: string = 'ro') { const messages = { ro: { unauthorized: 'Nu esti autentificat', updateFailed: 'Actualizarea setărilor a eșuat', success: 'Setări actualizate cu succes', invalidData: 'Date invalide' }, en: { unauthorized: 'Unauthorized', updateFailed: 'Settings update failed', success: 'Settings updated successfully', invalidData: 'Invalid data' } } return messages[locale as keyof typeof messages] || messages.ro } export async function PUT(request: Request) { try { const url = new URL(request.url) const locale = url.searchParams.get('locale') || 'ro' const messages = getErrorMessages(locale) // Get token from authorization header const authHeader = request.headers.get('authorization') const token = authHeader?.replace('Bearer ', '') if (!token) { return NextResponse.json({ error: messages.unauthorized }, { status: 401 }) } // Verify token and get user const user = await getUserFromToken(token) if (!user) { return NextResponse.json({ error: messages.unauthorized }, { status: 401 }) } // Parse request body const body = await request.json() const { theme, fontSize, notifications, emailUpdates, language } = body // Validate input - allow partial updates const updateData: any = {} if (theme !== undefined) { if (!['light', 'dark', 'auto'].includes(theme)) { return NextResponse.json({ error: messages.invalidData }, { status: 400 }) } updateData.theme = theme } if (fontSize !== undefined) { if (!['small', 'medium', 'large'].includes(fontSize)) { return NextResponse.json({ error: messages.invalidData }, { status: 400 }) } updateData.fontSize = fontSize } // Note: notifications and emailUpdates would need additional columns in User model // For now, we'll skip them or store in a JSON field if needed // Update user settings const updatedUser = await prisma.user.update({ where: { id: user.id }, data: updateData, select: { id: true, email: true, name: true, role: true, theme: true, fontSize: true, subscriptionTier: true, subscriptionStatus: true, conversationLimit: true, conversationCount: true, limitResetDate: true } }) return NextResponse.json({ success: true, message: messages.success, user: updatedUser }) } catch (error) { console.error('Settings update error:', error) const url = new URL(request.url) const locale = url.searchParams.get('locale') || 'ro' const messages = getErrorMessages(locale) return NextResponse.json({ error: messages.updateFailed }, { status: 500 }) } }