
/**
 * Chatbot History API Endpoint
 * Returns conversation history for a session
 */

import { NextRequest, NextResponse } from 'next/server';
import { getSession } from '@/lib/chatbot-engine';
import { errorLogger } from '@/lib/error-logger';

export const runtime = 'nodejs';
export const dynamic = 'force-dynamic';

export async function GET(request: NextRequest) {
  try {
    const searchParams = request.nextUrl.searchParams;
    const sessionId = searchParams.get('sessionId');

    if (!sessionId) {
      return NextResponse.json(
        { error: 'Session ID is required' },
        { status: 400 }
      );
    }

    const session = getSession(sessionId);

    return NextResponse.json({
      success: true,
      sessionId: session.id,
      messages: session.messages,
      context: session.context,
      created: session.created,
      lastActive: session.lastActive,
    });
  } catch (error) {
    errorLogger.log(error as Error, 'chatbot-history-api', 'medium');
    return NextResponse.json(
      { error: 'Failed to fetch chat history' },
      { status: 500 }
    );
  }
}
