import { NextRequest, NextResponse } from 'next/server';
import { prisma } from '@/lib/db';

export const dynamic = 'force-dynamic';

// PATCH - Update companion name
export async function PATCH(request: NextRequest) {
  try {
    const body = await request.json();
    const { userId, name } = body;
    
    if (!userId || !name) {
      return NextResponse.json({ error: 'userId and name are required' }, { status: 400 });
    }

    if (name.length > 30) {
      return NextResponse.json({ error: 'Name must be 30 characters or less' }, { status: 400 });
    }

    const companion = await prisma.aICompanion.update({
      where: { userId },
      data: { name: name.trim() },
    });

    return NextResponse.json({ success: true, name: companion.name });
  } catch (error) {
    console.error('[AI Agent] Name update error:', error);
    return NextResponse.json({ error: 'Failed to update name' }, { status: 500 });
  }
}
