import { NextResponse } from 'next/server';
import { withErrorHandler, RouteContext } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import { listMenuAuditLogForItem } from '@server/services/menu.service';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest, ctx: RouteContext) => {
    const { id } = await ctx.params;
    const url = new URL(req.url);
    const limitRaw = parseInt(url.searchParams.get('limit') ?? '50', 10);
    const limit = Number.isFinite(limitRaw) ? Math.min(Math.max(limitRaw, 1), 200) : 50;
    const entries = await listMenuAuditLogForItem(id, req.session.restaurantId!, limit);
    return NextResponse.json({ entries });
  })
);
