import { NextRequest, NextResponse } from 'next/server';
import { getAIResponse } from '@/lib/ai-service';
import { Language } from '@/types';

export async function POST(request: NextRequest) {
  try {
    const { question, language }: { question: string; language: Language } = await request.json();

    if (!question || !question.trim()) {
      return NextResponse.json(
        { error: 'Question is required' },
        { status: 400 }
      );
    }

    const response = await getAIResponse(question.trim(), language || 'en');
    
    return NextResponse.json(response);
  } catch (error) {
    console.error('Chat API error:', error);
    
    return NextResponse.json(
      { 
        error: 'Internal server error',
        message: 'Sorry, I had trouble processing your question. Please try again!'
      },
      { status: 500 }
    );
  }
}
