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';
import { requirePlanFeature } from '@server/utils/features';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest) => {
    const { restaurantId, branchId } = req.session;
    await requirePlanFeature(restaurantId!, 'analytics');
    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 });
  })
);
