import { NextResponse } from 'next/server';
import { v0 } from 'v0-sdk';

export async function POST(request: Request) {
  try {
    const { chatId, message, projectId } = await request.json();

    if (!chatId || !message) {
      return NextResponse.json(
        { error: 'Missing required fields: chatId and message' },
        { status: 400 }
      );
    }

    console.log(`[API] Iterate request for chat ${chatId}`);

    // The v0 SDK doesn't support sending follow-up messages via API
    // Users need to continue the conversation on v0.dev
    const webUrl = `https://v0.dev/chat/${chatId}`;

    return NextResponse.json({
      success: true,
      chatId: chatId,
      webUrl: webUrl,
      message: 'To continue this conversation, please visit the chat on v0.dev',
      note: 'The v0 API does not currently support sending follow-up messages. Use the web interface to iterate on your design.',
      yourMessage: message,
    });
  } catch (error) {
    console.error('[API] Error processing iteration request:', error);
    
    return NextResponse.json(
      { 
        error: error instanceof Error ? error.message : 'Failed to process request',
        details: error instanceof Error ? error.stack : undefined,
      },
      { status: 500 }
    );
  }
}

