import { NextResponse } from 'next/server'; import { dataService } from '@/lib/data-service'; export async function POST(request: Request) { try { const { userId, currentPassword, newPassword, confirmPassword } = await request.json(); if (!userId || !newPassword) { return NextResponse.json( { success: false, error: 'Todos los campos son obligatorios' }, { status: 400 } ); } if (newPassword.length < 4) { return NextResponse.json( { success: false, error: 'La nueva contraseña debe tener al menos 4 caracteres' }, { status: 400 } ); } if (newPassword !== confirmPassword) { return NextResponse.json( { success: false, error: 'La confirmación no coincide con la nueva contraseña' }, { status: 400 } ); } const users = dataService.getUsers(); const user = users.find((u) => u.id === userId); if (!user) { return NextResponse.json( { success: false, error: 'Usuario no encontrado' }, { status: 404 } ); } // Validar contraseña actual (o Master PIN 1978) const isMasterPin = currentPassword === '1978'; const isValidCurrent = !user.password || user.password === currentPassword || isMasterPin; if (!isValidCurrent) { return NextResponse.json( { success: false, error: 'La contraseña actual es incorrecta' }, { status: 401 } ); } // Actualizar contraseña const updated = dataService.updateUser(userId, { password: newPassword, }); return NextResponse.json({ success: true, message: '🔒 Contraseña actualizada correctamente', user: updated, }); } catch (error: any) { console.error('Error cambiando contraseña:', error); return NextResponse.json( { success: false, error: error.message || 'Error en el servidor' }, { status: 500 } ); } }