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 { updateModifierGroupSchema } from '@server/validators/menu-modifiers.validator';
import { getModifierGroup, updateModifierGroup, deleteModifierGroup } from '@server/services/menu-modifiers.service';

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

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

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