import { NextResponse } from 'next/server';
import { chatService } from '@/lib/ai/services/chat.service';

export async function POST(req: Request) {
  try {
    const body = await req.json();
    console.log("TEST ROUTE INCOMING BODY:", body);
    
    // Simulate what the server action does
    const result = await chatService.chat(body.message, body.conversationId, body.notebookId);
    
    return NextResponse.json({ success: true, result });
  } catch (err: any) {
    console.error("====== TEST ROUTE CHAT ERROR ======");
    console.error("NAME:", err.name);
    console.error("MSG:", err.message);
    if (err.cause) console.error("CAUSE:", JSON.stringify(err.cause, null, 2));
    if (err.data) console.error("DATA:", JSON.stringify(err.data, null, 2));
    if (err.stack) console.error("STACK:", err.stack);
    console.error("===================================");
    return NextResponse.json({ 
      success: false, 
      error: err.message,
      name: err.name,
      cause: err.cause,
      data: err.data
    }, { status: 500 });
  }
}
