import { NextResponse } from 'next/server';
import { getAnalyticsSummary } from '@/lib/analytics/service';

/**
 * GET /api/analytics/summary
 *
 * Returns aggregated analytics data for the Overview dashboard.
 * Query params: startDate, endDate (ISO strings, optional)
 *
 * This endpoint feeds data into the existing chart and table elements
 * on the Overview dashboard without altering its layout.
 */
export async function GET(request: Request) {
  try {
    const { searchParams } = new URL(request.url);
    const startDate = searchParams.get('startDate') || undefined;
    const endDate = searchParams.get('endDate') || undefined;

    const summary = await getAnalyticsSummary(startDate, endDate);

    return NextResponse.json({ success: true, data: summary });
  } catch (error: any) {
    console.error('[analytics/summary] GET error:', error?.message ?? error);
    return NextResponse.json(
      { success: false, error: error?.message ?? 'Failed to fetch analytics summary.' },
      { status: 500 }
    );
  }
}
