import { NextResponse } from 'next/server';
import { withErrorHandler, RouteContext } from '@server/middleware/withErrorHandler';
import { withAuth, AuthedRequest } from '@server/middleware/withAuth';
import { withValidationAuthed } from '@server/middleware/withValidation';
import { updateModifierOptionSchema } from '@server/validators/menu-modifiers.validator';
import { updateModifierOption, deleteModifierOption } from '@server/services/menu-modifiers.service';

export const PATCH = withErrorHandler(
  withAuth(
    withValidationAuthed(updateModifierOptionSchema, async (req, ctx: RouteContext) => {
      const { id, optId } = await ctx.params;
      const option = await updateModifierOption(optId, id, req.session.restaurantId!, req.parsedBody as Record<string, unknown>);
      return NextResponse.json({ option });
    })
  )
);

export const DELETE = withErrorHandler(
  withAuth(async (req: AuthedRequest, ctx: RouteContext) => {
    const { id, optId } = await ctx.params;
    await deleteModifierOption(optId, id, req.session.restaurantId!);
    return NextResponse.json({ success: true });
  })
);
