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

const restockSchema = z.object({
  target: z.number().int().min(0).nullable().optional(),
});

export const POST = withErrorHandler(
  withAuth(async (req: AuthedRequest, ctx: RouteContext) => {
    const { id } = await ctx.params;
    const body = await req.json().catch(() => ({}));
    const parsed = restockSchema.parse(body ?? {});
    const item = await restockItem(id, req.session.restaurantId!, {
      target: parsed.target ?? undefined,
    });
    return NextResponse.json({ item });
  })
);
