import { NextResponse } from 'next/server';
import { withErrorHandler } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import { getRevenueSummary } from '@server/services/analytics.service';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    const { restaurantId, branchId } = req.session;
    const url = new URL(req.url);
    const bId = url.searchParams.get('branch_id') ?? branchId ?? undefined;
    const summary = await getRevenueSummary(restaurantId!, bId);
    return NextResponse.json({ summary });
  })
);
