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 { createModifierOptionSchema } from '@server/validators/menu-modifiers.validator';
import { listModifierOptions, createModifierOption } from '@server/services/menu-modifiers.service';

export const GET = withErrorHandler(
  withAuth(async (req: AuthedRequest, ctx: RouteContext) => {
    const { id } = await ctx.params;
    const options = await listModifierOptions(id, req.session.restaurantId!);
    return NextResponse.json({ options });
  })
);

export const POST = withErrorHandler(
  withAuth(
    withValidationAuthed(createModifierOptionSchema, async (req, ctx: RouteContext) => {
      const { id } = await ctx.params;
      const option = await createModifierOption(id, req.session.restaurantId!, req.parsedBody as Record<string, unknown>);
      return NextResponse.json({ option }, { status: 201 });
    })
  )
);
